Class: Wallaby::ModelAuthorizer
- Inherits:
-
Object
- Object
- Wallaby::ModelAuthorizer
- 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.
Class Attribute Summary collapse
-
.provider_name ⇒ String, Symbol
Provider name of the authorization framework used.
Instance Attribute Summary collapse
- #context ⇒ ActionController::Base, ... readonly
- #model_class ⇒ Class readonly
- #options ⇒ Hash readonly
-
#provider ⇒ ModelAuthorizationProvider
readonly
The instance that does the job.
Attributes included from Baseable::ClassMethods
Class Method Summary collapse
-
.create(model_class, context) ⇒ Object
Factory method to determine which provider and what options to use.
-
.guess_and_set_provider_from(model_class, context) ⇒ Class
Go through the provider list and find out the one is .available?.
-
.providers_of(model_class) ⇒ Object
Shortcut of Wallaby::Map.authorizer_provider_map.
Instance Method Summary collapse
-
#initialize(model_class, provider_name: nil, provider: nil, context: nil, options: {}) ⇒ ModelAuthorizer
constructor
A new instance of ModelAuthorizer.
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.
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 = @context = context @provider = provider \ || self.class.providers_of(@model_class)[provider_name].new() end |
Class Attribute Details
.provider_name ⇒ String, Symbol
Provider name of the authorization framework used. It will be inherited from its parent classes if there isn’t one for current class.
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
#context ⇒ ActionController::Base, ... (readonly)
44 45 46 |
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 44 def context @context end |
#model_class ⇒ Class (readonly)
34 35 36 |
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 34 def model_class @model_class end |
#options ⇒ Hash (readonly)
49 50 51 |
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 49 def @options end |
#provider ⇒ ModelAuthorizationProvider (readonly)
Returns the instance that does the job.
39 40 41 |
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 39 def provider @provider end |
Class Method Details
.create(model_class, context) ⇒ Object
use this method instead of #initialize to create authorizer instance
Factory method to determine which provider and what options to use.
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) = provider_class.(context) new(model_class, provider: provider_class.new(), context: context) end |
.guess_and_set_provider_from(model_class, context) ⇒ Class
Go through the provider list and find out the one is .available?
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
Shortcut of Wallaby::Map.authorizer_provider_map
63 64 65 |
# File 'lib/authorizers/wallaby/model_authorizer.rb', line 63 def self.providers_of(model_class) Map.(model_class) end |