Class: ActionAuthorization::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/authorizer/resource.rb

Overview

This class represents a generic list of models that are about to authorized.

It is instantiated automatically by ActionController::Metal#check_authorization and there should be little need to instantiate it directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, actor, *resources, **options) ⇒ Resource

Creates a new instance of Resource.

Parameters:

  • action (String, Symbol)

    The name of the action being performed.

  • actor (Model)

    The model attempting authorization.

  • *resources (Model)

    The list of models being authorized.

  • **options

    Any additional options regarding the authorization options.



30
31
32
33
34
35
# File 'lib/authorizer/resource.rb', line 30

def initialize(action, actor, *resources, **options)
    @action = action
    @actor = actor
    @resources = resources
    @options = options
end

Instance Attribute Details

#actionString, Symbol (readonly)

Returns The action which :actor is attempting to complete.

Returns:

  • (String, Symbol)

    The action which :actor is attempting to complete.



12
13
14
# File 'lib/authorizer/resource.rb', line 12

def action
  @action
end

#actorModel (readonly)

Returns The model attempting authorization (usually a User).

Returns:

  • (Model)

    The model attempting authorization (usually a User).



15
16
17
# File 'lib/authorizer/resource.rb', line 15

def actor
  @actor
end

#optionsObject (readonly)

Returns The options which are being used for authorization.

Returns:

  • The options which are being used for authorization.



21
22
23
# File 'lib/authorizer/resource.rb', line 21

def options
  @options
end

#resourcesObject (readonly)

Returns The list of models being authorized.

Returns:

  • The list of models being authorized.



18
19
20
# File 'lib/authorizer/resource.rb', line 18

def resources
  @resources
end

Instance Method Details

#getObject

Returns the list of models passed into the constructor if the list passes authorization, otherwise raises ForbiddenError.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/authorizer/resource.rb', line 42

def get
  return @resources if @resources.nil?
  return @resources if @resources.length == 0
  
  behavior = @options[:behavior]
  if !behavior
      behavior = :filter
  end
  
  case behavior
  when :allow_all
      collect_permitted(return_res: true) {|results| results.length > 0}
  when :deny_all
      collect_permitted {|results| results.length == @resources.length}
  when :filter
      collect_permitted {|results| results.length > 0}
  else
    collect_permitted {|results| results.length > 0}
  end
end