Module: Critic::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/critic/controller.rb

Overview

Sugar for accessing the authorization interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



37
38
39
# File 'lib/critic/controller.rb', line 37

def authorization
  @authorization
end

Instance Method Details

#authorize(resource, action = default_action, policy: policy(resource), with: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/critic/controller.rb', line 13

def authorize(resource, action = default_action, policy: policy(resource), with: nil)
  authorizing!

  args = [with] if !with.is_a?(Array) && !with.nil?

  @authorization = policy.authorize(action, critic, resource, args)

  authorization_failed! if @authorization.denied?

  @authorization.result
end

#authorize_scope(scope, *args, action: nil, policy: policy(scope), **options) ⇒ Object



31
32
33
34
35
# File 'lib/critic/controller.rb', line 31

def authorize_scope(scope, *args, action: nil, policy: policy(scope), **options)
  authorization_action = action || policy.scope

  authorize(scope, authorization_action, *args, policy: policy, **options)
end

#authorized?(resource, *args, **options) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/critic/controller.rb', line 25

def authorized?(resource, *args, **options)
  authorize(resource, *args, **options)
rescue Critic::AuthorizationDenied
  false
end