decidim-user_fields 0.0.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +308 -49
- data/Rakefile +253 -0
- data/app/forms/decidim/custom_user_fields/admin/customizations_config_form.rb +67 -0
- data/app/forms/decidim/custom_user_fields/verifications/verification_form.rb +85 -0
- data/app/overrides/decidim/devise/invitations/edit/extra_user_fields_form.html.erb.deface +2 -2
- data/app/overrides/decidim/devise/omniauth_registrations/new/extra_user_fields.html.erb.deface +3 -0
- data/app/overrides/invitation_override.rb +31 -0
- data/app/packs/entrypoints/decidim_custom_user_fields.js +0 -0
- data/app/packs/stylesheets/decidim/custom_user_fields/_decidim_custom_user_fields.scss +48 -0
- data/app/views/decidim/custom_user_fields/_profile_form.html.erb +2 -2
- data/app/views/decidim/custom_user_fields/_registration_form.html.erb +2 -2
- data/app/views/decidim/custom_user_fields/_verification_form.erb +4 -0
- data/app/views/decidim/custom_user_fields/system/_customizations_tab.html.erb +15 -0
- data/config/assets.rb +9 -0
- data/config/i18n-tasks.yml +22 -0
- data/config/locales/decidim_custom_user_fields_scenarios.en.yml +50 -0
- data/config/locales/decidim_custom_user_fields_scenarios.fr.yml +48 -0
- data/config/locales/decidim_toggle.en.yml +13 -0
- data/config/locales/decidim_toggle.fr.yml +13 -0
- data/config/locales/decidim_user_fields.en.yml +25 -0
- data/config/locales/decidim_user_fields.fr.yml +25 -0
- data/lib/decidim/custom_user_fields/custom_user_fields.rb +10 -14
- data/lib/decidim/custom_user_fields/customization_field_naming.rb +17 -0
- data/lib/decidim/custom_user_fields/customizations/builder.rb +48 -0
- data/lib/decidim/custom_user_fields/customizations/customization.rb +29 -0
- data/lib/decidim/custom_user_fields/customizations.rb +42 -0
- data/lib/decidim/custom_user_fields/decidim_integrations.rb +52 -0
- data/lib/decidim/custom_user_fields/definition_guidance.rb +109 -0
- data/lib/decidim/custom_user_fields/dev/bootstrap.rb +102 -0
- data/lib/decidim/custom_user_fields/dev/local_oidc_seeder.rb +150 -0
- data/lib/decidim/custom_user_fields/dev/omniauth_providers_patcher.rb +68 -0
- data/lib/decidim/custom_user_fields/dev/openid_connect_setup.rb +110 -0
- data/lib/decidim/custom_user_fields/dev/scenario_customizations.rb +72 -0
- data/lib/decidim/custom_user_fields/engine.rb +59 -26
- data/lib/decidim/custom_user_fields/extended_data.rb +52 -0
- data/lib/decidim/custom_user_fields/field_definition.rb +28 -6
- data/lib/decidim/custom_user_fields/fields/boolean_field.rb +51 -0
- data/lib/decidim/custom_user_fields/fields/date_field.rb +80 -10
- data/lib/decidim/custom_user_fields/fields/dummy_field.rb +40 -0
- data/lib/decidim/custom_user_fields/fields/extra_field_ref_field.rb +118 -0
- data/lib/decidim/custom_user_fields/fields/generic_field.rb +99 -6
- data/lib/decidim/custom_user_fields/fields/text_area_field.rb +34 -9
- data/lib/decidim/custom_user_fields/fields/text_field.rb +47 -6
- data/lib/decidim/custom_user_fields/i18n/customization_keys.rb +55 -0
- data/lib/decidim/custom_user_fields/i18n/customization_keys_scanner.rb +80 -0
- data/lib/decidim/custom_user_fields/i18n/tasks.rb +7 -0
- data/lib/decidim/custom_user_fields/overrides/authorizations_controller.rb +18 -0
- data/lib/decidim/custom_user_fields/overrides/command.rb +23 -21
- data/lib/decidim/custom_user_fields/overrides/confirmations_controller.rb +15 -0
- data/lib/decidim/custom_user_fields/overrides/devise_authentication_methods.rb +68 -0
- data/lib/decidim/custom_user_fields/overrides/form_definition.rb +25 -8
- data/lib/decidim/custom_user_fields/overrides/invitations_controller.rb +109 -0
- data/lib/decidim/custom_user_fields/overrides/omniauth_command.rb +40 -0
- data/lib/decidim/custom_user_fields/overrides/omniauth_registrations_controller.rb +93 -0
- data/lib/decidim/custom_user_fields/overrides/user.rb +16 -0
- data/lib/decidim/custom_user_fields/registration_fields.rb +99 -0
- data/lib/decidim/custom_user_fields/update_customizations_config_command.rb +39 -0
- data/lib/decidim/custom_user_fields/upgrade/migrate_toggle_config.rb +74 -0
- data/lib/decidim/custom_user_fields/verifications/builder.rb +100 -0
- data/lib/decidim/custom_user_fields/verifications/verifications.rb +82 -0
- data/lib/decidim/custom_user_fields/version.rb +4 -2
- data/lib/decidim/user_fields.rb +46 -13
- data/lib/tasks/decidim/custom_user_fields/custom_user_fields_upgrade.rake +16 -0
- data/lib/tasks/decidim/custom_user_fields/custom_user_fields_webpacker.rake +59 -0
- data/lib/tasks/decidim/custom_user_fields/decidim_custom_user_fields_upgrade_tasks.rake +16 -0
- data/lib/tasks/decidim/custom_user_fields/dev/local_setup.rake +16 -0
- metadata +80 -15
- data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module InvitationAcceptExtendedData
|
|
6
|
+
def accept_resource
|
|
7
|
+
custom_params = extract_custom_field_params
|
|
8
|
+
if (validation_errors = validate_custom_params(custom_params)).any?
|
|
9
|
+
return invalid_invitation_resource(validation_errors)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
resource = super
|
|
13
|
+
return resource unless resource.valid? && resource.invitation_accepted?
|
|
14
|
+
|
|
15
|
+
persist_invitation_extended_data!(resource, custom_params)
|
|
16
|
+
resource
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
protected
|
|
20
|
+
|
|
21
|
+
def configure_permitted_parameters
|
|
22
|
+
devise_parameter_sanitizer.permit(
|
|
23
|
+
:accept_invitation,
|
|
24
|
+
keys: [:nickname, :password, :password_confirmation, :tos_agreement, :newsletter_notifications]
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def extract_custom_field_params
|
|
31
|
+
return {} unless params[:user]
|
|
32
|
+
|
|
33
|
+
user_params = params[:user].to_unsafe_h.with_indifferent_access if params[:user].respond_to?(:to_unsafe_h)
|
|
34
|
+
user_params ||= params[:user].to_h.with_indifferent_access
|
|
35
|
+
custom_keys = RegistrationFields.all_registration_fields.map { |field| field.name.to_s }
|
|
36
|
+
extracted = user_params.slice(*custom_keys)
|
|
37
|
+
custom_keys.each do |key|
|
|
38
|
+
params[:user].delete(key) if params[:user].respond_to?(:delete)
|
|
39
|
+
params[:user].delete(key.to_sym) if params[:user].respond_to?(:delete)
|
|
40
|
+
end
|
|
41
|
+
extracted
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validate_custom_params(custom_params)
|
|
45
|
+
organization = invitation_organization
|
|
46
|
+
return ActiveModel::Errors.new(Object.new) if custom_params.blank? || organization.blank?
|
|
47
|
+
|
|
48
|
+
ExtendedData.validate_params(organization, custom_params)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def invalid_invitation_resource(errors)
|
|
52
|
+
resource = resource_class.find_by_invitation_token(invitation_token, true)
|
|
53
|
+
errors.each { |error| resource.errors.add(error.attribute, error.message) }
|
|
54
|
+
assign_invitation_form_with_errors!(resource, errors)
|
|
55
|
+
resource
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def invitation_token
|
|
59
|
+
params.dig(:user, :invitation_token) || params[:invitation_token]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def invitation_organization
|
|
63
|
+
resource_class.find_by_invitation_token(invitation_token, true)&.organization
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def persist_invitation_extended_data!(resource, custom_params)
|
|
67
|
+
success, errors = ExtendedData.merge_into(resource, resource.organization, custom_params)
|
|
68
|
+
return if success
|
|
69
|
+
|
|
70
|
+
errors.each do |error|
|
|
71
|
+
resource.errors.add(error.attribute, error.message)
|
|
72
|
+
end
|
|
73
|
+
assign_invitation_form_with_errors!(resource, errors)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def assign_invitation_form_with_errors!(resource, errors)
|
|
77
|
+
@form = invitation_registration_form(resource)
|
|
78
|
+
merge_invitation_params_into_form!(@form)
|
|
79
|
+
copy_errors_to_form!(@form, errors)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def invitation_registration_form(resource)
|
|
83
|
+
Decidim::RegistrationForm.from_model(resource).with_context(
|
|
84
|
+
current_organization: invitation_organization,
|
|
85
|
+
current_user: try(:current_user),
|
|
86
|
+
invitation_token:
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def merge_invitation_params_into_form!(form)
|
|
91
|
+
return unless params[:user]
|
|
92
|
+
|
|
93
|
+
user_params = params[:user].to_unsafe_h.with_indifferent_access if params[:user].respond_to?(:to_unsafe_h)
|
|
94
|
+
user_params ||= params[:user].to_h.with_indifferent_access
|
|
95
|
+
|
|
96
|
+
user_params.each do |key, value|
|
|
97
|
+
setter = :"#{key}="
|
|
98
|
+
form.public_send(setter, value) if form.respond_to?(setter)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def copy_errors_to_form!(form, errors)
|
|
103
|
+
errors.each do |error|
|
|
104
|
+
form.errors.add(error.attribute, error.message)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/concern"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module CustomUserFields
|
|
7
|
+
module OmniauthCommand
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def create_or_find_user
|
|
13
|
+
super
|
|
14
|
+
persist_omniauth_extended_data!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def persist_omniauth_extended_data!
|
|
18
|
+
return unless @user
|
|
19
|
+
|
|
20
|
+
params = omniauth_extended_data
|
|
21
|
+
return if params.blank?
|
|
22
|
+
|
|
23
|
+
success, errors = ExtendedData.merge_into(@user, form.current_organization, params)
|
|
24
|
+
return if success
|
|
25
|
+
|
|
26
|
+
errors.each { |error| @user.errors.add(error.attribute, error.message) }
|
|
27
|
+
raise ActiveRecord::RecordInvalid, @user
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def omniauth_extended_data
|
|
31
|
+
org = form.try(:current_organization)
|
|
32
|
+
custom_data = {}
|
|
33
|
+
RegistrationFields.active_registration_fields(org).each do |field_def|
|
|
34
|
+
custom_data[field_def.name] = form[field_def.name]
|
|
35
|
+
end
|
|
36
|
+
custom_data
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/concern"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module CustomUserFields
|
|
7
|
+
module OmniauthRegistrationsController
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def verified_email
|
|
13
|
+
@verified_email ||= begin
|
|
14
|
+
email = oauth_verified_email
|
|
15
|
+
email ||= trusted_form_email_for_openid_connect
|
|
16
|
+
session[:verified_email] = email if email.present?
|
|
17
|
+
email.presence || session[:verified_email]
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def user_params_from_oauth_hash
|
|
22
|
+
return nil if oauth_data.empty?
|
|
23
|
+
|
|
24
|
+
uid = resolve_omniauth_uid
|
|
25
|
+
return nil if uid.blank?
|
|
26
|
+
|
|
27
|
+
info = oauth_info_with_indifferent_access
|
|
28
|
+
|
|
29
|
+
{
|
|
30
|
+
provider: oauth_data[:provider],
|
|
31
|
+
uid:,
|
|
32
|
+
name: info[:name],
|
|
33
|
+
nickname: info[:nickname],
|
|
34
|
+
oauth_signature: OmniauthRegistrationForm.create_signature(oauth_data[:provider], uid),
|
|
35
|
+
avatar_url: info[:image],
|
|
36
|
+
raw_data: oauth_hash
|
|
37
|
+
}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def oauth_verified_email
|
|
41
|
+
info = oauth_info_with_indifferent_access
|
|
42
|
+
email = info[:email].presence || raw_omniauth_info[:email].presence
|
|
43
|
+
return if email.blank?
|
|
44
|
+
return email if openid_connect_provider?
|
|
45
|
+
return email unless info.has_key?(:email_verified)
|
|
46
|
+
|
|
47
|
+
ActiveModel::Type::Boolean.new.cast(info[:email_verified]) ? email : nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def trusted_form_email_for_openid_connect
|
|
51
|
+
return unless Rails.env.development?
|
|
52
|
+
return unless openid_connect_registration?
|
|
53
|
+
|
|
54
|
+
@form&.email.presence || params.dig(:user, :email).presence
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def openid_connect_registration?
|
|
58
|
+
openid_connect_provider? && oauth_identity_present?
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def openid_connect_provider?
|
|
62
|
+
provider = oauth_data[:provider].presence || params.dig(:user, :provider)
|
|
63
|
+
provider.to_s == "openid_connect"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def oauth_identity_present?
|
|
67
|
+
resolve_omniauth_uid.present?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def resolve_omniauth_uid
|
|
71
|
+
uid = oauth_data[:uid]
|
|
72
|
+
return uid.to_s if uid.present?
|
|
73
|
+
|
|
74
|
+
raw = raw_omniauth_info
|
|
75
|
+
raw[:sub].presence || raw[:id].presence || raw[:uid].presence || raw[:nickname].presence ||
|
|
76
|
+
oauth_info_with_indifferent_access[:nickname].presence ||
|
|
77
|
+
oauth_info_with_indifferent_access[:email].presence
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def oauth_info_with_indifferent_access
|
|
81
|
+
info = oauth_data[:info] || {}
|
|
82
|
+
info = info.with_indifferent_access if info.respond_to?(:with_indifferent_access)
|
|
83
|
+
info
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def raw_omniauth_info
|
|
87
|
+
raw = oauth_hash.dig(:extra, :raw_info) || {}
|
|
88
|
+
raw = raw.with_indifferent_access if raw.respond_to?(:with_indifferent_access)
|
|
89
|
+
raw
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/concern"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module CustomUserFields
|
|
7
|
+
module UserExtendedDataGuard
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
# Make User::exphemeral nil-safe, required after bringing ephemeral users into core (see https://github.com/decidim/decidim/pull/13579)
|
|
11
|
+
def ephemeral?
|
|
12
|
+
(extended_data || {})["ephemeral"]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module RegistrationFields
|
|
6
|
+
RESERVED_EXTENDED_DATA_KEYS = %w(
|
|
7
|
+
nickname
|
|
8
|
+
name
|
|
9
|
+
personal_url
|
|
10
|
+
about
|
|
11
|
+
extended_data
|
|
12
|
+
newsletter_notifications_at
|
|
13
|
+
accepted_tos_version
|
|
14
|
+
locale
|
|
15
|
+
newsletter_notifications
|
|
16
|
+
managed
|
|
17
|
+
deleted_at
|
|
18
|
+
blocked
|
|
19
|
+
confirmed_at
|
|
20
|
+
officialized_at
|
|
21
|
+
).freeze
|
|
22
|
+
|
|
23
|
+
FIRST_LOGIN_MODES = %w(prompt_authorization none).freeze
|
|
24
|
+
DEFAULT_FIRST_LOGIN_MODE = "prompt_authorization"
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def reserved_key?(name)
|
|
28
|
+
RESERVED_EXTENDED_DATA_KEYS.include?(name.to_s)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def first_login_mode(organization)
|
|
32
|
+
value = raw_toggle_config(organization)[:first_login_mode].presence
|
|
33
|
+
return DEFAULT_FIRST_LOGIN_MODE if value.blank? || FIRST_LOGIN_MODES.exclude?(value.to_s)
|
|
34
|
+
|
|
35
|
+
value.to_s
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def prompt_authorization_on_first_login?(organization)
|
|
39
|
+
first_login_mode(organization) == DEFAULT_FIRST_LOGIN_MODE
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def enabled_customization_names(organization)
|
|
43
|
+
config = raw_toggle_config(organization)
|
|
44
|
+
from_enabled_customization_key(config) || from_legacy_enabled_flags(config)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def active_registration_fields(organization)
|
|
48
|
+
enabled_customization_names(organization).flat_map do |name|
|
|
49
|
+
Customizations.find(name)&.fields || []
|
|
50
|
+
end.uniq(&:name)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def all_registration_fields
|
|
54
|
+
Customizations.all.flat_map(&:fields).uniq(&:name)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def raw_toggle_config(organization)
|
|
60
|
+
Decidim::Toggle::OrganizationModuleConfig.find_by(
|
|
61
|
+
decidim_organization_id: organization.id,
|
|
62
|
+
module_name: "custom_user_fields"
|
|
63
|
+
)&.config&.with_indifferent_access || {}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def from_enabled_customization_key(config)
|
|
67
|
+
return unless config.has_key?(:enabled_customization)
|
|
68
|
+
|
|
69
|
+
name = config[:enabled_customization].to_s
|
|
70
|
+
Customizations.find(name) ? [name] : []
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def from_legacy_enabled_flags(config)
|
|
74
|
+
enabled = enabled_from_customization_flags(config)
|
|
75
|
+
return enabled.first(1) if enabled.any?
|
|
76
|
+
return [] if legacy_config_superseded?(config)
|
|
77
|
+
|
|
78
|
+
Array(config[:active_field_set].presence&.to_s)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def customization_enabled?(config, name)
|
|
82
|
+
ActiveModel::Type::Boolean.new.cast(
|
|
83
|
+
config[:"#{name}_enabled"] || config["#{name}_enabled"]
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def enabled_from_customization_flags(config)
|
|
88
|
+
Customizations.all.filter_map do |customization|
|
|
89
|
+
customization.name.to_s if customization_enabled?(config, customization.name)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def legacy_config_superseded?(config)
|
|
94
|
+
config.keys.any? { |key| key.to_s.end_with?("_enabled") }
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
class UpdateCustomizationsConfigCommand < Decidim::Command
|
|
6
|
+
def initialize(organization, form)
|
|
7
|
+
@organization = organization
|
|
8
|
+
@form = form
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
return broadcast(:invalid) if form.class.module_config_name.blank?
|
|
13
|
+
return broadcast(:invalid) if form.invalid?
|
|
14
|
+
|
|
15
|
+
persist_and_prune!
|
|
16
|
+
broadcast(:ok)
|
|
17
|
+
rescue ActiveRecord::RecordInvalid
|
|
18
|
+
broadcast(:invalid)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
attr_reader :organization, :form
|
|
24
|
+
|
|
25
|
+
def persist_and_prune!
|
|
26
|
+
Decidim::Toggle.save_config!(organization, form.class.module_config_name, form.to_h)
|
|
27
|
+
prune_disabled_authorization_handlers!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def prune_disabled_authorization_handlers!
|
|
31
|
+
current = organization.read_attribute(:available_authorizations)
|
|
32
|
+
pruned = Verifications.prune_disabled_handlers(current, organization)
|
|
33
|
+
return if pruned == current
|
|
34
|
+
|
|
35
|
+
organization.update!(available_authorizations: pruned)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Upgrade
|
|
6
|
+
# One-time migration: decidim-toggle config `active_field_set` → `enabled_customization`.
|
|
7
|
+
class MigrateToggleConfig
|
|
8
|
+
MODULE_NAME = "custom_user_fields"
|
|
9
|
+
LEGACY_KEY = "active_field_set"
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def run(dry_run: false)
|
|
13
|
+
new(dry_run:).run
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(dry_run: false)
|
|
18
|
+
@dry_run = dry_run
|
|
19
|
+
@migrated = 0
|
|
20
|
+
@skipped = 0
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def run
|
|
24
|
+
scope.find_each { |record| migrate_record!(record) }
|
|
25
|
+
summary
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
attr_reader :dry_run, :migrated, :skipped
|
|
31
|
+
|
|
32
|
+
def summary
|
|
33
|
+
{ migrated:, skipped: }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def scope
|
|
37
|
+
Decidim::Toggle::OrganizationModuleConfig.where(module_name: MODULE_NAME)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def migrate_record!(record)
|
|
41
|
+
config = (record.config || {}).with_indifferent_access
|
|
42
|
+
legacy_name = config[LEGACY_KEY]
|
|
43
|
+
return increment_skipped if legacy_name.blank?
|
|
44
|
+
|
|
45
|
+
persist_migrated!(record, config, legacy_name)
|
|
46
|
+
@migrated += 1
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def increment_skipped
|
|
50
|
+
@skipped += 1
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def persist_migrated!(record, config, legacy_name)
|
|
54
|
+
next_config = config.except(LEGACY_KEY)
|
|
55
|
+
next_config[:enabled_customization] ||= legacy_name.to_s
|
|
56
|
+
return log(record, legacy_name) if dry_run
|
|
57
|
+
|
|
58
|
+
record.update!(config: stringify_config(next_config))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def stringify_config(config)
|
|
62
|
+
config.to_h.stringify_keys
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def log(record, legacy_name)
|
|
66
|
+
Rails.logger.debug do
|
|
67
|
+
"[dry-run] org #{record.decidim_organization_id}: " \
|
|
68
|
+
"#{LEGACY_KEY}=#{legacy_name} → enabled_customization=#{legacy_name}"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Verifications
|
|
6
|
+
class Builder
|
|
7
|
+
include DefinitionMissing
|
|
8
|
+
|
|
9
|
+
DSL_METHODS = [
|
|
10
|
+
:add_field,
|
|
11
|
+
:renewable!,
|
|
12
|
+
:renewable?,
|
|
13
|
+
:ephemerable!,
|
|
14
|
+
:ephemerable?,
|
|
15
|
+
:register_workflow!
|
|
16
|
+
].freeze
|
|
17
|
+
|
|
18
|
+
attr_reader :name, :customization
|
|
19
|
+
attr_accessor :fields, :renewable, :time_between_renewals
|
|
20
|
+
|
|
21
|
+
def initialize(name, customization: nil)
|
|
22
|
+
@name = name.to_s
|
|
23
|
+
@customization = customization&.to_sym
|
|
24
|
+
@fields = []
|
|
25
|
+
@ephemerable = false
|
|
26
|
+
@renewable = false
|
|
27
|
+
@time_between_renewals = nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def renewable!(time_between_renewals = 1.day)
|
|
31
|
+
@renewable = true
|
|
32
|
+
@time_between_renewals = time_between_renewals
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def renewable?
|
|
36
|
+
@renewable
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def add_field(field_name, field_definition)
|
|
40
|
+
field_def = FieldDefinition.new(field_name, field_definition, handler_name)
|
|
41
|
+
field_def.field.customization_name = @customization if field_def.type == :extra_field_ref
|
|
42
|
+
fields.push(field_def)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def ephemerable!
|
|
46
|
+
@ephemerable = true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def ephemerable?
|
|
50
|
+
@ephemerable
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def handler_name
|
|
54
|
+
klass_name.underscore
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def klass_name
|
|
58
|
+
name.camelize.to_s
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def register_workflow!
|
|
62
|
+
validate_extra_field_ref_requirements!
|
|
63
|
+
return if workflow_already_registered?
|
|
64
|
+
|
|
65
|
+
Decidim::Verifications.register_workflow(handler_name.to_sym) do |workflow|
|
|
66
|
+
workflow.form = "Decidim::CustomUserFields::Verifications::#{klass_name}"
|
|
67
|
+
klass = Decidim::CustomUserFields::Verifications.create_verification_class(klass_name)
|
|
68
|
+
workflow.ephemerable = ephemerable? if workflow.respond_to?(:ephemerable=)
|
|
69
|
+
workflow.renewable = renewable? if workflow.respond_to?(:renewable=)
|
|
70
|
+
workflow.time_between_renewals = time_between_renewals unless time_between_renewals.nil?
|
|
71
|
+
workflow.metadata_cell = "decidim/verifications/authorization_metadata"
|
|
72
|
+
|
|
73
|
+
klass.decidim_custom_fields = fields
|
|
74
|
+
fields.map do |field|
|
|
75
|
+
field.configure_form(klass)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def workflow_already_registered?
|
|
83
|
+
Decidim::Verifications.find_workflow_manifest(handler_name).present?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def validate_extra_field_ref_requirements!
|
|
87
|
+
uses_extra_field_ref = fields.any? { |field| field.type == :extra_field_ref }
|
|
88
|
+
if uses_extra_field_ref && customization.nil?
|
|
89
|
+
raise Decidim::CustomUserFields::Error,
|
|
90
|
+
"authorization #{name} must be registered inside a customization when using extra_field_ref fields"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
fields.each do |field|
|
|
94
|
+
field.field.validate_customization_reference! if field.type == :extra_field_ref
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Verifications
|
|
6
|
+
include ActiveSupport::Configurable
|
|
7
|
+
|
|
8
|
+
def self.verifications
|
|
9
|
+
@verifications ||= []
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def verification_classes
|
|
14
|
+
@verification_classes ||= []
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def workflow_customizations
|
|
18
|
+
@workflow_customizations ||= {}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def workflow_customization(handler_name)
|
|
22
|
+
workflow_customizations[handler_name.to_s]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def workflow_handlers_for(customization_name)
|
|
26
|
+
workflow_customizations.select { |_, name| name.to_s == customization_name.to_s }.keys
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def selectable_workflows(organization)
|
|
30
|
+
enabled = enabled_customization_names(organization)
|
|
31
|
+
Decidim.authorization_workflows.select { |workflow| selectable_workflow?(workflow, enabled) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def enabled_customization_names(organization)
|
|
35
|
+
return [] unless organization
|
|
36
|
+
|
|
37
|
+
RegistrationFields.enabled_customization_names(organization)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def selectable_workflow?(workflow, enabled_names)
|
|
41
|
+
customization = workflow_customization(workflow.name)
|
|
42
|
+
customization.blank? || enabled_names.include?(customization.to_s)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def disabled_handler_names(organization)
|
|
46
|
+
enabled = enabled_customization_names(organization)
|
|
47
|
+
workflow_customizations.filter_map do |handler, name|
|
|
48
|
+
handler.to_s if enabled.exclude?(name.to_s)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def prune_disabled_handlers(raw, organization)
|
|
53
|
+
prune_raw_authorizations(raw, disabled_handler_names(organization))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def prune_raw_authorizations(raw, disabled)
|
|
57
|
+
case raw
|
|
58
|
+
when Hash then raw.reject { |name, _options| disabled.include?(name.to_s) }
|
|
59
|
+
else Array(raw).map(&:to_s) - disabled
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def create_verification_class(class_name)
|
|
64
|
+
klass = Class.new(Decidim::CustomUserFields::Verifications::VerificationForm)
|
|
65
|
+
const_set(class_name, klass)
|
|
66
|
+
verification_classes.push klass
|
|
67
|
+
klass
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.register(verification_name, customization: nil, &)
|
|
72
|
+
builder = Decidim::CustomUserFields::Verifications::Builder.new(verification_name, customization:)
|
|
73
|
+
yield builder
|
|
74
|
+
builder.register_workflow!
|
|
75
|
+
if customization
|
|
76
|
+
workflow_customizations[builder.handler_name] = customization.to_sym
|
|
77
|
+
Customizations.find(customization)&.register_handler!(builder.handler_name)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|