Class: Decidim::AuthorizationHandler
- Inherits:
-
Form
- Object
- Decidim::AttributeObject::Form
- Form
- Decidim::AuthorizationHandler
- Defined in:
- decidim-verifications/app/services/decidim/authorization_handler.rb
Overview
This is the base class for authorization handlers, all implementations should inherit from it. Each AuthorizationHandler is a form that will be used to check if the authorization is valid or not. When it is valid a new authorization will be created for the user.
Feel free to use validations to assert fields against a remote API, local database, or whatever.
It also sets two default attributes, ‘user` and `handler_name`.
Direct Known Subclasses
AnotherDummyAuthorizationHandler, Verifications::CsvCensus::CensusForm, Verifications::IdDocuments::InformationForm, Verifications::PostalLetter::AddressForm, Verifications::PostalLetter::ConfirmationForm, Verifications::PostalLetter::PostageForm, Verifications::Sms::ConfirmationForm, Verifications::Sms::MobilePhoneForm, DummyAuthorizationHandler
Constant Summary
Constants included from Decidim::AttributeObject::TypeMap
Decidim::AttributeObject::TypeMap::Boolean, Decidim::AttributeObject::TypeMap::Decimal
Instance Attribute Summary
Attributes inherited from Decidim::AttributeObject::Form
Class Method Summary collapse
-
.handler_for(name, params = {}) ⇒ Object
Finds a handler class from a String.
-
.handler_name ⇒ Object
A serialized version of the handler’s name.
Instance Method Summary collapse
- #authorization_attributes ⇒ Object
-
#duplicate ⇒ Decidim::Authorization?
Fetches the duplicate record of the same authorization currently belonging to other user than the user being authorized.
-
#form_attributes ⇒ Object
The attributes of the handler that should be exposed as form input when rendering the handler in a form.
-
#handler_name ⇒ Object
Same as the class method but accessible from the instance.
-
#metadata ⇒ Object
Any data that the developer would like to inject to the ‘metadata` field of an authorization when it is created.
-
#to_partial_path ⇒ Object
The String partial path so Rails can render the handler as a form.
-
#transferrable? ⇒ Boolean
Defines whether the duplicate authorizations can be transferred to a new user.
-
#unique? ⇒ Boolean
Defines whether the authorization is unique or if there is a duplicate for this particular authorization that matches the same unique_id.
-
#unique_id ⇒ Object
A unique ID to be implemented by the authorization handler that ensures no duplicates are created.
-
#verification_attachment ⇒ Object
An optional attachment to help out with verification.
-
#verification_metadata ⇒ Object
Any data to be injected in the ‘verification_metadata` field of an authorization when it is created.
Methods inherited from Decidim::AttributeObject::Form
ensure_hash, from_model, from_params, hash_from, infer_model_name, #map_model, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context
Methods included from Decidim::AttributeObject::Model
#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h
Class Method Details
.handler_for(name, params = {}) ⇒ Object
Finds a handler class from a String. This is necessary when processing the form data. It will only look for valid handlers that have also been configured in ‘Decidim.authorization_handlers`.
name - The String name of the class to find, usually in the same shape as the one returned by ‘handler_name`. params - An optional Hash with params to initialize the handler.
Returns an AuthorizationHandler descendant. Returns nil when no handlers could be found.
149 150 151 152 153 154 155 156 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 149 def self.handler_for(name, params = {}) return unless name manifest = Decidim..find { |m| m.name == name } return unless manifest manifest.form.constantize.from_params(params || {}) end |
.handler_name ⇒ Object
A serialized version of the handler’s name.
Returns a String.
128 129 130 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 128 def self.handler_name name.demodulize.underscore end |
Instance Method Details
#authorization_attributes ⇒ Object
87 88 89 90 91 92 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 87 def { unique_id:, metadata: } end |
#duplicate ⇒ Decidim::Authorization?
Fetches the duplicate record of the same authorization currently belonging to other user than the user being authorized.
55 56 57 58 59 60 61 62 63 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 55 def duplicate return unless user Authorization.find_by( user: User.where.not(id: user.id).where(organization: user.organization), name: handler_name, unique_id: ) end |
#form_attributes ⇒ Object
The attributes of the handler that should be exposed as form input when rendering the handler in a form.
Returns an Array of Strings.
69 70 71 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 69 def form_attributes attributes.except("id", "user").keys end |
#handler_name ⇒ Object
Same as the class method but accessible from the instance.
Returns a String.
135 136 137 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 135 def handler_name self.class.handler_name end |
#metadata ⇒ Object
Any data that the developer would like to inject to the ‘metadata` field of an authorization when it is created. Can be useful if some of the params the user sent with the authorization form want to be persisted for future use.
As a convention, an ‘extras’ key can be used to store information not directly related with authorization. Thus, when rendering previous verification data, on renewal, ‘extras’ is not rendered to the user.
Returns a Hash.
104 105 106 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 104 def {} end |
#to_partial_path ⇒ Object
The String partial path so Rails can render the handler as a form. This is useful if you want to have a custom view to render the form instead of the default view.
Example:
A handler named Decidim::CensusHandler would look for its partial in:
decidim/census/form
Returns a String.
83 84 85 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 83 def to_partial_path "#{handler_name.sub!(/_handler$/, "")}/form" end |
#transferrable? ⇒ Boolean
Defines whether the duplicate authorizations can be transferred to a new user.
46 47 48 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 46 def transferrable? duplicate.present? && duplicate.user.deleted? end |
#unique? ⇒ Boolean
Defines whether the authorization is unique or if there is a duplicate for this particular authorization that matches the same unique_id.
37 38 39 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 37 def unique? unique_id.nil? || duplicate.blank? end |
#unique_id ⇒ Object
A unique ID to be implemented by the authorization handler that ensures no duplicates are created. This uniqueness check will be skipped if unique_id returns nil.
28 29 30 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 28 def unique_id nil end |
#verification_attachment ⇒ Object
An optional attachment to help out with verification.
121 122 123 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 121 def nil end |
#verification_metadata ⇒ Object
Any data to be injected in the ‘verification_metadata` field of an authorization when it is created. This data will be used for multi-step verification workflows in order to confirm the authorization.
Returns a Hash.
114 115 116 |
# File 'decidim-verifications/app/services/decidim/authorization_handler.rb', line 114 def {} end |