Module: Backframe::ActsAsUser::ClassMethods

Defined in:
lib/backframe/activerecord/acts_as_user.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_user(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/backframe/activerecord/acts_as_user.rb', line 13

def acts_as_user(*args)
  arguments = args[0] || {}

  reset = arguments[:reset] || 'Backframe::Reset'
  activation = arguments[:activation] || 'Backframe::Activation'

  attr_accessor :password, :change_password, :set_password, :old_password, :new_password, :confirm_password, :confirm_email

  has_many :activations, :class_name => activation, :dependent => :destroy, :as => :user
  has_many :resets, :class_name => reset, :dependent => :destroy, :as => :user

  after_validation :set_new_password, :if => Proc.new { |u| u.new_password.present? }
  after_create :activate

  validates_presence_of :first_name, :last_name, :email
  validate :validate_password, :if => Proc.new { |u| u.change_password.present? || u.set_password.present? }

  class_eval "    def full_name\n      self.first_name+' '+self.last_name\n    end\n\n    def rfc822\n      self.full_name+' <'+self.email+'>'\n    end\n\n    def locked_out?\n      self.signin_locked_at.present? && self.signin_locked_at > Time.now.ago(5.minutes)\n    end\n\n    def signin(status)\n      if status == :success\n        self.signin_locked_at = nil\n        self.signin_attempts = 0\n      elsif status == :failed\n        self.signin_attempts += 1\n        if self.signin_attempts >= 5\n          self.signin_attempts = 0\n          self.signin_locked_at = Time.now\n        end\n      end\n      self.save\n    end\n\n    def authenticate(password)\n      self.password_hash == BCrypt::Engine.hash_secret(password, self.password_salt)\n    end\n\n    def password=(password)\n      @password = password\n      self.password_salt = BCrypt::Engine.generate_salt\n      self.password_hash = BCrypt::Engine.hash_secret(password, self.password_salt)\n    end\n\n    def password\n      @password\n    end\n\n    def validate_password\n      if self.change_password && self.old_password.blank?\n        return self.errors.add(:old_password, I18n.t('activerecord.errors.messages.blank'))\n      elsif self.change_password && !authenticate(self.old_password)\n        return self.errors.add(:old_password, I18n.t(:user_invalid_old_password))\n      elsif self.new_password.blank? || self.confirm_password.blank?\n        return self.errors.add(:new_password, I18n.t(:user_unconfirmed_password))\n      elsif self.confirm_password != self.new_password\n        return self.errors.add(:new_password, I18n.t(:user_unmatching_passwords))\n      end\n    end\n\n    def set_new_password\n      self.password = self.new_password\n    end\n\n    def activate\n      self.activations.create\n    end\n\n    def reset\n      self.resets.create\n    end\n\n  EOV\n\nend\n"