Class: Ckeditor::Hooks::PunditAuthorization

Inherits:
Object
  • Object
show all
Includes:
Ckeditor::Helpers::Controllers
Defined in:
lib/ckeditor/hooks/pundit.rb

Overview

This adapter is for the Pundit authorization library. You can create another adapter for different authorization behavior, just be certain it responds to each of the public methods here.

Defined Under Namespace

Modules: ControllerExtension

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ PunditAuthorization

See the authorize_with config method for where the initialization happens.



13
14
15
16
# File 'lib/ckeditor/hooks/pundit.rb', line 13

def initialize(controller)
  @controller = controller
  @controller.extend ControllerExtension
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.

Raises:

  • (Pundit::NotAuthorizedError)


22
23
24
# File 'lib/ckeditor/hooks/pundit.rb', line 22

def authorize(action, model_object = nil)
  raise Pundit::NotAuthorizedError unless authorized?(action, model_object)
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.

Returns:

  • (Boolean)


30
31
32
# File 'lib/ckeditor/hooks/pundit.rb', line 30

def authorized?(action, model_object = nil)
  Pundit.policy(@controller.current_user_for_pundit, model_object).public_send(action + '?' ) if action
end