Class: Decidim::Verifications::Adapter

Inherits:
Object
  • Object
show all
Defined in:
decidim-verifications/lib/decidim/verifications/adapter.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest) ⇒ Adapter

Returns a new instance of Adapter.



62
63
64
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 62

def initialize(manifest)
  @manifest = manifest
end

Class Method Details

.from_collection(collection) ⇒ Object



50
51
52
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 50

def self.from_collection(collection)
  collection.map { |e| from_element(e) }
end

.from_element(element) ⇒ Object



54
55
56
57
58
59
60
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 54

def self.from_element(element)
  manifest = Verifications.find_workflow_manifest(element)

  raise UnregisteredVerificationManifest unless manifest

  new(manifest)
end

Instance Method Details

#admin_root_pathObject

Administrational entry point for the verification engine



107
108
109
110
111
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 107

def admin_root_path
  raise InvalidVerificationRoute.new(route: "admin_route_path") if manifest.type == "direct"

  admin_engine.send(:root_path, redirect_params)
end

#authorize(authorization, options, component, resource) ⇒ Object

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.



124
125
126
127
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 124

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) ⇒ Object

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



94
95
96
97
98
99
100
101
102
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 94

def renew_path(redirect_url: nil)
  if manifest.type == "direct"
    decidim_verifications.renew_authorizations_path(redirect_params(handler: name, redirect_url:))
  else
    raise MissingVerificationRoute.new(handler: name, route: "renew_authorization_path", action: "renew") unless main_engine.respond_to?(:renew_authorization_path)

    main_engine.send(:renew_authorization_path, redirect_params(redirect_url:))
  end
end

#resume_authorization_path(redirect_url: nil) ⇒ Object

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



83
84
85
86
87
88
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 83

def resume_authorization_path(redirect_url: nil)
  raise InvalidVerificationRoute.new(route: "edit_authorization_path") if manifest.type == "direct"
  raise MissingVerificationRoute.new(handler: name, route: "edit_authorization_path", action: "resume") unless main_engine.respond_to?(:edit_authorization_path)

  main_engine.send(:edit_authorization_path, redirect_params(redirect_url:))
end

#root_path(redirect_url: nil) ⇒ Object

Main entry point for the verification engine



71
72
73
74
75
76
77
# File 'decidim-verifications/lib/decidim/verifications/adapter.rb', line 71

def root_path(redirect_url: nil)
  if manifest.type == "direct"
    decidim_verifications.new_authorization_path(redirect_params(handler: name, redirect_url:))
  else
    main_engine.send(:root_path, redirect_params(redirect_url:))
  end
end