Class: Accessly::Policy::Base
- Inherits:
-
Object
- Object
- Accessly::Policy::Base
- Defined in:
- lib/accessly/policy/base.rb
Constant Summary collapse
- ACTIONS_MODULE =
Module that will hold our meta-programmed action methods just above the policy class in the inheritance hierarchy; allowing for them to be overridden in the policy.
:Actions
Instance Attribute Summary collapse
-
#actor ⇒ Object
readonly
Returns the value of attribute actor.
Class Method Summary collapse
-
.actions(actions) ⇒ Hash
Meta-programs action methods from actions supplied.
-
.actions_on_objects(actions_on_objects) ⇒ Hash
Meta-programs action_on_objects methods from the actions supplied.
- .model_scope ⇒ Object
- .namespace ⇒ Object
Instance Method Summary collapse
- #accessly_query ⇒ Object
-
#actors ⇒ Object
Specifies all the actors used in permission lookups.
- #can?(action, object = nil) ⇒ Boolean
- #grant!(action, object = nil) ⇒ Object
- #grant_object ⇒ Object
-
#initialize(actor) ⇒ Base
constructor
A new instance of Base.
- #list(action) ⇒ Object
- #model_scope ⇒ Object
- #namespace ⇒ Object
- #revoke!(action, object = nil) ⇒ Object
- #revoke_object ⇒ Object
- #segment_id ⇒ Object
- #unrestricted? ⇒ Boolean
Constructor Details
#initialize(actor) ⇒ Base
Returns a new instance of Base.
11 12 13 |
# File 'lib/accessly/policy/base.rb', line 11 def initialize(actor) @actor = actor end |
Instance Attribute Details
#actor ⇒ Object (readonly)
Returns the value of attribute actor.
9 10 11 |
# File 'lib/accessly/policy/base.rb', line 9 def actor @actor end |
Class Method Details
.actions(actions) ⇒ Hash
Meta-programs action methods from actions supplied. Used in policies as a DSL to declare actions.
This defines the actions on the ‘actions_module` so that they are positioned higher in the inheritance tree than methods defined on the class itself. This will allow us to define methods that override these base methods and call `super`.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/accessly/policy/base.rb', line 41 def self.actions(actions) _actions.merge!(actions) actions.each do |action, action_id| actions_module.module_eval do define_method(:"#{action}?") do |*args| _can_do_action?(action, action_id, args.first) end end end end |
.actions_on_objects(actions_on_objects) ⇒ Hash
Meta-programs action_on_objects methods from the actions supplied. Used in policies as a DSL to declare actions on objects. It is different from actions in that it will also define a method for listing all objects authorized with this action for the given actor and that these actions will always be associated not only with an actor, but with an object of the action.
on the policy
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/accessly/policy/base.rb', line 79 def self.actions_on_objects(actions_on_objects) _actions_on_objects.merge!(actions_on_objects) actions_on_objects.each do |action, action_id| actions_module.module_eval do define_method(:"#{action}?") do |*args| _can_do_action?(action, action_id, args.first) end define_method(action) do |*args| _list_for_action(action, action_id) end end end end |
.model_scope ⇒ Object
102 103 104 |
# File 'lib/accessly/policy/base.rb', line 102 def self.model_scope raise ArgumentError.new("#model_scope is not defined on #{self.name}.") end |
.namespace ⇒ Object
94 95 96 |
# File 'lib/accessly/policy/base.rb', line 94 def self.namespace String(self) end |
Instance Method Details
#accessly_query ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/accessly/policy/base.rb', line 150 def accessly_query @_accessly_query ||= begin query = Accessly::Query.new(actors) query.on_segment(segment_id) unless segment_id.nil? query end end |
#actors ⇒ Object
Specifies all the actors used in permission lookups. Override this method in child policy classes to specify other actors that the actor given in the initializer may inherit permissions from.
114 115 116 |
# File 'lib/accessly/policy/base.rb', line 114 def actors actor end |
#can?(action, object = nil) ⇒ Boolean
126 127 128 129 130 131 132 |
# File 'lib/accessly/policy/base.rb', line 126 def can?(action, object = nil) if object.nil? send("#{action}?") else send("#{action}?", object) end end |
#grant!(action, object = nil) ⇒ Object
138 139 140 141 142 |
# File 'lib/accessly/policy/base.rb', line 138 def grant!(action, object = nil) object_id = _get_object_id(object) action_id = _get_action_id(action, object_id) grant_object.grant!(action_id, namespace, object_id) end |
#grant_object ⇒ Object
158 159 160 161 162 163 |
# File 'lib/accessly/policy/base.rb', line 158 def grant_object grant_object = Accessly::Permission::Grant.new(actor) grant_object.on_segment(segment_id) unless segment_id.nil? grant_object end |
#list(action) ⇒ Object
134 135 136 |
# File 'lib/accessly/policy/base.rb', line 134 def list(action) send(action) end |
#model_scope ⇒ Object
106 107 108 |
# File 'lib/accessly/policy/base.rb', line 106 def model_scope self.class.model_scope end |
#namespace ⇒ Object
98 99 100 |
# File 'lib/accessly/policy/base.rb', line 98 def namespace self.class.namespace end |
#revoke!(action, object = nil) ⇒ Object
144 145 146 147 148 |
# File 'lib/accessly/policy/base.rb', line 144 def revoke!(action, object = nil) object_id = _get_object_id(object) action_id = _get_action_id(action, object_id) revoke_object.revoke!(action_id, namespace, object_id) end |
#revoke_object ⇒ Object
165 166 167 168 169 170 |
# File 'lib/accessly/policy/base.rb', line 165 def revoke_object revoke_object = Accessly::Permission::Revoke.new(actor) revoke_object.on_segment(segment_id) unless segment_id.nil? revoke_object end |
#segment_id ⇒ Object
122 123 124 |
# File 'lib/accessly/policy/base.rb', line 122 def segment_id nil end |
#unrestricted? ⇒ Boolean
118 119 120 |
# File 'lib/accessly/policy/base.rb', line 118 def unrestricted? false end |