pseudo_member/app/models/pseudo_user.rb

99 lines
2.6 KiB
Ruby
Raw Permalink Normal View History

2017-09-21 12:50:17 +00:00
class PseudoUser
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::SecurePassword
field :user_name
field :email
field :first_name
field :last_name
field :last_login, :type => DateTime, :default => DateTime.now
field :password_digest, type: String
field :confirmation_token, type: String
field :reset_token, type: String
field :enabled, type: Boolean, :default => false
has_secure_password
validates :user_name, uniqueness: true
validates :email, uniqueness: true
validates :password, :on => [:create], length: {:in => 8..20}
def generate_confirmation_token(url)
self.confirmation_token = SecureRandom.hex(5)
self.save
# PseudoMemberMailer.send_user_confirmation(self).deliver
email = Email.new
email.mail_to = self.email
email.mail_subject = "Please confirm your email."
email.template = "admin/pseudo_members/confirm_email.html.erb"
email.template_data = {
"name" => self.name,
"url" => "#{url}&confirmation_token=#{self.confirmation_token}"
}
email.deliver
end
2018-06-23 16:49:07 +00:00
def generate_reset_token(url)
self.reset_token = SecureRandom.hex(5)
self.save
# PseudoMemberMailer.send_user_confirmation(self).deliver
email = Email.new
email.mail_to = self.email
email.mail_subject = "Click to reset your password."
email.template = "admin/pseudo_members/reset_password.html.erb"
email.template_data = {
"name" => self.name,
"url" => "#{url}&reset_token=#{self.reset_token}"
}
email.deliver
end
2017-09-21 12:50:17 +00:00
def is_confirmed?
self.confirmation_token.nil?
end
def name
self.first_name + " " + self.last_name rescue self.email
end
2018-06-23 16:49:07 +00:00
# def generate_reset_token
# self.reset_token = SecureRandom.hex(5)
# self.save
# end
2017-09-21 12:50:17 +00:00
def self.check_password_token(reset_token = nil)
user = self.find_by(reset_token: reset_token) rescue nil
token_status = user.present?
if token_status
true
else
false
end
end
def update_password(password, password_confirmation)
2018-01-19 13:30:59 +00:00
self.update_attributes(password: password, password_confirmation: password_confirmation, reset_token: nil)
2017-09-21 12:50:17 +00:00
self.save
end
def self.confirm_email(confirmation_token = nil)
if confirmation_token
user = self.find_by(confirmation_token: confirmation_token) rescue nil
token_status = user.present?
case token_status
when true
user.confirmation_token = nil
user.save
return {:success => true, :id => user.id.to_s}
when false
return {:success => false}
end
else
return {:success => false}
end
end
end