Class: Authorization::Analyzer::MergeableRulesProcessor

Inherits:
GeneralAuthorizationProcessor show all
Defined in:
lib/declarative_authorization/authorization_rules_analyzer.rb

Instance Method Summary collapse

Methods inherited from GeneralAuthorizationProcessor

#analyze, #initialize, #process_arglist, #process_hash, #process_iter, #process_lit

Constructor Details

This class inherits a constructor from Authorization::Analyzer::GeneralAuthorizationProcessor

Instance Method Details

#analyze_rulesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/declarative_authorization/authorization_rules_analyzer.rb', line 69

def analyze_rules
  if @has_permission
    #p @has_permission
    permissions_by_context_and_rules = @has_permission.inject({}) do |memo, permission|
      key = [permission[:context], permission[:rules]]
      memo[key] ||= []
      memo[key] << permission
      memo
    end

    permissions_by_context_and_rules.each do |key, rules|
      if rules.length > 1
        rule_lines = rules.collect {|rule| rule[:line] }
        rules.each do |rule|
          @analyzer.reports << Report.new(:mergeable_rules, "", rule[:line],
            "Similar rules already in line(s) " +
                rule_lines.reject {|l| l == rule[:line] } * ", ")
        end
      end
    end
  end
end

#process_call(exp) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/declarative_authorization/authorization_rules_analyzer.rb', line 92

def process_call (exp)
  klass = exp.shift
  name = exp.shift
  case name
  when :role
    analyze_rules
    @has_permission = []
    s(:call, klass, name)
  when :has_permission_on
    arglist_line = exp[0].line
    arglist = process(exp.shift).shift
    context = arglist.shift
    args_hash = arglist.shift
    @has_permission << {
      :context => context,
      :rules => [],
      :privilege => args_hash && args_hash[:to],
      # a hack: call exp line seems to be wrong
      :line => arglist_line
    }
    s(:call, klass, name)
  when :to
    @has_permission.last[:privilege] = process(exp.shift).shift if @has_permission
    s(:call, klass, name)
  when :if_attribute
    @in_if_attribute = true
    rules = process(exp.shift).shift
    @has_permission.last[:rules] << rules if @has_permission
    @in_if_attribute = false
    s(:call, klass, name)
  else
    s(:call, klass, name, process(exp.shift))
  end
end