Class: Authorization::DevelopmentSupport::Analyzer::MergeableRulesProcessor

Inherits:
GeneralAuthorizationProcessor show all
Defined in:
lib/declarative_authorization/development_support/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::DevelopmentSupport::Analyzer::GeneralAuthorizationProcessor

Instance Method Details

#analyze_rulesObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/declarative_authorization/development_support/analyzer.rb', line 178

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/declarative_authorization/development_support/analyzer.rb', line 201

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
    rules = process(exp.shift).shift
    rules.unshift :if_attribute
    @has_permission.last[:rules] << rules if @has_permission
    s(:call, klass, name)
  when :if_permitted_to
    rules = process(exp.shift).shift
    rules.unshift :if_permitted_to
    @has_permission.last[:rules] << rules if @has_permission
    s(:call, klass, name)
  else
    s(:call, klass, name, process(exp.shift))
  end
end