pseudo_member/app/models/pseudo_user.rb

83 lines
2.1 KiB
Ruby

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
def is_confirmed?
self.confirmation_token.nil?
end
def name
self.first_name + " " + self.last_name rescue self.email
end
def generate_reset_token
self.reset_token = SecureRandom.hex(5)
self.save
end
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)
self.update_attributes(password: password, password_confirmation: password_confirmation, reset_token: nil)
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