Class: Ckeditor::Hooks::CanCanAuthorization

Inherits:
Object
  • Object
show all
Defined in:
lib/ckeditor/hooks/cancan.rb

Overview

This adapter is for the CanCanCan 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, ability = ::Ability) ⇒ CanCanAuthorization

See the authorize_with config method for where the initialization happens.



12
13
14
15
16
17
# File 'lib/ckeditor/hooks/cancan.rb', line 12

def initialize(controller, ability = ::Ability)
  @controller = controller
  @controller.instance_variable_set '@ability', ability
  @controller.extend ControllerExtension
  @controller.current_ability.authorize! :access, :ckeditor
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.



23
24
25
26
27
28
# File 'lib/ckeditor/hooks/cancan.rb', line 23

def authorize(action, model_object = nil)
  if action
    @controller.instance_variable_set(:@_authorized, true)
    @controller.current_ability.authorize!(action.to_sym, model_object)
  end
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)


34
35
36
# File 'lib/ckeditor/hooks/cancan.rb', line 34

def authorized?(action, model_object = nil)
  @controller.current_ability.can?(action.to_sym, model_object) if action
end