Class: Authorization::DevelopmentSupport::AnalyzerEngine::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_authorization/development_support/development_support.rb

Constant Summary collapse

@@role_objects =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, rules, engine) ⇒ Role

Returns a new instance of Role



81
82
83
84
85
# File 'lib/declarative_authorization/development_support/development_support.rb', line 81

def initialize (role, rules, engine)
  @role = role
  @rules = rules
  @engine = engine
end

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role



80
81
82
# File 'lib/declarative_authorization/development_support/development_support.rb', line 80

def role
  @role
end

Class Method Details

.all(engine) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/declarative_authorization/development_support/development_support.rb', line 125

def self.all (engine)
  rules_by_role = engine.auth_rules.inject({}) do |memo, rule|
    memo[rule.role] ||= []
    memo[rule.role] << rule
    memo
  end
  engine.roles.collect do |role|
    new(role, (rules_by_role[role] || []).
          collect {|rule| Rule.new(rule, engine)}, engine)
  end
end

.all_for_privilege(privilege, context, engine) ⇒ Object



136
137
138
139
140
141
# File 'lib/declarative_authorization/development_support/development_support.rb', line 136

def self.all_for_privilege (privilege, context, engine)
  privilege = privilege.is_a?(Symbol) ? Privilege.for_sym(privilege, engine) : privilege
  privilege_symbols = ([privilege] + privilege.ancestors).map(&:to_sym)
  all(engine).select {|role| role.rules.any? {|rule| rule.matches?([role.to_sym], privilege_symbols, context)}}.
      collect {|role| [role] + role.descendants}.flatten.uniq
end

.for_sym(role_sym, engine) ⇒ Object



121
122
123
# File 'lib/declarative_authorization/development_support/development_support.rb', line 121

def self.for_sym (role_sym, engine)
  @@role_objects[[role_sym, engine]] ||= new(role_sym, nil, engine)
end

Instance Method Details

#ancestors(role_symbol = nil) ⇒ Object

ancestors' privileges are included in in the current role



95
96
97
98
99
100
# File 'lib/declarative_authorization/development_support/development_support.rb', line 95

def ancestors (role_symbol = nil)
  role_symbol ||= @role
  (@engine.role_hierarchy[role_symbol] || []).
      collect {|lower_role| ancestors(lower_role) }.flatten +
    (role_symbol == @role ? [] : [Role.for_sym(role_symbol, @engine)])
end

#descendants(role_symbol = nil) ⇒ Object



101
102
103
104
105
106
# File 'lib/declarative_authorization/development_support/development_support.rb', line 101

def descendants (role_symbol = nil)
  role_symbol ||= @role
  (@engine.rev_role_hierarchy[role_symbol] || []).
      collect {|higher_role| descendants(higher_role) }.flatten +
    (role_symbol == @role ? [] : [Role.for_sym(role_symbol, @engine)])
end

#rulesObject



108
109
110
111
# File 'lib/declarative_authorization/development_support/development_support.rb', line 108

def rules
  @rules ||= @engine.auth_rules.select {|rule| rule.role == @role}.
      collect {|rule| Rule.new(rule, @engine)}
end

#rules_for_permission(privilege, context) ⇒ Object



112
113
114
115
116
# File 'lib/declarative_authorization/development_support/development_support.rb', line 112

def rules_for_permission (privilege, context)
  rules.select do |rule|
    rule.matches?([@role], [privilege.to_sym], context)
  end
end

#source_fileObject



90
91
92
# File 'lib/declarative_authorization/development_support/development_support.rb', line 90

def source_file
  @rules.empty? ? nil : @rules.first.source_file
end

#source_lineObject



87
88
89
# File 'lib/declarative_authorization/development_support/development_support.rb', line 87

def source_line
  @rules.empty? ? nil : @rules.first.source_line
end

#to_symObject



118
119
120
# File 'lib/declarative_authorization/development_support/development_support.rb', line 118

def to_sym
  @role
end