class User
Public Instance Methods
# File lib/generators/authkit/templates/app/models/user.rb, line 120 def change_password(password, password_confirmation) self.password = password self.password_confirmation = password_confirmation # Don't nil out the token unless the changes are valid as it may be # needed again (when re-rendering the form, for instance) if valid? self.reset_password_token = nil self.reset_password_token_created_at = nil end self.save end
# File lib/generators/authkit/templates/app/models/user.rb, line 65 def confirmation_token_expired? # TODO confirmation tokens expire in 3 days by default self.confirmation_token_created_at.blank? || self.confirmation_token_created_at <= 3.days.ago end
# File lib/generators/authkit/templates/app/models/user.rb, line 105 def email_confirmed return false if self.confirmation_token.blank? || self.confirmation_email.blank? self.email = self.confirmation_email # Don't nil out the token unless the changes are valid as it may be # needed again (when re-rendering the form, for instance) if valid? self.confirmation_token = nil self.confirmation_token_created_at = nil end self.save end
# File lib/generators/authkit/templates/app/models/user.rb, line 47 def full_name [first_name, last_name].compact.join(" ") end
# File lib/generators/authkit/templates/app/models/user.rb, line 38 def full_name=(value) return if value.blank? splitter = FullNameSplitter.new(value) self.first_name = splitter.first_name self.last_name = splitter.last_name self.full_name end
# File lib/generators/authkit/templates/app/models/user.rb, line 34 def incomplete? <% if username? %>username.blank? || <% end %>email.blank? || password_digest.blank? end
# File lib/generators/authkit/templates/app/models/user.rb, line 101 def pending_confirmation? self.confirmation_token.present? end
# File lib/generators/authkit/templates/app/models/user.rb, line 60 def reset_password_token_expired? # TODO reset password tokens expire in 1 day by default self.reset_password_token_created_at.blank? || self.reset_password_token_created_at <= 1.day.ago end
The tokens created by this method have unique indexes but collisions are very unlikely (1/64^32). Because of this there shouldn't be a conflict. If one occurs the ActiveRecord::StatementInvalid or ActiveRecord::RecordNotUnique exeception should bubble up.
# File lib/generators/authkit/templates/app/models/user.rb, line 92 def send_confirmation self.confirmation_token = SecureRandom.urlsafe_base64(32) self.confirmation_token_created_at = Time.now self.save! # TODO: insert your mailer logic here true end
The tokens created by this method have unique indexes but collisions are very unlikely (1/64^32). Because of this there shouldn't be a conflict. If one occurs the ActiveRecord::StatementInvalid or ActiveRecord::RecordNotUnique exeception should bubble up.
# File lib/generators/authkit/templates/app/models/user.rb, line 79 def send_reset_password self.reset_password_token = SecureRandom.urlsafe_base64(32) self.reset_password_token_created_at = Time.now self.save! # TODO: insert your mailer logic here true end
# File lib/generators/authkit/templates/app/models/user.rb, line 70 def send_welcome # TODO insert your mailer logic here true end
# File lib/generators/authkit/templates/app/models/user.rb, line 30 def suspended? self.suspended_at.present? end
# File lib/generators/authkit/templates/app/models/user.rb, line 51 def track_sign_in(ip) self.sign_in_count += 1 self.last_sign_in_at = self.current_sign_in_at self.last_sign_in_ip = self.current_sign_in_ip self.current_sign_in_at = Time.now self.current_sign_in_ip = ip self.save end
Protected Instance Methods
# File lib/generators/authkit/templates/app/models/user.rb, line 164 def confirmation_email_set? confirmation_email.present? && confirmation_email_changed? && confirmation_email != email end
It is possible that a user will change their email, not confirm, and then sign up for the service again using the same email. If they later go to confirm the email change on the first account it will fail because the email will be used by the new signup. Though this is problematic it avoids the larger problem of users blocking new user signups by changing their email address to something they don't control. This check is just for convenience and does not need to guarantee uniqueness.
# File lib/generators/authkit/templates/app/models/user.rb, line 175 def confirmation_email_uniqueness errors.add(:confirmation_email, :taken, value: email) if User.where('email = ?', confirmation_email).count > 0 end
# File lib/generators/authkit/templates/app/models/user.rb, line 156 def downcase_email self.email = self.email.downcase if self.email end
# File lib/generators/authkit/templates/app/models/user.rb, line 136 def has_auth? <% if oauth? %> self.auths.first.present? <% else %> false <% end %> end
# File lib/generators/authkit/templates/app/models/user.rb, line 144 def has_auth_or_skip_password_validation? has_auth? || skip_password_validation? end
# File lib/generators/authkit/templates/app/models/user.rb, line 152 def has_password? self.password.present? end
# File lib/generators/authkit/templates/app/models/user.rb, line 160 def set_confirmation_email self.confirmation_email = self.email if self.confirmation_email.blank? end
# File lib/generators/authkit/templates/app/models/user.rb, line 148 def skip_password_validation? self.password.blank? && self.password_digest.present? end