Skip to content

Commit e76f9f9

Browse files
committed
wip: add UBX authorization handler
1 parent aca55bd commit e76f9f9

32 files changed

Lines changed: 1306 additions & 18 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
module Omniauth
6+
class ConfirmOmniauthAuthorization < Decidim::Verifications::ConfirmUserAuthorization
7+
def call
8+
return broadcast(:invalid) unless form.valid?
9+
10+
if confirmation_successful?
11+
authorization.attributes = {
12+
unique_id: form.unique_id,
13+
encrypted_metadata: Decidim::MetadataEncryptor.new(
14+
uid: form.unique_id
15+
).encrypt(form.metadata.reject { |k, _v| k == :nickname })
16+
}
17+
18+
authorization.grant!
19+
broadcast(:ok)
20+
else
21+
broadcast(:invalid)
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
module Admin
6+
class VerificationsController < Decidim::Admin::ApplicationController
7+
def destroy_before_date
8+
enforce_permission_to :destroy, :authorization
9+
return unless params.has_key?(:revocations_before_date)
10+
11+
form = RevocationsBeforeDateForm.from_params(params[:revocations_before_date])
12+
RevokeByConditionAuthorizations.call(current_organization, current_user, form) do
13+
on(:ok) do
14+
flash[:notice] = t("authorization_revocation.destroy_ok", scope: "decidim.admin.menu")
15+
redirect_to decidim_admin.authorization_workflows_url
16+
end
17+
on(:invalid) do
18+
flash.now[:alert] = t("authorization_revocation.destroy_nok", scope: "decidim.admin.menu")
19+
redirect_to decidim_admin.authorization_workflows_url
20+
end
21+
end
22+
end
23+
24+
def destroy_all
25+
enforce_permission_to :destroy, :authorization
26+
RevokeAllAuthorizations.call(current_organization, current_user) do
27+
on(:ok) do
28+
flash[:notice] = t("authorization_revocation.destroy_ok", scope: "decidim.admin.menu")
29+
redirect_to decidim_admin.authorization_workflows_url
30+
end
31+
on(:invalid) do
32+
flash.now[:alert] = t("authorization_revocation.destroy_nok", scope: "decidim.admin.menu")
33+
redirect_to decidim_admin.authorization_workflows_url
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end
40+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
class ApplicationController < Decidim::ApplicationController
6+
include NeedsPermission
7+
8+
before_action :confirmed_user, only: [:new, :create]
9+
10+
def new
11+
raise NotImplementedError
12+
end
13+
14+
def create
15+
raise NotImplementedError
16+
end
17+
18+
private
19+
20+
def confirmed_user
21+
return true if !current_user || current_user && current_user.verifiable?
22+
23+
redirect_back(
24+
fallback_location: root_path,
25+
alert: t(
26+
"authorizations.create.unconfirmed",
27+
scope: "decidim.verifications"
28+
)
29+
) && (return false)
30+
end
31+
end
32+
end
33+
end
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
# This controller allows users to create and destroy their authorizations. It
6+
# shouldn't be necessary to expand it to add new authorization schemes.
7+
class AuthorizationsController < ApplicationController
8+
helper_method :handler, :unauthorized_methods, :disabled_methods, :authorization_anti_affinity
9+
before_action :valid_handler, only: [:new, :create]
10+
11+
include Decidim::UserProfile
12+
helper Decidim::DecidimFormHelper
13+
helper Decidim::CtaButtonHelper
14+
helper Decidim::AuthorizationFormHelper
15+
helper MetadataHelper
16+
17+
layout "layouts/decidim/user_profile", only: [:index]
18+
19+
def new; end
20+
21+
def index
22+
@granted_authorizations = granted_authorizations
23+
@pending_authorizations = pending_authorizations
24+
end
25+
26+
def first_login
27+
if unauthorized_methods.length == 1
28+
redirect_to(
29+
action: :new,
30+
handler: unauthorized_methods.first.name,
31+
redirect_url: decidim.account_path
32+
)
33+
end
34+
end
35+
36+
def create
37+
AuthorizeUser.call(handler) do
38+
on(:ok) do
39+
flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications")
40+
redirect_to redirect_url || authorizations_path
41+
end
42+
43+
on(:invalid) do
44+
flash[:alert] = t("authorizations.create.error", scope: "decidim.verifications")
45+
render action: :new
46+
end
47+
end
48+
end
49+
50+
protected
51+
52+
def handler
53+
@handler ||= Decidim::AuthorizationHandler.handler_for(handler_name, handler_params)
54+
end
55+
56+
def handler_params
57+
(params[:authorization_handler] || {}).merge(user: current_user)
58+
end
59+
60+
def handler_name
61+
params[:handler] || params.dig(:authorization_handler, :handler_name)
62+
end
63+
64+
def valid_handler
65+
return true if handler
66+
67+
logger.warn "Invalid authorization handler given: #{handler_name} doesn't"\
68+
"exist or you haven't added it to `Decidim.authorization_handlers`"
69+
70+
redirect_to(authorizations_path) && (return false)
71+
end
72+
73+
def unauthorized_methods
74+
@unauthorized_methods ||= available_verification_workflows.reject do |handler|
75+
(active_authorization_methods + authorization_anti_affinity).include?(handler.key)
76+
end
77+
end
78+
79+
def disabled_methods
80+
@disabled_methods ||= available_verification_workflows.select do |handler|
81+
authorization_anti_affinity.include?(handler.key)
82+
end
83+
end
84+
85+
def authorization_anti_affinity
86+
@authorization_anti_affinity ||= active_authorization_methods.map do |handler|
87+
Decidim::Verifications.find_workflow_manifest(handler).anti_affinity
88+
end.flatten.compact
89+
end
90+
91+
def active_authorization_methods
92+
Authorizations.new(organization: current_organization, user: current_user).pluck(:name)
93+
end
94+
95+
def granted_authorizations
96+
Authorizations.new(organization: current_organization, user: current_user, granted: true)
97+
end
98+
99+
def pending_authorizations
100+
Authorizations.new(organization: current_organization, user: current_user, granted: false)
101+
end
102+
103+
def store_current_location
104+
return if redirect_url.blank? || !request.format.html?
105+
106+
store_location_for(:user, redirect_url)
107+
end
108+
end
109+
end
110+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
module CsvCensus
6+
module Admin
7+
class CensusController < Decidim::Admin::ApplicationController
8+
layout "decidim/admin/users"
9+
10+
before_action :show_instructions,
11+
unless: :csv_census_active?
12+
13+
def index
14+
enforce_permission_to :index, :authorization
15+
@form = form(CensusDataForm).instance
16+
@status = Status.new(current_organization)
17+
end
18+
19+
def create
20+
enforce_permission_to :create, :authorization
21+
@form = form(CensusDataForm).from_params(params)
22+
CreateCensusData.call(@form, current_organization) do
23+
on(:ok) do
24+
flash[:notice] = t(".success", count: @form.data.values.count, errors: @form.data.errors.count)
25+
end
26+
27+
on(:invalid) do
28+
flash[:alert] = t(".error")
29+
end
30+
end
31+
redirect_to census_path
32+
end
33+
34+
def destroy_all
35+
enforce_permission_to :destroy, :authorization
36+
CsvDatum.clear(current_organization)
37+
38+
redirect_to census_path, notice: t(".success")
39+
end
40+
41+
private
42+
43+
def show_instructions
44+
enforce_permission_to :index, :authorization
45+
render :instructions
46+
end
47+
48+
def csv_census_active?
49+
current_organization.available_authorizations.include?("csv_census")
50+
end
51+
end
52+
end
53+
end
54+
end
55+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
module CsvCensus
6+
class AuthorizationsController < Decidim::ApplicationController
7+
helper_method :authorization
8+
9+
before_action :load_authorization
10+
11+
def new
12+
@form = CensusForm.from_params(user: current_user)
13+
ConfirmCensusAuthorization.call(@authorization, @form) do
14+
on(:ok) do
15+
flash[:notice] = t("authorizations.new.success", scope: "decidim.verifications.csv_census")
16+
end
17+
on(:invalid) do
18+
flash[:alert] = t("authorizations.new.error", scope: "decidim.verifications.csv_census")
19+
end
20+
redirect_to decidim_verifications.authorizations_path
21+
end
22+
end
23+
24+
private
25+
26+
def authorization
27+
@authorization ||= AuthorizationPresenter.new(@authorization)
28+
end
29+
30+
def load_authorization
31+
@authorization = Decidim::Authorization.find_or_initialize_by(
32+
user: current_user,
33+
name: "csv_census"
34+
)
35+
end
36+
end
37+
end
38+
end
39+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Verifications
5+
module IdDocuments
6+
module Admin
7+
#
8+
# Handles the configuration for the ID documents verification
9+
#
10+
class ConfigController < Decidim::Admin::ApplicationController
11+
layout "decidim/admin/users"
12+
13+
def edit
14+
enforce_permission_to :update, :organization, organization: current_organization
15+
16+
@form = form(ConfigForm).from_model(current_organization)
17+
end
18+
19+
def update
20+
enforce_permission_to :update, :organization, organization: current_organization
21+
22+
@form = form(ConfigForm).from_params(params)
23+
24+
UpdateConfig.call(@form) do
25+
on(:ok) do
26+
flash[:notice] = t("config.update.success", scope: "decidim.verifications.id_documents.admin")
27+
redirect_to pending_authorizations_path
28+
end
29+
30+
on(:invalid) do
31+
flash.now[:alert] = t("config.update.error", scope: "decidim.verifications.id_documents.admin")
32+
render action: :edit
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)