Class: Aegis::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/aegis/role.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, permissions, options) ⇒ Role

permissions is a hash like: permissions = lambda { |user| … }



7
8
9
10
11
12
# File 'lib/aegis/role.rb', line 7

def initialize(name, permissions, options)
  @name = name
  @permissions = permissions
  @default_permission = options[:default_permission] == :allow ? :allow : :deny
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symb, *args) ⇒ Object (private)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aegis/role.rb', line 37

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

Instance Attribute Details

#default_permissionObject (readonly)

Returns the value of attribute default_permission.



4
5
6
# File 'lib/aegis/role.rb', line 4

def default_permission
  @default_permission
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/aegis/role.rb', line 4

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



23
24
25
# File 'lib/aegis/role.rb', line 23

def <=>(other)
  name.to_s <=> other.name.to_s
end

#allow_by_default?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/aegis/role.rb', line 14

def allow_by_default?
  @default_permission == :allow
end

#idObject



31
32
33
# File 'lib/aegis/role.rb', line 31

def id
  name.to_s
end

#may?(permission, *args) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/aegis/role.rb', line 18

def may?(permission, *args)
  # puts "may? #{permission}, #{args}"
  @permissions.may?(self, permission, *args)
end

#to_sObject



27
28
29
# File 'lib/aegis/role.rb', line 27

def to_s
  name.to_s.humanize
end