Class: IronAdmin::Policy
- Inherits:
-
Object
- Object
- IronAdmin::Policy
- Defined in:
- lib/iron_admin/policy.rb
Overview
Action aliases are supported:
:showand:indexare aliases for:read- Allowing
:readimplicitly allows:showand:index - Allowing
:showor:indexis treated as allowing:read
Authorization policy for controlling access to resource actions.
Policies define which actions users can perform on resources.
They use a simple DSL with allow and deny to manage permissions.
When no policy is defined for a resource, all actions are allowed by default. Once a policy block is provided, actions must be explicitly allowed. Deny rules take precedence over allow rules.
Constant Summary collapse
- ACTION_ALIASES =
Maps controller actions to CRUD operations.
{ show: :read, index: :read, }.freeze
- REVERSE_ALIASES =
Reverse mapping from CRUD operations to controller actions.
ACTION_ALIASES.each_with_object({}) do |(action, crud), hash| (hash[crud] ||= []) << action end.freeze
Instance Method Summary collapse
-
#action_allowed?(action_name, user) ⇒ Boolean
Checks if a custom action (or bulk action) is allowed.
-
#allow(*actions, if: nil) ⇒ void
Grants permission for one or more actions.
-
#allowed?(action, user) ⇒ Boolean
Checks if a CRUD action is allowed for the given user.
-
#deny(*actions, if: nil) ⇒ void
Denies permission for one or more actions.
-
#initialize { ... } ⇒ Policy
constructor
Creates a new Policy instance.
Constructor Details
#initialize { ... } ⇒ Policy
Creates a new Policy instance.
59 60 61 62 63 64 |
# File 'lib/iron_admin/policy.rb', line 59 def initialize(&) @allow_rules = {} @deny_rules = {} @configured = block_given? instance_eval(&) if block_given? end |
Instance Method Details
#action_allowed?(action_name, user) ⇒ Boolean
Checks if a custom action (or bulk action) is allowed.
Unlike #allowed?, this does not use action aliases.
Custom actions are allowed by default unless explicitly restricted
via an allow rule with a condition. This separates custom action
authorization from CRUD policy — custom actions are not gated by
the CRUD allowlist.
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/iron_admin/policy.rb', line 169 def action_allowed?(action_name, user) return true unless @configured action = action_name.to_sym return false if denied?(action, user) return true unless @allow_rules.key?(action) condition = @allow_rules[action] condition.nil? || condition.call(user) end |
#allow(*actions, if: nil) ⇒ void
This method returns an undefined value.
Grants permission for one or more actions.
82 83 84 85 |
# File 'lib/iron_admin/policy.rb', line 82 def allow(*actions, if: nil) condition = binding.local_variable_get(:if) actions.each { |action| @allow_rules[action] = condition } end |
#allowed?(action, user) ⇒ Boolean
Checks if a CRUD action is allowed for the given user.
Handles action aliases automatically:
- If :show or :index is checked, also checks for :read permission
- If :read is checked, also checks for :show/:index permissions
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/iron_admin/policy.rb', line 122 def allowed?(action, user) return true unless @configured # Deny rules take precedence over allow rules return false if denied?(action, user) # Check the action directly first if @allow_rules.key?(action) condition = @allow_rules[action] return condition.nil? || condition.call(user) end # Check forward alias (e.g., :show -> :read) aliased_action = ACTION_ALIASES[action] if aliased_action && @allow_rules.key?(aliased_action) condition = @allow_rules[aliased_action] return condition.nil? || condition.call(user) end # Check reverse aliases (e.g., :read -> [:show, :index]) reverse_actions = REVERSE_ALIASES[action] reverse_actions&.each do |reverse_action| next unless @allow_rules.key?(reverse_action) condition = @allow_rules[reverse_action] return condition.nil? || condition.call(user) end false end |
#deny(*actions, if: nil) ⇒ void
This method returns an undefined value.
Denies permission for one or more actions.
Deny rules take precedence over allow rules. If an action is both allowed and denied, the deny rule wins.
103 104 105 106 |
# File 'lib/iron_admin/policy.rb', line 103 def deny(*actions, if: nil) condition = binding.local_variable_get(:if) actions.each { |action| @deny_rules[action] = condition } end |