decidim-cdtb 0.1.4 → 0.1.7
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/CHANGELOG.md +13 -0
- data/Gemfile.lock +1 -1
- data/README.md +30 -11
- data/decidim-cdtb.gemspec +1 -1
- data/lib/decidim/cdtb/spam/user_spam_detector.rb +6 -3
- data/lib/decidim/cdtb/tasks.rb +1 -0
- data/lib/decidim/cdtb/users/remover.rb +125 -0
- data/lib/decidim/cdtb/version.rb +1 -1
- data/lib/tasks/users.rake +23 -0
- metadata +8 -7
- data/lib/tasks/cdtb.rake +0 -13
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 7ea237b7898bc194b0f63514d1f699d5cd1cf649ac7dd7433de19d9319541474
         | 
| 4 | 
            +
              data.tar.gz: be9a6f3251d35022b933524b3227edac2186a1c338a4025f6d73eb5495633e61
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3999c77a44817f3c0b3bc6b97663a47f3dade93f896958cdce06298bd9695d734cdd82859315607ecc3fcd990efd6e567e2e63d6e0141f5c3d256bc44d6b91ef
         | 
| 7 | 
            +
              data.tar.gz: 513123ffe284a654f47666d8a0e355b29cb3dd011a30bab73cdef0348776f45cd44dab8c8c9f23f5e962fb5f00a5dc9acb9d68a70ced7b39335cfc225ce56839
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,5 +1,18 @@ | |
| 1 1 | 
             
            ## [Unreleased]
         | 
| 2 2 |  | 
| 3 | 
            +
            ## [0.1.7] - 2024-04-22 (Emocions de colors)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            - Fix remove users task and add the reporter user mailer to arguments
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ## [0.1.6] - 2024-04-11 (Malifetes ben fetes)
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            - Add remover users task
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            ## [0.1.5] - 2024-02-08 (Pastissos voladors de colors)
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            - Fix homepage in rubygems.org
         | 
| 14 | 
            +
            - Add organization id and name in spam csv
         | 
| 15 | 
            +
             | 
| 3 16 | 
             
            ## [0.1.4] - 2024-01-30 (Peus grans com gegants)
         | 
| 4 17 |  | 
| 5 18 | 
             
            - Add users spam detector task
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -36,17 +36,6 @@ bin/rake cdtb:org_by_host_like[vila] | |
| 36 36 | 
             
            host: localhost, time_zone: Madrid, locales: ca + [ca, es, oc], available authorizations: [postal_letter, members_picker_authorization_handler]
         | 
| 37 37 | 
             
            ```
         | 
| 38 38 |  | 
| 39 | 
            -
            ### Fix nicknames
         | 
| 40 | 
            -
             | 
| 41 | 
            -
            In a previous version than Decidim v0.25 a validation to the `Decidim::User.nickname` was added with a migration to fix existing nicknames. But the migration was only taking into acocunt managed (impersonated) users.
         | 
| 42 | 
            -
             | 
| 43 | 
            -
            This task iterates (with `find_each`) over all non managed users and nicknamizes the nickname.
         | 
| 44 | 
            -
             | 
| 45 | 
            -
            To execute the task run:
         | 
| 46 | 
            -
             | 
| 47 | 
            -
            ```
         | 
| 48 | 
            -
            bin/rake cdtb:fix_nicknames
         | 
| 49 | 
            -
            ```
         | 
| 50 39 |  | 
| 51 40 | 
             
            ### Anonymize production dump
         | 
| 52 41 |  | 
| @@ -98,6 +87,36 @@ Decidim::Cdtb.configure do |config| | |
| 98 87 | 
             
            end
         | 
| 99 88 | 
             
            ```
         | 
| 100 89 |  | 
| 90 | 
            +
            ### Users
         | 
| 91 | 
            +
             | 
| 92 | 
            +
            Tasks related with users.
         | 
| 93 | 
            +
             | 
| 94 | 
            +
            ### Fix nicknames
         | 
| 95 | 
            +
             | 
| 96 | 
            +
            In a previous version than Decidim v0.25 a validation to the `Decidim::User.nickname` was added with a migration to fix existing nicknames. But the migration was only taking into account managed (impersonated) users.
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            This task iterates (with `find_each`) over all non managed users and nicknamizes the nickname.
         | 
| 99 | 
            +
             | 
| 100 | 
            +
            To execute the task run:
         | 
| 101 | 
            +
             | 
| 102 | 
            +
            ```
         | 
| 103 | 
            +
            bin/rake cdtb:users:fix_nicknames
         | 
| 104 | 
            +
            ```
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            #### Remove users
         | 
| 107 | 
            +
             | 
| 108 | 
            +
            You can delete users through a CSV with the user ID and a reporter user mailer. The purpose is to be able to eliminate potentially spammy users.
         | 
| 109 | 
            +
             | 
| 110 | 
            +
            This task reports and hide the user's comments, blocks the user, and finally deletes the user.
         | 
| 111 | 
            +
             | 
| 112 | 
            +
            The CSV will have a header and one column with the user ID.
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            To execute the task run:
         | 
| 115 | 
            +
             | 
| 116 | 
            +
            ```
         | 
| 117 | 
            +
            bundle exec rake cdtb:users:remove[spam_users.csv, reporter_user@example.org]
         | 
| 118 | 
            +
            ```
         | 
| 119 | 
            +
             | 
| 101 120 | 
             
            ### Upgrades:
         | 
| 102 121 |  | 
| 103 122 | 
             
            #### Upgrade modules
         | 
    
        data/decidim-cdtb.gemspec
    CHANGED
    
    | @@ -10,7 +10,7 @@ Gem::Specification.new do |spec| | |
| 10 10 |  | 
| 11 11 | 
             
              spec.summary = "CodiTramuntana's Decidim Toolbelt (cdtb)."
         | 
| 12 12 | 
             
              spec.description = "A gem to help managing Decidim applications."
         | 
| 13 | 
            -
              spec.homepage = " | 
| 13 | 
            +
              spec.homepage = "https://github.com/CodiTramuntana/decidim-module-cdtb"
         | 
| 14 14 | 
             
              spec.license = "MIT"
         | 
| 15 15 | 
             
              spec.required_ruby_version = ">= 2.7.5"
         | 
| 16 16 |  | 
| @@ -35,11 +35,12 @@ module Decidim | |
| 35 35 | 
             
                      @num_users
         | 
| 36 36 | 
             
                    end
         | 
| 37 37 |  | 
| 38 | 
            +
                    # rubocop:disable Metrics/AbcSize
         | 
| 38 39 | 
             
                    def do_execution(context)
         | 
| 39 40 | 
             
                      progress_bar = context[:progress_bar]
         | 
| 40 | 
            -
             | 
| 41 41 | 
             
                      CSV.open("spam_users.csv", "w") do |csv|
         | 
| 42 | 
            -
                        csv_headers = ["ID", "Is suspicious?", "Name", "Email", "Nickname", "Personal URL", "About" | 
| 42 | 
            +
                        csv_headers = ["ID", "Is suspicious?", "Name", "Email", "Nickname", "Personal URL", "About",
         | 
| 43 | 
            +
                                       "Organization ID", "Organization Name"]
         | 
| 43 44 | 
             
                        csv << csv_headers
         | 
| 44 45 |  | 
| 45 46 | 
             
                        @users.find_each do |user|
         | 
| @@ -50,12 +51,14 @@ module Decidim | |
| 50 51 | 
             
                            @num_applied+= 1
         | 
| 51 52 | 
             
                          end
         | 
| 52 53 |  | 
| 53 | 
            -
                          csv << [user.id, suspicious, user.name, user.email, user.nickname, user.personal_url, user.about | 
| 54 | 
            +
                          csv << [user.id, suspicious, user.name, user.email, user.nickname, user.personal_url, user.about,
         | 
| 55 | 
            +
                                  user.organization.id, user.organization.name]
         | 
| 54 56 |  | 
| 55 57 | 
             
                          progress_bar.increment
         | 
| 56 58 | 
             
                        end
         | 
| 57 59 | 
             
                      end
         | 
| 58 60 | 
             
                    end
         | 
| 61 | 
            +
                    # rubocop:enable Metrics/AbcSize
         | 
| 59 62 |  | 
| 60 63 | 
             
                    def end_execution(_ctx)
         | 
| 61 64 | 
             
                      if @num_applied.positive?
         | 
    
        data/lib/decidim/cdtb/tasks.rb
    CHANGED
    
    | @@ -3,6 +3,7 @@ | |
| 3 3 | 
             
            require "decidim/cdtb/tasks_utils"
         | 
| 4 4 | 
             
            require "decidim/cdtb/task"
         | 
| 5 5 | 
             
            require "decidim/cdtb/fixes/nickname_fixer"
         | 
| 6 | 
            +
            require "decidim/cdtb/users/remover"
         | 
| 6 7 | 
             
            require "decidim/cdtb/multitenants/org_by_host_like"
         | 
| 7 8 | 
             
            require "decidim/cdtb/spam/user_spam_detector"
         | 
| 8 9 | 
             
            require "decidim/cdtb/storage/local_sharding"
         | 
| @@ -0,0 +1,125 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Decidim
         | 
| 4 | 
            +
              module Cdtb
         | 
| 5 | 
            +
                module Users
         | 
| 6 | 
            +
                  # Remove Decidim::User's
         | 
| 7 | 
            +
                  #
         | 
| 8 | 
            +
                  class Remover < ::Decidim::Cdtb::Task
         | 
| 9 | 
            +
                    def initialize(csv_path, reporter_user_email)
         | 
| 10 | 
            +
                      @csv_path = csv_path
         | 
| 11 | 
            +
                      @reporter_user_email = reporter_user_email
         | 
| 12 | 
            +
                      progress_bar = { title: "Decidim::User" }
         | 
| 13 | 
            +
                      super("USER REMOVER", progress_bar: progress_bar)
         | 
| 14 | 
            +
                    end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    def prepare_execution(_ctx); end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    def total_items
         | 
| 19 | 
            +
                      File.open(@csv_path).readlines.size - 1
         | 
| 20 | 
            +
                    end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                    def do_execution(context)
         | 
| 23 | 
            +
                      progress_bar = context[:progress_bar]
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                      CSV.foreach(@csv_path, headers: true, col_sep: ",") do |row|
         | 
| 26 | 
            +
                        user = Decidim::User.find_by(id: row[0])
         | 
| 27 | 
            +
                        next unless user.present?
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                        reporter_user = Decidim::User.find_by(email: @reporter_user_email,
         | 
| 30 | 
            +
                                                              organization: user.organization)
         | 
| 31 | 
            +
                        comments = Decidim::Comments::Comment.where(decidim_author_id: user.id)
         | 
| 32 | 
            +
                        manage_comments(comments, user, reporter_user) unless comments.empty?
         | 
| 33 | 
            +
                        destroy_user(user) if block_user(user, reporter_user)
         | 
| 34 | 
            +
                        progress_bar.increment
         | 
| 35 | 
            +
                      end
         | 
| 36 | 
            +
                    end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    def end_execution(_ctx)
         | 
| 39 | 
            +
                      log_task_step("#{@num_applied} users removed")
         | 
| 40 | 
            +
                    end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                    private
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                    def manage_comments(comments, user, reporter_user)
         | 
| 45 | 
            +
                      comments.find_each do |comment|
         | 
| 46 | 
            +
                        report_comment(comment, user, reporter_user)
         | 
| 47 | 
            +
                      end
         | 
| 48 | 
            +
                    end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                    def block_user(user, reporter_user)
         | 
| 51 | 
            +
                      params = {
         | 
| 52 | 
            +
                        user_id: user.id,
         | 
| 53 | 
            +
                        justification: "Confirmed spam suspicious"
         | 
| 54 | 
            +
                      }
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                      form = Decidim::Admin::BlockUserForm.from_params(params).with_context(
         | 
| 57 | 
            +
                        {
         | 
| 58 | 
            +
                          current_organization: user.organization,
         | 
| 59 | 
            +
                          current_user: reporter_user
         | 
| 60 | 
            +
                        }
         | 
| 61 | 
            +
                      )
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                      Decidim::Admin::BlockUser.call(form) do
         | 
| 64 | 
            +
                        on(:ok) do
         | 
| 65 | 
            +
                          puts "OK: User #{user.id} blocked"
         | 
| 66 | 
            +
                          return true
         | 
| 67 | 
            +
                        end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                        on(:invalid) do
         | 
| 70 | 
            +
                          puts "ERROR: User #{user.id} not blocked"
         | 
| 71 | 
            +
                          return false
         | 
| 72 | 
            +
                        end
         | 
| 73 | 
            +
                      end
         | 
| 74 | 
            +
                    end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                    def destroy_user(user)
         | 
| 77 | 
            +
                      params = {
         | 
| 78 | 
            +
                        delete_reason: "Confirmed spam suspicious"
         | 
| 79 | 
            +
                      }
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                      form = Decidim::DeleteAccountForm.from_params(params)
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                      Decidim::DestroyAccount.call(user, form) do
         | 
| 84 | 
            +
                        on(:ok) do
         | 
| 85 | 
            +
                          puts "OK: User #{user.id} removed"
         | 
| 86 | 
            +
                        end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                        on(:invalid) do
         | 
| 89 | 
            +
                          puts "ERROR: User #{user.id} not removed"
         | 
| 90 | 
            +
                        end
         | 
| 91 | 
            +
                      end
         | 
| 92 | 
            +
                    end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                    def report_comment(comment, user, reporter_user)
         | 
| 95 | 
            +
                      params = {
         | 
| 96 | 
            +
                        reason: "spam",
         | 
| 97 | 
            +
                        details: "Spam message"
         | 
| 98 | 
            +
                      }
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                      form = Decidim::ReportForm.from_params(params).with_context(context_for_report(user, comment, reporter_user))
         | 
| 101 | 
            +
                      reportable = GlobalID::Locator.locate_signed(comment.to_sgid.to_s)
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                      Decidim::CreateReport.call(form, reportable, reporter_user) do
         | 
| 104 | 
            +
                        on(:ok) do
         | 
| 105 | 
            +
                          puts "OK: Comment #{comment.id} of User #{user.id} reported"
         | 
| 106 | 
            +
                        end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                        on(:invalid) do
         | 
| 109 | 
            +
                          puts "ERROR: Comment #{comment.id} of User #{user.id} not reported"
         | 
| 110 | 
            +
                        end
         | 
| 111 | 
            +
                      end
         | 
| 112 | 
            +
                    end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                    def context_for_report(user, comment, reporter_user)
         | 
| 115 | 
            +
                      {
         | 
| 116 | 
            +
                        current_organization: user.organization,
         | 
| 117 | 
            +
                        current_component: comment.component,
         | 
| 118 | 
            +
                        current_user: reporter_user,
         | 
| 119 | 
            +
                        current_participatory_space: comment.participatory_space
         | 
| 120 | 
            +
                      }
         | 
| 121 | 
            +
                    end
         | 
| 122 | 
            +
                  end
         | 
| 123 | 
            +
                end
         | 
| 124 | 
            +
              end
         | 
| 125 | 
            +
            end
         | 
    
        data/lib/decidim/cdtb/version.rb
    CHANGED
    
    
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "decidim/cdtb/tasks"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            namespace :cdtb do
         | 
| 6 | 
            +
              namespace :users do
         | 
| 7 | 
            +
                desc <<~EODESC
         | 
| 8 | 
            +
                  Fix Decidim::User's nicknames.
         | 
| 9 | 
            +
                EODESC
         | 
| 10 | 
            +
                task fix_nicknames: [:environment] do
         | 
| 11 | 
            +
                  fixer= ::Decidim::Cdtb::Fixes::NicknameFixer.new
         | 
| 12 | 
            +
                  fixer.execute!
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                desc <<~EODESC
         | 
| 16 | 
            +
                  Remove Decidim::User's by IDs in a CSV.
         | 
| 17 | 
            +
                EODESC
         | 
| 18 | 
            +
                task :remove, %i[csv_path reporter_user_email] => [:environment] do |_taks, args|
         | 
| 19 | 
            +
                  service = ::Decidim::Cdtb::Users::Remover.new(args.csv_path, args.reporter_user_email)
         | 
| 20 | 
            +
                  service.execute!
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: decidim-cdtb
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.7
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Oliver Valls
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2024- | 
| 11 | 
            +
            date: 2024-04-22 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: decidim
         | 
| @@ -114,24 +114,25 @@ files: | |
| 114 114 | 
             
            - lib/decidim/cdtb/upgrades/install_gem_migrations_step.rb
         | 
| 115 115 | 
             
            - lib/decidim/cdtb/upgrades/upgrade_modules_task.rb
         | 
| 116 116 | 
             
            - lib/decidim/cdtb/upgrades/validate_migrations_task.rb
         | 
| 117 | 
            +
            - lib/decidim/cdtb/users/remover.rb
         | 
| 117 118 | 
             
            - lib/decidim/cdtb/version.rb
         | 
| 118 119 | 
             
            - lib/generators/cdtb/USAGE
         | 
| 119 120 | 
             
            - lib/generators/cdtb/templates/validate_migrations.yml
         | 
| 120 121 | 
             
            - lib/generators/cdtb/validate_migrations_ci_generator.rb
         | 
| 121 122 | 
             
            - lib/tasks/anonymize.rake
         | 
| 122 | 
            -
            - lib/tasks/cdtb.rake
         | 
| 123 123 | 
             
            - lib/tasks/multitenants.rake
         | 
| 124 124 | 
             
            - lib/tasks/spam.rake
         | 
| 125 125 | 
             
            - lib/tasks/storage.rake
         | 
| 126 126 | 
             
            - lib/tasks/upgrade.rake
         | 
| 127 | 
            +
            - lib/tasks/users.rake
         | 
| 127 128 | 
             
            - sig/decidim/cdtb.rbs
         | 
| 128 | 
            -
            homepage:  | 
| 129 | 
            +
            homepage: https://github.com/CodiTramuntana/decidim-module-cdtb
         | 
| 129 130 | 
             
            licenses:
         | 
| 130 131 | 
             
            - MIT
         | 
| 131 132 | 
             
            metadata:
         | 
| 132 | 
            -
              homepage_uri:  | 
| 133 | 
            -
              source_code_uri:  | 
| 134 | 
            -
              changelog_uri:  | 
| 133 | 
            +
              homepage_uri: https://github.com/CodiTramuntana/decidim-module-cdtb
         | 
| 134 | 
            +
              source_code_uri: https://github.com/CodiTramuntana/decidim-module-cdtb
         | 
| 135 | 
            +
              changelog_uri: https://github.com/CodiTramuntana/decidim-module-cdtb/CHANGELOG.md
         | 
| 135 136 | 
             
            post_install_message: 
         | 
| 136 137 | 
             
            rdoc_options: []
         | 
| 137 138 | 
             
            require_paths:
         | 
    
        data/lib/tasks/cdtb.rake
    DELETED
    
    | @@ -1,13 +0,0 @@ | |
| 1 | 
            -
            # frozen_string_literal: true
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            require "decidim/cdtb/tasks"
         | 
| 4 | 
            -
             | 
| 5 | 
            -
            namespace :cdtb do
         | 
| 6 | 
            -
              desc <<~EODESC
         | 
| 7 | 
            -
                Fix Decidim::User's nicknames.
         | 
| 8 | 
            -
              EODESC
         | 
| 9 | 
            -
              task fix_nicknames: [:environment] do
         | 
| 10 | 
            -
                fixer= ::Decidim::Cdtb::Fixes::NicknameFixer.new
         | 
| 11 | 
            -
                fixer.execute!
         | 
| 12 | 
            -
              end
         | 
| 13 | 
            -
            end
         |