From 0b3c2aaa3a60ff6f466305e34c3ba03bc3731e5d Mon Sep 17 00:00:00 2001 From: Eduardo Martinez Echevarria Date: Wed, 8 Jan 2025 14:22:41 +0100 Subject: [PATCH] Improve allowlist and blocklists detection --- .../user_autoblock_scores_presenter.rb | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/presenters/decidim/decidim_awesome/user_autoblock_scores_presenter.rb b/app/presenters/decidim/decidim_awesome/user_autoblock_scores_presenter.rb index 6a3dafc5..e5a6d957 100644 --- a/app/presenters/decidim/decidim_awesome/user_autoblock_scores_presenter.rb +++ b/app/presenters/decidim/decidim_awesome/user_autoblock_scores_presenter.rb @@ -6,6 +6,8 @@ module DecidimAwesome # Decorator for users # class UserAutoblockScoresPresenter < SimpleDelegator + class SkipItem < StandardError; end + USERS_AUTOBLOCKS_TYPES = { "about_blank" => { has_variable: false, default_blocklist: true }, "activities_blank" => { has_variable: false, default_blocklist: true }, @@ -40,18 +42,22 @@ def email_unconfirmed_detection_method(allowlist:, blocklist:) end def email_domain_detection_method(allowlist:, blocklist:, skip_with_blank_lists: true) - return if skip_with_blank_lists && allowlist.blank? && blocklist.blank? + return !skip_with_blank_lists if allowlist.blank? && blocklist.blank? email_domain = email.split("@").last - email_domain.blank? || - ( - (allowlist.all? { |name| name != email_domain }) && - (blocklist.blank? || blocklist.any? { |name| name == email_domain }) - ) + + raise SkipItem if blocklist.present? && !included_in_list?(email_domain, blocklist) + raise SkipItem if allowlist.present? && included_in_list?(email_domain, allowlist) + + !included_in_list?(email_domain, allowlist) && (blocklist.blank? || included_in_list?(email_domain, blocklist)) end private + def included_in_list?(domain, list) + list.any? { |name| name == domain } + end + def current_rules @current_rules ||= (AwesomeConfig.find_by(var: :users_autoblocks, organization:)&.value || []) end @@ -68,6 +74,8 @@ def rule_value(rule) return rule["weight"] if (rule["application_type"] == "positive" && positive) || (rule["application_type"] == "negative" && !positive) + 0 + rescue SkipItem 0 end