Module: Authorization::Maintenance::Usage

Defined in:
lib/declarative_authorization/maintenance.rb

Overview

Module for grouping usage-related helper methods

Class Method Summary collapse

Class Method Details

.usages_by_controllerObject

Delivers a hash of => usage_info_hash, where usage_info_hash has the form of



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/declarative_authorization/maintenance.rb', line 51

def self.usages_by_controller
  # load each application controller
  begin
    Dir.foreach(File.join(RAILS_ROOT, %w{app controllers})) do |entry|
      if entry =~ /^\w+_controller\.rb$/
        require File.join(RAILS_ROOT, %w{app controllers}, entry)
      end
    end
  rescue Errno::ENOENT
  end
  controllers = []
  ObjectSpace.each_object(Class) do |obj|
    controllers << obj if obj.ancestors.include?(ActionController::Base) and
                          !%w{ActionController::Base ApplicationController}.include?(obj.name)
  end

  controllers.inject({}) do |memo, controller|
    catchall_permissions = []
    permission_by_action = {}
    controller.all_filter_access_permissions.each do |controller_permissions|
      catchall_permissions << controller_permissions if controller_permissions.actions.include?(:all)
      controller_permissions.actions.reject {|action| action == :all}.each do |action|
        permission_by_action[action] = controller_permissions
      end
    end

    actions = controller.public_instance_methods(false) - controller.hidden_actions
    memo[controller] = actions.inject({}) do |actions_memo, action|
      action_sym = action.to_sym
      actions_memo[action_sym] =
        if permission_by_action[action_sym]
          {
            :privilege => permission_by_action[action_sym].privilege,
            :context   => permission_by_action[action_sym].context,
            :controller_permissions => [permission_by_action[action_sym]]
          }
        elsif !catchall_permissions.empty?
          {
            :privilege => catchall_permissions[0].privilege,
            :context   => catchall_permissions[0].context,
            :controller_permissions => catchall_permissions
          }
        else
          {}
        end
      actions_memo
    end
    memo
  end
end