Class: Ckeditor::Hooks::ActionPolicyAuthorization
- Inherits:
-
Object
- Object
- Ckeditor::Hooks::ActionPolicyAuthorization
- Includes:
- Ckeditor::Helpers::Controllers
- Defined in:
- lib/ckeditor/hooks/action_policy.rb
Overview
This adapter is for the Action Policy authorization library. You can create another adapter for different authorization behavior, just be certain it responds to each of the public methods here.
Instance Method Summary collapse
-
#authorize(_action, model_object = nil) ⇒ Object
This method is called in every controller action and should raise an exception when the authorization fails.
-
#authorized?(action, model_object = nil) ⇒ Boolean
This method is called primarily from the view to determine whether the given user has access to perform the action on a given model.
-
#initialize(controller) ⇒ ActionPolicyAuthorization
constructor
See the
authorize_with
config method for where the initialization happens.
Constructor Details
#initialize(controller) ⇒ ActionPolicyAuthorization
See the authorize_with
config method for where the initialization happens.
14 15 16 |
# File 'lib/ckeditor/hooks/action_policy.rb', line 14 def initialize(controller) @controller = controller end |
Instance Method Details
#authorize(_action, model_object = nil) ⇒ Object
This method is called in every controller action and should raise an exception when the authorization fails. The first argument is the name of the controller action as a symbol (:create, :destroy, etc.). The second argument is the actual model instance if it is available.
22 23 24 |
# File 'lib/ckeditor/hooks/action_policy.rb', line 22 def (_action, model_object = nil) @controller.(model_object, context: {user: @controller.ckeditor_current_user}) end |
#authorized?(action, model_object = nil) ⇒ Boolean
This method is called primarily from the view to determine whether the given user has access to perform the action on a given model. It should return true when authorized. This takes the same arguments as authorize
. The difference is that this will return a boolean whereas authorize
will raise an exception when not authorized.
30 31 32 33 34 35 36 |
# File 'lib/ckeditor/hooks/action_policy.rb', line 30 def (action, model_object = nil) if action @controller.allowed_to?(:"#{action}?", model_object, context: {user: @controller.ckeditor_current_user}) end end |