Class: WhurlEngine::Extensions::CanCan::AuthorizationAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/whurl_engine/extensions/cancan/authorization_adapter.rb

Overview

This adapter is for the CanCan 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) ⇒ AuthorizationAdapter

See the authorize_with config method for where the initialization happens.



10
11
12
13
14
15
# File 'lib/whurl_engine/extensions/cancan/authorization_adapter.rb', line 10

def initialize(controller, ability = ::Ability)
  @controller = controller
  @controller.instance_variable_set '@ability', ability
  @controller.extend ControllerExtension
  @controller.current_ability.authorize!(:access, :whurl_engine)
end

Instance Method Details

#attributes_for(action, abstract_model) ⇒ Object

This is called in the new/create actions to determine the initial attributes for new records. It should return a hash of attributes which match what the user is authorized to create.



47
48
49
50
# File 'lib/whurl_engine/extensions/cancan/authorization_adapter.rb', line 47

def attributes_for(action, abstract_model)
  action = translate_action(action)
  @controller.current_ability.attributes_for(action, abstract_model && abstract_model.model)
end

#authorize(action, abstract_model = nil, 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, :bulk_delete, etc.). The second argument is the AbstractModel instance that applies. The third argument is the actual model instance if it is available.



22
23
24
25
# File 'lib/whurl_engine/extensions/cancan/authorization_adapter.rb', line 22

def authorize(action, abstract_model = nil, model_object = nil)
  action = translate_action(action)
  @controller.current_ability.authorize!(action, model_object || abstract_model && abstract_model.model) if action
end

#authorized?(action, abstract_model = nil, 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)


31
32
33
34
# File 'lib/whurl_engine/extensions/cancan/authorization_adapter.rb', line 31

def authorized?(action, abstract_model = nil, model_object = nil)
  action = translate_action(action)
  @controller.current_ability.can?(action, model_object || abstract_model && abstract_model.model) if action
end

#query(action, abstract_model) ⇒ Object

This is called when needing to scope a database query. It is called within the list and bulk_delete/destroy actions and should return a scope which limits the records to those which the user can perform the given action on.



39
40
41
42
# File 'lib/whurl_engine/extensions/cancan/authorization_adapter.rb', line 39

def query(action, abstract_model)
  action = translate_action(action)
  abstract_model.model.accessible_by(@controller.current_ability, action)
end