Class: Wallaby::ModelAuthorizer

Inherits:
Object
  • Object
show all
Extended by:
Baseable::ClassMethods
Defined in:
lib/authorizers/wallaby/model_authorizer.rb

Overview

This is the base authorizer class to provider authorization for given/associated model.

For best practice, please create an application authorizer class (see example) to better control the functions shared between different model authorizers.

Examples:

Create an application class for Admin Interface usage

class Admin::ApplicationAuthorizer < Wallaby::ModelAuthorizer
  base_class!
end

Since:

  • wallaby-5.2.0

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Baseable::ClassMethods

#base_class

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Baseable::ClassMethods

base_class!, base_class?, namespace, namespace=

Constructor Details

#initialize(model_class, provider_name: nil, provider: nil, context: nil, options: {}) ⇒ ModelAuthorizer

Returns a new instance of ModelAuthorizer.

Parameters:

  • model_class (Class)
  • provider_name (String) (defaults to: nil)
  • provider (Wallaby::ModelAuthorizationProvider) (defaults to: nil)
  • context (ActionController::Base, ActionView::Base) (defaults to: nil)
  • options (Hash) (defaults to: {})

Since:

  • wallaby-5.2.0



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 72

def initialize(
  model_class,
  provider_name: nil,
  provider: nil,
  context: nil,
  options: {}
)
  @model_class = model_class || self.class.model_class
  @options = options
  @context = context
  @provider = provider \
    || self.class.providers_of(@model_class)[provider_name].new(options)
end

Class Attribute Details

.provider_nameString, Symbol

Provider name of the authorization framework used. It will be inherited from its parent classes if there isn’t one for current class.

Returns:

  • (String, Symbol)

Since:

  • wallaby-5.2.0



25
26
27
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 25

def provider_name
  @provider_name || superclass.try(:provider_name)
end

Instance Attribute Details

#contextActionController::Base, ... (readonly)

Returns:

  • (ActionController::Base, ActionView::Base, nil)

Since:

  • 0.2.2



44
45
46
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 44

def context
  @context
end

#model_classClass (readonly)

Returns:

  • (Class)

Since:

  • wallaby-5.2.0



34
35
36
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 34

def model_class
  @model_class
end

#optionsHash (readonly)

Returns:

  • (Hash)

Since:

  • 0.2.2



49
50
51
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 49

def options
  @options
end

#providerModelAuthorizationProvider (readonly)

Returns the instance that does the job.

Returns:

Since:

  • wallaby-5.2.0



39
40
41
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 39

def provider
  @provider
end

Class Method Details

.create(model_class, context) ⇒ Object

Note:

use this method instead of #initialize to create authorizer instance

Factory method to determine which provider and what options to use.

Parameters:

  • model_class (Class)
  • context (ActionController::Base, ActionView::Base)

Since:

  • wallaby-5.2.0



55
56
57
58
59
60
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 55

def self.create(model_class, context)
  model_class ||= self.model_class
  provider_class = guess_and_set_provider_from(model_class, context)
  options = provider_class.options_from(context)
  new(model_class, provider: provider_class.new(options), context: context)
end

.guess_and_set_provider_from(model_class, context) ⇒ Class

Go through the provider list and find out the one is .available?

Parameters:

  • model_class (Class)
  • context (ActionController::Base, ActionView::Base)

Returns:

  • (Class)

    provider class

Since:

  • wallaby-5.2.0



91
92
93
94
95
96
97
98
99
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 91

def self.guess_and_set_provider_from(model_class, context)
  providers = providers_of(model_class)
  provider_class =
    providers[provider_name] \
      || providers.values.find { |klass| klass.available? context } \
      || providers[:default] # fallback to default
  self.provider_name ||= provider_class.provider_name
  provider_class
end

.providers_of(model_class) ⇒ Object

Since:

  • wallaby-5.2.0



63
64
65
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 63

def self.providers_of(model_class)
  Map.authorizer_provider_map(model_class)
end