Class: Bali::Role

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

Constant Summary collapse

RIGHTS =
[
  INHERIT = :inherit,
  DEFAULT_DENY = :default_deny,
  DEFAULT_ALLOW = :default_allow
].freeze
IDENTIFIER_CLASSES =
[
  String,
  Symbol,
  NilClass,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Role

Returns a new instance of Role.



39
40
41
42
43
44
45
# File 'lib/bali/role.rb', line 39

def initialize(name)
  @name = name.to_sym if name
  @right_level = INHERIT

  @cans = {}
  @cants = {}
end

Instance Attribute Details

#can_allObject

Returns the value of attribute can_all.



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

def can_all
  @can_all
end

#cansObject

Returns the value of attribute cans.



15
16
17
# File 'lib/bali/role.rb', line 15

def cans
  @cans
end

#cantsObject

Returns the value of attribute cants.



15
16
17
# File 'lib/bali/role.rb', line 15

def cants
  @cants
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#right_levelObject

Returns the value of attribute right_level.



21
22
23
# File 'lib/bali/role.rb', line 21

def right_level
  @right_level
end

#scope(&block) ⇒ Object (readonly)

Returns the value of attribute scope.

Raises:



16
17
18
# File 'lib/bali/role.rb', line 16

def scope
  @scope
end

Class Method Details

.extract_roles_from_object(object) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/bali/role.rb', line 31

def self.extract_roles_from_object(object)
  method_name = object.class.role_field_for_authorization

  method_name ?
    formalize(object.send(method_name)) :
    formalize(nil)
end

.formalize(object) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/bali/role.rb', line 23

def self.formalize(object)
  case object
  when *IDENTIFIER_CLASSES then [object]
  when Array then (object.count == 0 ? nil : object)
  else formalize(extract_roles_from_object(object))
  end
end

Instance Method Details

#<<(rule) ⇒ Object

DSL METHODS



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bali/role.rb', line 88

def << rule
  operation = rule.operation.to_sym

  if rule.term == :cant
    cants[operation] = rule
    cans.delete operation
  else
    cans[operation] = rule
    cants.delete operation
  end
end

#add(term, *operations, block) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/bali/role.rb', line 79

def add(term, *operations, block)
  operations.each do |operation|
    rule = Bali::Rule.new(term, operation)
    rule.conditional = block if block
    self << rule
  end
end

#can(*args, &block) ⇒ Object

DSL METHODS



56
57
58
# File 'lib/bali/role.rb', line 56

def can(*args, &block)
  add :can, *args, block
end

#can_all?Boolean

Returns the value of attribute can_all.

Returns:

  • (Boolean)


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

def can_all
  @can_all
end

#cant(*args, &block) ⇒ Object



60
61
62
# File 'lib/bali/role.rb', line 60

def cant(*args, &block)
  add :cant, *args, block
end

#cant_allObject



68
69
70
# File 'lib/bali/role.rb', line 68

def cant_all
  @right_level = DEFAULT_DENY
end

#cant_all?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/bali/role.rb', line 51

def cant_all?
  right_level == DEFAULT_DENY
end

#find_rule(term, operation) ⇒ Object



100
101
102
103
104
105
# File 'lib/bali/role.rb', line 100

def find_rule(term, operation)
  case term
  when :can then cans[operation.to_sym]
  when :cant then cants[operation.to_sym]
  end
end

#rulesObject



107
108
109
# File 'lib/bali/role.rb', line 107

def rules
  cans.values + cants.values
end