From c116065b7aa07b429aef8dd7d76cff596c317f30 Mon Sep 17 00:00:00 2001 From: Eduardo Martinez Echevarria Date: Tue, 3 Dec 2024 22:09:53 +0100 Subject: [PATCH] Perform block in task and include admin email --- ...decidim_awesome_autoblock_users_tasks.rake | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/tasks/decidim_awesome_autoblock_users_tasks.rake b/lib/tasks/decidim_awesome_autoblock_users_tasks.rake index 80976258..0560fae4 100644 --- a/lib/tasks/decidim_awesome_autoblock_users_tasks.rake +++ b/lib/tasks/decidim_awesome_autoblock_users_tasks.rake @@ -3,24 +3,29 @@ namespace :decidim_decidim_awesome do namespace :autoblock_users do desc "Performs autoblock users task" - task run: :environment do - Decidim::Organization.all.each do |organization| - if (current_config = organization_config(organization)).blank? - puts "\nSkipping #{organization.name}...\n" + task :run_block, [:admin_email] => :environment do |_task, args| + current_user = Decidim::User.where(admin: true).find_by(email: args.admin_email) + + raise "A valid admin email is required to run this task" if current_user.blank? + + Decidim::Organization.all.each do |current_organization| + if (current_config = organization_config(current_organization)).blank? + puts "\nSkipping #{current_organization.name}...\n" next end - puts "\nRunning on #{organization.name}...\n" + puts "\nRunning on #{current_organization.name}...\n" config_data = OpenStruct.new(current_config.value || {}) - config_form = Decidim::DecidimAwesome::Admin::UsersAutoblocksConfigForm.from_model(config_data).with_context(current_organization: organization) + config_form = Decidim::DecidimAwesome::Admin::UsersAutoblocksConfigForm.from_model(config_data).with_context(current_organization:, current_user:) + config_form.perform_block = true Decidim::DecidimAwesome::Admin::AutoblockUsers.call(config_form) do on(:ok) do |count, block_performed| if block_performed - puts "\n✓ Process finished. Total users detected: #{count}. Block not performed.\n" - else puts "\n✓ Process finished. Total users detected and blocked: #{count}.\n" + else + puts "\n✓ Process finished. Total users detected: #{count}. Block not performed.\n" end end