Class: ActionAuthorizer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb

Direct Known Subclasses

ApplicationAuthorizer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user, params) ⇒ Base

Returns a new instance of Base.



6
7
8
9
# File 'lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb', line 6

def initialize(current_user, params)
  @current_user = current_user
  @params = params
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



4
5
6
# File 'lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb', line 4

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb', line 4

def params
  @params
end

Class Method Details

.authorized?(current_user, controller, action, params) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



16
17
18
19
20
21
# File 'lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb', line 16

def authorized?(current_user, controller, action, params)
  authorizer = "#{controller}_authorizer".classify.constantize.new(current_user, params)
  result = (authorizer.skip?(action) || !!current_user&.persisted?) && authorizer.respond_to?(action.to_sym) && authorizer.send(action.to_sym)
  raise ActionAuthorizer::ResultError.new(result) if [true, false].exclude?(result)
  result
end

.skip_authentication(only: nil) ⇒ Object



23
24
25
26
27
# File 'lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb', line 23

def skip_authentication(only: nil)
  define_method(:skip?) do |action|
    only.blank? || [only].compact.flatten.map!(&:to_sym).include?(action.to_sym)
  end
end

Instance Method Details

#skip?(action) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/generators/action_authorizer/install/templates/app/authorizers/action_authorizer/base.rb', line 11

def skip?(action)
  false
end