decidim-user_fields 0.0.1 → 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 +320 -55
- 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/admin/officializations/index/_export_users_dropdown.html.erb.deface +1 -3
- 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 +91 -22
- 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 +51 -27
- 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 +27 -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 +81 -16
- data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pathname"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module CustomUserFields
|
|
7
|
+
module Dev
|
|
8
|
+
# Idempotently writes the dummy app initializer that registers the
|
|
9
|
+
# openid_connect OmniAuth provider in Decidim.omniauth_providers.
|
|
10
|
+
class OmniauthProvidersPatcher
|
|
11
|
+
SNIPPET = <<~RUBY
|
|
12
|
+
# frozen_string_literal: true
|
|
13
|
+
|
|
14
|
+
# Local Zitadel OIDC testing: register the openid_connect provider
|
|
15
|
+
# catalogue entry. Enabled in development only, honouring
|
|
16
|
+
# OMNIAUTH_OPENID_CONNECT_ENABLED; credentials come from the
|
|
17
|
+
# organization record (see Dev::OpenidConnectSetup.provider_setup).
|
|
18
|
+
if Rails.env.development?
|
|
19
|
+
Decidim.omniauth_providers[:openid_connect] = {
|
|
20
|
+
enabled: Decidim::Env.new("OMNIAUTH_OPENID_CONNECT_ENABLED", "true").present?,
|
|
21
|
+
icon: "shield-line"
|
|
22
|
+
}
|
|
23
|
+
end
|
|
24
|
+
RUBY
|
|
25
|
+
|
|
26
|
+
def self.call(initializer_path: default_initializer_path)
|
|
27
|
+
new(initializer_path:).call
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.default_initializer_path
|
|
31
|
+
module_root = File.expand_path("../../../..", __dir__)
|
|
32
|
+
Pathname.new(module_root).join(
|
|
33
|
+
"spec/decidim_dummy_app/config/initializers/decidim_openid_connect.rb"
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(initializer_path:)
|
|
38
|
+
@initializer_path = Pathname.new(initializer_path)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def call
|
|
42
|
+
raise_missing_dummy_app unless dummy_app_installed?
|
|
43
|
+
|
|
44
|
+
content = initializer_path.file? ? initializer_path.read : nil
|
|
45
|
+
status = content == SNIPPET ? :skipped : :patched
|
|
46
|
+
initializer_path.write(SNIPPET) if status == :patched
|
|
47
|
+
status
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
attr_reader :initializer_path
|
|
53
|
+
|
|
54
|
+
def dummy_app_installed?
|
|
55
|
+
dummy_app_root.join("config/application.rb").file?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def dummy_app_root
|
|
59
|
+
initializer_path.dirname.dirname.dirname
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def raise_missing_dummy_app
|
|
63
|
+
raise "Missing dummy app (#{dummy_app_root}). Run: bundle exec rake test_app"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Dev
|
|
6
|
+
# Registers the openid_connect OmniAuth strategy for local Zitadel testing.
|
|
7
|
+
module OpenidConnectSetup
|
|
8
|
+
CONFIG_MAPPING = {
|
|
9
|
+
name: :name,
|
|
10
|
+
response_type: :response_type,
|
|
11
|
+
response_mode: :response_mode,
|
|
12
|
+
discovery: :discovery,
|
|
13
|
+
scope: :scope,
|
|
14
|
+
uid_field: :uid_field,
|
|
15
|
+
issuer: :issuer,
|
|
16
|
+
post_logout_redirect_uri: :post_logout_redirect_uri,
|
|
17
|
+
client_options__port: :client_options__port,
|
|
18
|
+
client_options__scheme: :client_options__scheme,
|
|
19
|
+
client_options__host: :client_options__host,
|
|
20
|
+
client_options__authorization_endpoint: :client_options__authorization_endpoint,
|
|
21
|
+
client_options__token_endpoint: :client_options__token_endpoint,
|
|
22
|
+
client_options__userinfo_endpoint: :client_options__userinfo_endpoint,
|
|
23
|
+
client_options__jwks_uri: :client_options__jwks_uri,
|
|
24
|
+
client_options__end_session_endpoint: :client_options__end_session_endpoint,
|
|
25
|
+
client_options__identifier: :client_options__identifier,
|
|
26
|
+
client_options__secret: :client_options__secret,
|
|
27
|
+
client_options__redirect_uri: :client_options__redirect_uri
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
module_function
|
|
31
|
+
|
|
32
|
+
def enabled?
|
|
33
|
+
return false unless defined?(OmniAuth::Strategies::OpenIDConnect)
|
|
34
|
+
# Compose sets ZITADEL_OIDC_ENABLED=1 for local OIDC; never load that
|
|
35
|
+
# middleware in the test suite (setup decrypts org omniauth settings).
|
|
36
|
+
return false if Rails.env.test?
|
|
37
|
+
|
|
38
|
+
ActiveModel::Type::Boolean.new.cast(
|
|
39
|
+
ENV.fetch("ZITADEL_OIDC_ENABLED", Rails.env.development?)
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def register_middleware!
|
|
44
|
+
return unless enabled?
|
|
45
|
+
|
|
46
|
+
configure_zitadel_http_clients!
|
|
47
|
+
|
|
48
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
|
49
|
+
provider(
|
|
50
|
+
:openid_connect,
|
|
51
|
+
setup: Decidim::CustomUserFields::Dev::OpenidConnectSetup.provider_setup
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def provider_setup
|
|
57
|
+
lambda do |env|
|
|
58
|
+
request = Rack::Request.new(env)
|
|
59
|
+
organization = Decidim::Organization.find_by(host: request.host)
|
|
60
|
+
provider_config = Decidim::CustomUserFields::Dev::OpenidConnectSetup.openid_connect_provider_config(organization)
|
|
61
|
+
|
|
62
|
+
CONFIG_MAPPING.each do |option_key, config_key|
|
|
63
|
+
value = provider_config[config_key]
|
|
64
|
+
next if value.nil?
|
|
65
|
+
|
|
66
|
+
if option_key.to_s.include?("__")
|
|
67
|
+
key_parent, key = option_key.to_s.split("__", 2)
|
|
68
|
+
env["omniauth.strategy"].options[key_parent.to_sym] ||= {}
|
|
69
|
+
env["omniauth.strategy"].options[key_parent.to_sym][key.to_sym] = value
|
|
70
|
+
else
|
|
71
|
+
env["omniauth.strategy"].options[option_key] = value
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
env["omniauth.strategy"]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def openid_connect_provider_config(organization)
|
|
80
|
+
return {} if organization.blank?
|
|
81
|
+
|
|
82
|
+
organization.enabled_omniauth_providers.fetch(:openid_connect, {})
|
|
83
|
+
rescue ActiveSupport::MessageEncryptor::InvalidMessage
|
|
84
|
+
{}
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Zitadel resolves the instance from the HTTP Host header (ExternalDomain=localhost).
|
|
88
|
+
# Server-side calls use the Docker service name in the URL but must send Host: localhost:8080.
|
|
89
|
+
def configure_zitadel_http_clients!
|
|
90
|
+
host_header = { "Host" => zitadel_public_host }
|
|
91
|
+
|
|
92
|
+
Rack::OAuth2.http_config do |faraday|
|
|
93
|
+
faraday.headers.update(host_header)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
OpenIDConnect.http_config do |faraday|
|
|
97
|
+
faraday.headers.update(host_header)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def zitadel_public_host
|
|
102
|
+
ENV.fetch("ZITADEL_PUBLIC_HOST") do
|
|
103
|
+
uri = URI.parse(ENV.fetch("OIDC_ISSUER", "http://localhost:8080"))
|
|
104
|
+
uri.port == uri.default_port ? uri.host : "#{uri.host}:#{uri.port}"
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module ScenarioCustomizations
|
|
6
|
+
class << self
|
|
7
|
+
def register!
|
|
8
|
+
return if Customizations.find(:birthdate_age_gates)
|
|
9
|
+
|
|
10
|
+
register_birthdate_age_gates!
|
|
11
|
+
register_location_validation!
|
|
12
|
+
register_association!
|
|
13
|
+
refresh_form_definitions!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def refresh_form_definitions!
|
|
17
|
+
[
|
|
18
|
+
Decidim::RegistrationForm,
|
|
19
|
+
Decidim::OmniauthRegistrationForm,
|
|
20
|
+
Decidim::AccountForm
|
|
21
|
+
].each { |form_class| FormDefinition.setup_form_class(form_class) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def register_birthdate_age_gates!
|
|
27
|
+
Decidim::CustomUserFields.register_customization(:birthdate_age_gates) do |customization|
|
|
28
|
+
customization.registration_fields do |set|
|
|
29
|
+
set.add_field :birthdate, type: :date, required: false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
{ "TwelvePlus" => 12, "SixteenPlus" => 16, "EighteenPlus" => 18 }.each do |authorization_name, years|
|
|
33
|
+
customization.authorization authorization_name do |config|
|
|
34
|
+
config.add_field :birthdate,
|
|
35
|
+
type: :extra_field_ref,
|
|
36
|
+
ref: :birthdate,
|
|
37
|
+
required: true,
|
|
38
|
+
not_after: years.years.ago.to_date.iso8601
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def register_location_validation!
|
|
45
|
+
Decidim::CustomUserFields.register_customization(:location_validation) do |customization|
|
|
46
|
+
customization.authorization "LocationValidated" do |config|
|
|
47
|
+
config.add_field :birthdate, type: :text, required: true, format: /\A\d{2}\.\d{2}\.\d{4}\z/
|
|
48
|
+
config.add_field :postal_code, type: :text, required: true
|
|
49
|
+
config.add_field :full_name, type: :text, required: true
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def register_association!
|
|
55
|
+
Decidim::CustomUserFields.register_customization(:association) do |customization|
|
|
56
|
+
customization.registration_fields do |set|
|
|
57
|
+
set.add_field :represent_association, type: :boolean, required: true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
customization.authorization "AssociationOnly" do |config|
|
|
61
|
+
config.add_field :represent_association,
|
|
62
|
+
type: :extra_field_ref,
|
|
63
|
+
ref: :represent_association,
|
|
64
|
+
required: true,
|
|
65
|
+
must_be_true: true
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails"
|
|
4
|
+
require "decidim/core"
|
|
5
|
+
require "decidim/toggle"
|
|
6
|
+
require "deface"
|
|
4
7
|
|
|
5
8
|
module Decidim
|
|
6
9
|
module CustomUserFields
|
|
@@ -12,30 +15,60 @@ module Decidim
|
|
|
12
15
|
# root to: "custom_user_fields#index"
|
|
13
16
|
end
|
|
14
17
|
|
|
15
|
-
|
|
18
|
+
# Avoid Zeitwerk::NameError by copying this override from Deface (no longer works after Rails 7.1 because of API changes)
|
|
19
|
+
initializer "decidim_custom_user_fields.zeitwerk_ignore_overrides",
|
|
20
|
+
before: :set_load_path do |app|
|
|
21
|
+
next if Rails.configuration.respond_to?(:autoloader) && Rails.configuration.autoloader != :zeitwerk
|
|
22
|
+
|
|
23
|
+
overrides_path = Engine.root.join("app/overrides")
|
|
24
|
+
app.config.eager_load_paths.delete(overrides_path)
|
|
25
|
+
Rails.autoloaders.each { |autoloader| autoloader.ignore(overrides_path) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
initializer "decidim_custom_user_fields.registration_additions" do
|
|
16
29
|
config.to_prepare do
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
DecidimIntegrations.apply!
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
initializer "decidim_custom_user_fields.dev_openid_connect",
|
|
35
|
+
after: :load_config_initializers do
|
|
36
|
+
next unless defined?(Decidim::CustomUserFields::Dev::OpenidConnectSetup)
|
|
37
|
+
|
|
38
|
+
Decidim::CustomUserFields::Dev::OpenidConnectSetup.register_middleware!
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
initializer "decidim_custom_user_fields.dev_scenarios" do
|
|
42
|
+
next unless Rails.env.development?
|
|
43
|
+
|
|
44
|
+
require "decidim/custom_user_fields/dev/scenario_customizations"
|
|
45
|
+
Rails.application.config.to_prepare do
|
|
46
|
+
Decidim::CustomUserFields::ScenarioCustomizations.register!
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
initializer "decidim_custom_user_fields.authorization_workflow_filter" do
|
|
51
|
+
next unless Decidim::Toggle.respond_to?(:filter_authorization_workflows)
|
|
52
|
+
|
|
53
|
+
Decidim::Toggle.filter_authorization_workflows do |workflow, organization|
|
|
54
|
+
Verifications.selectable_workflow?(
|
|
55
|
+
workflow,
|
|
56
|
+
Verifications.enabled_customization_names(organization)
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
initializer "decidim_custom_user_fields.organization_settings_tab",
|
|
62
|
+
after: "decidim_toggle.organization_settings_tabs" do
|
|
63
|
+
Decidim::Toggle.settings_tabs :organization_settings do |tabs|
|
|
64
|
+
next unless Customizations.any?
|
|
65
|
+
|
|
66
|
+
tabs.add_tab :custom_user_fields,
|
|
67
|
+
::I18n.t("decidim.custom_user_fields.system.customizations_tab"),
|
|
68
|
+
form: Admin::CustomizationsConfigForm,
|
|
69
|
+
command: UpdateCustomizationsConfigCommand,
|
|
70
|
+
module_name: :custom_user_fields,
|
|
71
|
+
form_layout_partial: "decidim/custom_user_fields/system/customizations_tab"
|
|
39
72
|
end
|
|
40
73
|
end
|
|
41
74
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module ExtendedData
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def validate_params(organization, params, errors: ActiveModel::Errors.new(Object.new))
|
|
9
|
+
params = params.to_h.with_indifferent_access
|
|
10
|
+
active_registration_fields(organization).each do |field_def|
|
|
11
|
+
key = field_def.name
|
|
12
|
+
next unless params.has_key?(key.to_s) || params.has_key?(key)
|
|
13
|
+
|
|
14
|
+
raw = params[key.to_s] || params[key]
|
|
15
|
+
value = field_def.sanitized_value(raw)
|
|
16
|
+
field_def.validate(value, params, errors)
|
|
17
|
+
end
|
|
18
|
+
errors
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def build_from_params(organization, params)
|
|
22
|
+
params = params.to_h.with_indifferent_access
|
|
23
|
+
data = {}
|
|
24
|
+
active_registration_fields(organization).each do |field_def|
|
|
25
|
+
key = field_def.name
|
|
26
|
+
next unless params.has_key?(key.to_s) || params.has_key?(key)
|
|
27
|
+
|
|
28
|
+
raw = params[key.to_s] || params[key]
|
|
29
|
+
data[key] = field_def.sanitized_value(raw)
|
|
30
|
+
end
|
|
31
|
+
data
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def merge_into(user, organization, params)
|
|
35
|
+
errors = ActiveModel::Errors.new(user)
|
|
36
|
+
validate_params(organization, params, errors:)
|
|
37
|
+
return [false, errors] if errors.any?
|
|
38
|
+
|
|
39
|
+
extended_data = (user.extended_data || {}).merge(build_from_params(organization, params))
|
|
40
|
+
if user.update(extended_data:)
|
|
41
|
+
[true, errors]
|
|
42
|
+
else
|
|
43
|
+
[false, user.errors]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def active_registration_fields(organization)
|
|
48
|
+
RegistrationFields.active_registration_fields(organization)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "forwardable"
|
|
2
4
|
|
|
3
5
|
module Decidim
|
|
4
6
|
module CustomUserFields
|
|
@@ -7,28 +9,48 @@ module Decidim
|
|
|
7
9
|
|
|
8
10
|
attr_reader :type,
|
|
9
11
|
:name,
|
|
10
|
-
:field
|
|
12
|
+
:field,
|
|
13
|
+
:handler_name
|
|
11
14
|
|
|
12
15
|
def_delegators :@field,
|
|
13
16
|
:configure_form,
|
|
14
17
|
:map_model,
|
|
15
|
-
:form_tag
|
|
18
|
+
:form_tag,
|
|
19
|
+
:options,
|
|
20
|
+
:options=,
|
|
21
|
+
:i18n_context,
|
|
22
|
+
:i18n_context=,
|
|
23
|
+
:validate,
|
|
24
|
+
:skip_hashing?,
|
|
25
|
+
:sanitized_value,
|
|
26
|
+
:storage_name
|
|
16
27
|
|
|
17
|
-
def initialize(name, kwargs)
|
|
28
|
+
def initialize(name, kwargs, handler_name)
|
|
29
|
+
@handler_name = handler_name
|
|
18
30
|
@name = name.to_s.to_sym
|
|
19
31
|
@type = kwargs[:type]
|
|
20
|
-
# TODO: parse kwargs
|
|
21
32
|
case type
|
|
33
|
+
when :dummy
|
|
34
|
+
@field = Fields::DummyField.new(self, kwargs)
|
|
22
35
|
when :text
|
|
23
36
|
@field = Fields::TextField.new(self, kwargs)
|
|
24
37
|
when :textarea
|
|
25
38
|
@field = Fields::TextAreaField.new(self, kwargs)
|
|
26
39
|
when :date
|
|
27
40
|
@field = Fields::DateField.new(self, kwargs)
|
|
41
|
+
when :extra_field_ref
|
|
42
|
+
@field = Fields::ExtraFieldRefField.new(self, kwargs)
|
|
43
|
+
when :boolean
|
|
44
|
+
@field = Fields::BooleanField.new(self, kwargs)
|
|
28
45
|
else
|
|
29
|
-
raise Error,
|
|
46
|
+
raise Error, DefinitionGuidance.unsupported_type_message(type)
|
|
30
47
|
end
|
|
31
48
|
end
|
|
49
|
+
|
|
50
|
+
def deep_dup
|
|
51
|
+
opts = field.options.except(:type, "type").merge(type:)
|
|
52
|
+
self.class.new(name, opts, handler_name)
|
|
53
|
+
end
|
|
32
54
|
end
|
|
33
55
|
end
|
|
34
56
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Fields
|
|
6
|
+
class BooleanField < GenericField
|
|
7
|
+
def configure_form(form)
|
|
8
|
+
form.attribute(name, :boolean)
|
|
9
|
+
validations = {}
|
|
10
|
+
if options[:must_be_true] || required?
|
|
11
|
+
validations[:inclusion] = {
|
|
12
|
+
in: [true, "1", 1, "true"],
|
|
13
|
+
message: proc { label(:required) }
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
apply_form_validations(form, validations)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def skip_hashing?
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def validate(value, _data, errors)
|
|
24
|
+
return unless options[:must_be_true] || required?
|
|
25
|
+
|
|
26
|
+
errors.add(name, label(:required)) unless ActiveModel::Type::Boolean.new.cast(value)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def map_model(form, data)
|
|
30
|
+
return if data[name].nil?
|
|
31
|
+
|
|
32
|
+
form[name] = ActiveModel::Type::Boolean.new.cast(data[name])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def sanitized_value(value)
|
|
36
|
+
ActiveModel::Type::Boolean.new.cast(value)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def form_tag(form_tag)
|
|
40
|
+
form_tag.check_box(
|
|
41
|
+
name,
|
|
42
|
+
label: label(:label),
|
|
43
|
+
help_text: label_exists?(:help_text) && label(:help_text),
|
|
44
|
+
class: class_name,
|
|
45
|
+
label_options: { class: label_class_name }
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -1,31 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Decidim
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Fields
|
|
6
|
+
class DateField < GenericField
|
|
7
|
+
def configure_form(form)
|
|
8
|
+
form.attribute(name, String)
|
|
9
|
+
validations = {
|
|
10
|
+
format: {
|
|
11
|
+
with: /\A\d{4}-\d{2}-\d{2}\z/,
|
|
12
|
+
allow_blank: !required?,
|
|
13
|
+
message: proc do
|
|
14
|
+
label(:bad_format)
|
|
15
|
+
end
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if required?
|
|
19
|
+
validations[:presence] = {
|
|
20
|
+
message: proc do |_object, _data|
|
|
21
|
+
label(:required)
|
|
22
|
+
end
|
|
23
|
+
}
|
|
8
24
|
end
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
25
|
+
apply_form_validations(form, validations)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def validate(value, _data, errors)
|
|
29
|
+
return if value.blank? && !required?
|
|
30
|
+
|
|
31
|
+
if value.blank?
|
|
32
|
+
errors.add(name, label(:required))
|
|
33
|
+
return
|
|
12
34
|
end
|
|
13
|
-
|
|
14
|
-
def form_tag(form_tag)
|
|
15
|
-
field_options = {}
|
|
16
|
-
if options[:min].present? || options[:max].present?
|
|
17
|
-
field_options[:min] = options[:min]
|
|
18
|
-
field_options[:max] = options[:max]
|
|
19
|
-
end
|
|
20
35
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
)
|
|
36
|
+
date_value = nil
|
|
37
|
+
begin
|
|
38
|
+
date_value = Date.strptime(value, "%Y-%m-%d")
|
|
39
|
+
rescue TypeError, ::Date::Error
|
|
40
|
+
errors.add(name, label(:bad_date))
|
|
41
|
+
return
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
validate_not_before(date_value, errors) if options[:not_before].present?
|
|
45
|
+
validate_not_after(date_value, errors) if options[:not_after].present?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def map_model(form, data)
|
|
49
|
+
form[name] = data[name] if data[name].present?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def sanitized_value(raw_value)
|
|
53
|
+
return nil if raw_value.blank?
|
|
54
|
+
|
|
55
|
+
raw_value
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def form_tag(form_tag)
|
|
59
|
+
options = {
|
|
60
|
+
label: label(:label),
|
|
61
|
+
help_text: label_exists?(:help_text) && label(:help_text),
|
|
62
|
+
label_options: { class: label_class_name },
|
|
63
|
+
class: class_name
|
|
64
|
+
}
|
|
65
|
+
raw = form_tag.object[name]
|
|
66
|
+
form_tag.object.public_send("#{name}=", nil) if raw.blank?
|
|
67
|
+
if raw.present?
|
|
68
|
+
begin
|
|
69
|
+
options[:value] = Date.strptime(raw.to_s, "%Y-%m-%d")
|
|
70
|
+
rescue Date::Error
|
|
71
|
+
# ignore invalid stored values when re-rendering the form
|
|
72
|
+
end
|
|
26
73
|
end
|
|
74
|
+
form_tag.date_field(
|
|
75
|
+
name,
|
|
76
|
+
options
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def not_before_date
|
|
83
|
+
Date.strptime(options[:not_before], "%Y-%m-%d")
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def not_after_date
|
|
87
|
+
Date.strptime(options[:not_after], "%Y-%m-%d")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def validate_not_before(value, errors)
|
|
91
|
+
errors.add(name, label(:bad_not_before)) if value < not_before_date
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def validate_not_after(value, errors)
|
|
95
|
+
errors.add(name, label(:bad_not_after)) if value > not_after_date
|
|
27
96
|
end
|
|
28
97
|
end
|
|
29
98
|
end
|
|
30
99
|
end
|
|
31
|
-
|
|
100
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Fields
|
|
6
|
+
class DummyField < GenericField
|
|
7
|
+
def configure_form(form)
|
|
8
|
+
form.attribute(name, String)
|
|
9
|
+
validation_hash
|
|
10
|
+
|
|
11
|
+
apply_form_validations(form, validation_hash)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def validation_hash
|
|
15
|
+
{}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def map_model(form, data)
|
|
19
|
+
form[name] = data[name].strip if data[name].present?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def sanitized_value(value)
|
|
23
|
+
stripped_value = (value || "").strip
|
|
24
|
+
return stripped_value if stripped_value.present?
|
|
25
|
+
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def form_tag(_form_tag)
|
|
30
|
+
{
|
|
31
|
+
name:,
|
|
32
|
+
label: label(:label),
|
|
33
|
+
help_text: label_exists?(:help_text) && label(:help_text),
|
|
34
|
+
class_name:
|
|
35
|
+
}.to_json
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|