diff --git a/.env-example b/.env-example index 28848eb819..3642eaf4e2 100644 --- a/.env-example +++ b/.env-example @@ -99,3 +99,7 @@ RAILS_LOG_LEVEL=warn # Redirect to the TOS page after signup (default: true) # DECIDIM_HALF_SIGNUP_SHOW_TOS_PAGE_AFTER_SIGNUP=true + +# Automatically save AH metadata to user extended data +# Format : comma separated list of auhtorization handler names +# AUTO_EXPORT_AUTHORIZATIONS_DATA_TO_USER_DATA_ENABLED_FOR="authorization1,authorization2" \ No newline at end of file diff --git a/app/jobs/authorization_data_to_user_data_job.rb b/app/jobs/authorization_data_to_user_data_job.rb new file mode 100644 index 0000000000..433f7f57c0 --- /dev/null +++ b/app/jobs/authorization_data_to_user_data_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AuthorizationDataToUserDataJob < ApplicationJob + queue_as :exports + + def perform(*args) + Decidim::AuthorizationDataToUserDataService.run(*args) + end +end diff --git a/app/services/decidim/authorization_data_to_user_data_service.rb b/app/services/decidim/authorization_data_to_user_data_service.rb new file mode 100644 index 0000000000..db56519e36 --- /dev/null +++ b/app/services/decidim/authorization_data_to_user_data_service.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Decidim + class AuthorizationDataToUserDataService + def self.run(**args) + new(**args).execute + end + + def initialize(**args) + @name = args[:name] + @user = args[:user] + end + + def execute + Decidim::Authorization.find_each(filter) do |authorization| + authorization.user.update(extended_data: authorization.user.extended_data.merge({ @name.to_s => authorization.metadata })) + end + end + + def filter + @filter ||= { + name: @name, + user: @user + }.compact + end + end +end diff --git a/config/application.rb b/config/application.rb index b2e73e8524..1fbb48f0e9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -50,7 +50,8 @@ class Application < Rails::Application require "extends/helpers/decidim/icon_helper_extends" require "extends/commands/decidim/initiatives/admin/update_initiative_answer_extends" require "extends/controllers/decidim/initiatives/committee_requests_controller_extends" - require "extends/models/decidim/budgets/project_extend" + require "extends/models/decidim/budgets/project_extends" + require "extends/models/decidim/authorization_extends" require "extends/forms/decidim/initiatives/admin/initiative_form_extends" require "extends/commands/decidim/budgets/admin/import_proposals_to_budgets_extends" require "extends/controllers/decidim/newsletters_controller_extends" diff --git a/config/secrets.yml b/config/secrets.yml index e89444500e..6a710fa03f 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -18,6 +18,8 @@ default: &default min_length: <%= ENV.fetch("DECIDIM_ADMIN_PASSWORD_MIN_LENGTH", 15).to_i %> repetition_times: <%= ENV.fetch("DECIDIM_ADMIN_PASSWORD_REPETITION_TIMES", 5).to_i %> strong: <%= ENV.fetch("DECIDIM_ADMIN_PASSWORD_STRONG", "false").to_s %> + authorizations: + export_data_to_userdata_enabled_for: <%= ENV.fetch("AUTO_EXPORT_AUTHORIZATIONS_DATA_TO_USER_DATA_ENABLED_FOR", "") %> currency: <%= ENV["CURRENCY"] || "€" %> half_signup: show_tos_page_after_signup: <%= ENV.fetch("DECIDIM_HALF_SIGNUP_SHOW_TOS_PAGE_AFTER_SIGNUP", "true") == "true" %> diff --git a/lib/extends/models/decidim/authorization_extends.rb b/lib/extends/models/decidim/authorization_extends.rb new file mode 100644 index 0000000000..2d9a750ec3 --- /dev/null +++ b/lib/extends/models/decidim/authorization_extends.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "active_support/concern" +module AuthorizationExtends + extend ActiveSupport::Concern + + included do + after_commit :export_to_user_extended_data, if: proc { |authorization| + Rails.application.secrets.dig(:decidim, :export_data_to_userdata_enabled_for)&.split(",")&.include?(authorization.name) + } + + def export_to_user_extended_data + Decidim::AuthorizationDataToUserDataService.run(name: name, user: user) + end + end +end + +Decidim::Authorization.include(AuthorizationExtends) diff --git a/lib/extends/models/decidim/budgets/project_extend.rb b/lib/extends/models/decidim/budgets/project_extends.rb similarity index 100% rename from lib/extends/models/decidim/budgets/project_extend.rb rename to lib/extends/models/decidim/budgets/project_extends.rb diff --git a/lib/tasks/authorizations.rake b/lib/tasks/authorizations.rake new file mode 100644 index 0000000000..c3430a6e3c --- /dev/null +++ b/lib/tasks/authorizations.rake @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +namespace :authorizations do + task export_to_user_extended_data: :environment do + name = ENV["AUTHORIZATION_HANDLE_NAME"].presence + raise "AUTHORIZATION_HANDLE_NAME is blank." if name.blank? + + raise "No data found for authorization handler name '#{name}'" unless Decidim::Authorization.exists?(name: name) + + AuthorizationDataToUserDataJob.perform_later(name: name) + end +end