Class: Protected::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/protected/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_passwordObject

Returns the value of attribute current_password.



85
86
87
# File 'app/models/protected/user.rb', line 85

def current_password
  @current_password
end

Class Method Details

.reset_password_and_send_password_instructions(params) ⇒ Object



147
148
149
150
151
# File 'app/models/protected/user.rb', line 147

def reset_password_and_send_password_instructions(params)
  record = find_by_email(params["email"])
  record.random_password && record.save unless record.nil?
  send_reset_password_instructions(params)
end

Instance Method Details

#archive_passwordObject

TODO: Extract this code into security pack extension



90
91
92
93
94
# File 'app/models/protected/user.rb', line 90

def archive_password
  if(self.encrypted_password_changed?)
    self.old_passwords.create! :encrypted_password => self.encrypted_password, :password_salt => self.password_salt, :password_archivable_id => self.id, :password_archivable_type => self.class.to_s
  end
end

#nameObject



108
109
110
# File 'app/models/protected/user.rb', line 108

def name
  [first_name, last_name].join(' ')
end

#password_used?(password) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'app/models/protected/user.rb', line 112

def password_used?(password)
  self.password = password
  return password_archive_included?
end

#random_passwordObject



129
130
131
# File 'app/models/protected/user.rb', line 129

def random_password
  self.password = self.password_confirmation = PasswordUtils.generate_random_password
end

#reset_password!(pass, conf) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/protected/user.rb', line 117

def reset_password!(pass,conf)
  if reset_password_token.present? and
     pass == conf and
     encrypted_password_changed? and
     password_archive_included?
     errors.add(:base, I18n.t('errors.messages.taken_in_past'))
  else
    super
  end
  self
end

#send_mail_if_neededObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/protected/user.rb', line 133

def send_mail_if_needed
  if !self.changes.empty? &&
    self.changes[:id].present? &&
    self.changes[:id][0].nil? &&
    self.changes[:id][1].present?
    if self.first_login?
      self.archive_password
      UserMailer.(self).deliver
      UserMailer.welcome_password_instructions(self).deliver
    end
  end
end

#update_on_first_login!(modified_user) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/protected/user.rb', line 96

def update_on_first_login!(modified_user)
  if password_used?(modified_user[:password])
    raise PasswordAlreadyUsedException
  else
    update_attributes!(
      :password => modified_user[:password],
      :password_confirmation => modified_user[:password_confirmation],
      :first_login => false)
    archive_password
  end
end