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


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
101
102
# File 'lib/declarative_authorization/maintenance.rb', line 55

def self.usages_by_controller
  # load each application controller
  begin
    Dir.glob(File.join(::Rails.root, 'app', 'controllers', '**', '*_controller\.rb')) do |entry|
      require entry
    end
  rescue Errno::ENOENT
  end
  controllers = ObjectSpace.each_object(Class).select do |obj|
    obj.ancestors.include?(ActionController::Base) &&
      obj != ActionController::Base &&
      (!obj.name || obj.name.demodulize != 'ApplicationController')
  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.to_a
    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