Sha256: 9facad28b5c6f3ab05ac407596bf99b07a3d853ade90be92a626cc678b3d4b1a

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Auther
  class Account
    include ActiveModel::Validations

    attr_accessor :name, :login, :secure_login, :password, :secure_password, :paths

    validates :name, presence: true
    validates :paths, presence: {unless: lambda { |account| account.paths.is_a? Array }, message: "must be an array"}

    def initialize name: nil, login: nil, secure_login: nil, password: nil, secure_password: nil, paths: [], secret: nil
      @name = name
      @login = login
      @secure_login = secure_login
      @password = password
      @secure_password = secure_password
      @paths = paths
      @secret = secret
    end

    def valid?
      super && authorized_login? && authorized_password?
    end

    def invalid?
      !valid?
    end

    private

    def secret
      @secret
    end

    def decrypt attribute
      if attribute.present? && secret.present?
        cipher = Auther::Cipher.new secret
        cipher.decrypt attribute
      end
    end

    def authorized? attribute, secure_attribute, error_name
      if attribute == decrypt(secure_attribute)
        true
      else
        errors.add error_name, "is invalid"
        false
      end
    end

    def authorized_login?
      authorized? login, secure_login, "login"
    end

    def authorized_password?
      authorized? password, secure_password, "password"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
auther-1.0.0 app/models/auther/account.rb
auther-0.3.0 app/models/auther/account.rb