Sha256: ac3642b8d533886d89a290f7ef20de539daf6fa8a6b2fa55ed0c1fdece0ba328

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'securerandom'
require 'scrypt'

module Alfa
  class << self
    attr_accessor :GROUPS
  end

  class GuestUser
    def self.grants
      @grants ||= Alfa.GROUPS[:public]
    end

    def self.grant?(name)
      grants.include?(name.to_sym)
    end

    def self.groups
      []
    end

    def self.group?(name)
      groups.include?(name.to_sym)
    end

    def self.logged?
      false
    end

    def [](key)
      nil
    end

    def method_missing(*o)
      nil
    end
  end


  class User
    def initialize(object)
      @object = object
    end

    def grants
      (groups + [:public]).map{|g| Alfa.GROUPS[g] || []}.flatten
    end

    def grant?(name)
      grants.include?(name.to_sym)
    end

    # @return Array
    def groups
      @object.groups.map{|s| s.to_s.strip.to_sym}
    end

    def group?(name)
      groups.include?(name.to_sym)
    end

    def logged?
      true
    end

    def [](key)
      @object[key]
    end

    def method_missing(*o)
      @object.send(*o)
    end

    def self.method_missing(*o)
      @object.class.send(*o)
    end
  end

  # Prepend this module in User model in your Project
  # Example:
  #   class User < Sequel::Model(DB::Main[:users])
  #     prepend Alfa::UserModule
  #   end
  module UserModule
    def password=(p)
      self.passhash = SCrypt::Password.create(p)
    end

    def password_valid?(p)
      SCrypt::Password.new(self.passhash) == p
    end

    def groups=(g)
      super(Array(g))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alfa-0.0.8.pre lib/alfa/user.rb