Sha256: c8838c867e595207f995ac38b6d9b6eb585b36936b18cc7b8a8f32f4a9883f82

Contents?: true

Size: 1.96 KB

Versions: 10

Compression:

Stored size: 1.96 KB

Contents

require 'active_model'
require 'active_support'

# :nodoc: namespace
module Authpwn

# :nodoc: namespace
module UserExtensions

# Augments the User model with a password virtual attribute.
module PasswordField
  extend ActiveSupport::Concern

  included do
    validates :password, length: { minimum: 1, on: :create, allow_nil: true },
                         confirmation: { allow_nil: true }
  end

  # Credentials::Password instance associated with this user.
  def password_credential
    credentials.find { |c| c.instance_of?(Credentials::Password) }
  end

  # The password from the user's Password credential, or nil.
  #
  # Returns nil if this user has no Password credential.
  def password
    credential = self.password_credential
    credential && credential.password
  end

  # The password_confirmation from the user's Password credential, or nil.
  #
  # Returns nil if this user has no Password credential.
  def password_confirmation
    credential = self.password_credential
    credential && credential.password_confirmation
  end

  # Sets the password on the user's Password credential.
  #
  # Creates a new Credentials::Password instance if necessary.
  def password=(new_password)
    if credential = self.password_credential
      credential.password = new_password
    else
      credentials << Credentials::Password.new(password: new_password)
    end
    new_password
  end

  # Sets the password on the user's Password credential.
  #
  # Creates a new Credentials::Password instance if necessary.
  def password_confirmation=(new_password_confirmation)
    if credential = self.password_credential
      credential.password_confirmation = new_password_confirmation
    else
      credentials << Credentials::Password.new(password_confirmation:
                                               new_password_confirmation)
    end
    new_password_confirmation
  end
end  # module Authpwn::UserExtensions::PasswordField

end  # module Authpwn::UserExtensions

end  # module Authpwn

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
authpwn_rails-0.23.0 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.22.1 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.22.0 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.21.1 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.21.0 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.20.0 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.19.0 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.18.2 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.18.1 lib/authpwn_rails/user_extensions/password_field.rb
authpwn_rails-0.18.0 lib/authpwn_rails/user_extensions/password_field.rb