Sha256: 93adc3f031bc5f976a5a8146319a0811db3d131c13ba9747db44bc3a8ba6d682

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 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 options = {}
      @name = options.fetch :name, nil
      @login = options.fetch :login, nil
      @secure_login = options.fetch :secure_login, nil
      @password = options.fetch :password, nil
      @secure_password = options.fetch :secure_password, nil
      @paths = options.fetch :paths, []
      @secret = options.fetch :secret, nil
    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

3 entries across 3 versions & 1 rubygems

Version Path
auther-1.3.0 app/models/auther/account.rb
auther-1.2.0 app/models/auther/account.rb
auther-1.1.0 app/models/auther/account.rb