Sha256: d1095c3a363bf8e0213cd7af92ae5a03b125357431a2994e57d9d7e9ea7fd7b1

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

module Aegis
  class Role
  
    attr_reader :name, :default_permission
          
    # permissions is a hash like: permissions[:edit_user] = lambda { |user| ... }
    def initialize(name, permissions, options)
      @name = name
      @permissions = permissions
      @default_permission = options[:default_permission] == :allow ? :allow : :deny
      freeze
    end
    
    def allow_by_default?
      @default_permission == :allow
    end
    
    def may?(permission, *args)
      # puts "may? #{permission}, #{args}"
      @permissions.may?(self, permission, *args)
    end
    
    def <=>(other)
      name.to_s <=> other.name.to_s
    end

    def to_s
      name.to_s.humanize
    end
    
    def id
      name.to_s
    end

    private
    
    def method_missing(symb, *args)
      method_name = symb.to_s
      if method_name =~ /^may_(.+)(\?|\!)$/
        permission, severity = $1, $2
        permission = Aegis::Normalization.normalize_permission(permission)
        may = may?(permission, *args)
        if severity == '!' && !may 
          raise PermissionError, "Access denied: #{permission}" 
        else
          may
        end
      else
        super
      end
    end
        
    
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
makandra-aegis-1.1.1 lib/aegis/role.rb
makandra-aegis-1.1.2 lib/aegis/role.rb
aegis-1.1.8 lib/aegis/role.rb
aegis-1.1.7 lib/aegis/role.rb
bmaland-aegis-1.1.6 lib/aegis/role.rb
aegis-1.1.6 lib/aegis/role.rb
aegis-1.1.5 lib/aegis/role.rb
bmaland-aegis-1.1.5 lib/aegis/role.rb
aegis-1.1.4 lib/aegis/role.rb
aegis-1.1.3 lib/aegis/role.rb