class Decidim::Verifications::Adapter

Provides a unified interface for direct and deferred authorizations, so they can be used transparently

Attributes

manifest[R]

Public Class Methods

from_collection(collection) click to toggle source
# File lib/decidim/verifications/adapter.rb, line 27
def self.from_collection(collection)
  collection.map { |e| from_element(e) }
end
from_element(element) click to toggle source
# File lib/decidim/verifications/adapter.rb, line 31
def self.from_element(element)
  manifest = Verifications.find_workflow_manifest(element)

  raise UnregisteredVerificationManifest unless manifest

  new(manifest)
end
new(manifest) click to toggle source
# File lib/decidim/verifications/adapter.rb, line 39
def initialize(manifest)
  @manifest = manifest
end

Public Instance Methods

admin_root_path() click to toggle source

Administrational entry point for the verification engine

# File lib/decidim/verifications/adapter.rb, line 81
def admin_root_path
  raise InvalidDirectVerificationRoute.new(route: "admin_route_path") if manifest.type == "direct"

  public_send(:"decidim_admin_#{name}").send(:root_path, redirect_params)
end
authorize(authorization, options, component, resource) click to toggle source

Authorize user to perform an action using the authorization handler action authorizer. Saves the action_authorizer object with its context for subsequent methods calls.

authorization - The existing authorization record to be evaluated. Can be nil. options - A hash with options related only to the current authorization process. component - The component where the authorization is taking place. resource - The resource where the authorization is taking place. Can be nil.

Returns the result of authorization handler check. Check Decidim::Verifications::DefaultActionAuthorizer class docs.

# File lib/decidim/verifications/adapter.rb, line 98
def authorize(authorization, options, component, resource)
  @action_authorizer = @manifest.action_authorizer_class.new(authorization, options_for_authorizer_class(options), component, resource)
  @action_authorizer.authorize
end
renew_path(redirect_url: nil) click to toggle source

In the case of renewable authorizations, route to renew an authorization process.

# File lib/decidim/verifications/adapter.rb, line 70
def renew_path(redirect_url: nil)
  if manifest.type == "direct"
    decidim_verifications.renew_authorizations_path(redirect_params(handler: name, redirect_url: redirect_url))
  else
    main_engine.send(:renew_authorization_path, redirect_params(redirect_url: redirect_url))
  end
end
resume_authorization_path(redirect_url: nil) click to toggle source

In the case of deferred authorizations, route to resume an authorization process. Otherwise it rises

# File lib/decidim/verifications/adapter.rb, line 60
def resume_authorization_path(redirect_url: nil)
  raise InvalidDirectVerificationRoute.new(route: "edit_authorization_path") if manifest.type == "direct"

  main_engine.send(:edit_authorization_path, redirect_params(redirect_url: redirect_url))
end
root_path(redirect_url: nil) click to toggle source

Main entry point for the verification engine

# File lib/decidim/verifications/adapter.rb, line 48
def root_path(redirect_url: nil)
  if manifest.type == "direct"
    decidim_verifications.new_authorization_path(redirect_params(handler: name, redirect_url: redirect_url))
  else
    main_engine.send(:root_path, redirect_params(redirect_url: redirect_url))
  end
end

Private Instance Methods

attributes_required_for_authorization() click to toggle source
# File lib/decidim/verifications/adapter.rb, line 124
def attributes_required_for_authorization
  @attributes_required_for_authorization ||= manifest.options.attributes.stringify_keys.select { |_, attribute| attribute.required_for_authorization? }
end
main_engine() click to toggle source
# File lib/decidim/verifications/adapter.rb, line 107
def main_engine
  send("decidim_#{manifest.name}")
end
options_for_authorizer_class(options) click to toggle source
# File lib/decidim/verifications/adapter.rb, line 116
def options_for_authorizer_class(options)
  options = options.present? ? options.stringify_keys : {}

  attributes_required_for_authorization.inject(options) do |options_for_authorizer_class, (key, _)|
    options_for_authorizer_class.update(key => OpenStruct.new(required_for_authorization?: true, value: options[key]))
  end
end
redirect_params(params = {}) click to toggle source
# File lib/decidim/verifications/adapter.rb, line 111
def redirect_params(params = {})
  # Could add redirect params if a ActionAuthorizer object was previously set.
  params.merge(@action_authorizer&.redirect_params || {})
end