A platform for community discussion. Free, open, simple.
FEATURE: Allow admins to disable self-service account deletion
Kane Yorkcdaa60b56bb2
diff --git a/app/models/user.rb b/app/models/user.rbindex 4ae0f86cc08..562291438f0 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -233,7 +233,6 @@ class User < ActiveRecord::BaseLAST_VISIT = -2end- MAX_SELF_DELETE_POST_COUNT ||= 1MAX_STAFF_DELETE_POST_COUNT ||= 5def self.max_password_length@@ -1286,6 +1285,7 @@ class User < ActiveRecord::Basedef has_more_posts_than?(max_post_count)return true if user_stat && (user_stat.topic_count + user_stat.post_count) > max_post_count+ return true if max_post_count < 0DB.query_single(<<~SQL, user_id: self.id).first > max_post_countSELECT COUNT(1)diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex e3b8271e5df..410f545eba1 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1952,6 +1952,7 @@ en:relative_date_duration: "Number of days after posting where post dates will be shown as relative (7d) instead of absolute (20 Feb)."delete_user_max_post_age: "Don't allow deleting users whose first post is older than (x) days."delete_all_posts_max: "The maximum number of posts that can be deleted at once with the Delete All Posts button. If a user has more than this many posts, the posts cannot all be deleted at once and the user can't be deleted."+ delete_user_self_max_post_count: "The maximum number of posts a user can have while allowing self-service account deletion. Set to -1 to disable self-service account deletion."username_change_period: "The maximum number of days after registration that accounts can change their username (0 to disallow username change)."email_editable: "Allow users to change their e-mail address after registration."logout_redirect: "Location to redirect browser to after logout (eg: https://example.com/logout)"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 33501a4df46..018d7390ccd 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -542,6 +542,9 @@ users:client: truedefault: 15min: 1+ delete_user_self_max_post_count:+ default: 1+ min: -1redirect_users_to_top_page: trueprioritize_username_in_ux:client: truediff --git a/lib/guardian/user_guardian.rb b/lib/guardian/user_guardian.rbindex 26e726437b9..ecdff65838d 100644--- a/lib/guardian/user_guardian.rb+++ b/lib/guardian/user_guardian.rb@@ -62,7 +62,7 @@ module UserGuardianreturn false if user.nil? || user.admin?if is_me?(user)!SiteSetting.enable_sso &&- !user.has_more_posts_than?(User::MAX_SELF_DELETE_POST_COUNT)+ !user.has_more_posts_than?(SiteSetting.delete_user_self_max_post_count)elseis_staff? && (user.first_post_created_at.nil? ||diff --git a/spec/components/guardian/user_guardian_spec.rb b/spec/components/guardian/user_guardian_spec.rbindex 12fbdd02a21..e8708435494 100644--- a/spec/components/guardian/user_guardian_spec.rb+++ b/spec/components/guardian/user_guardian_spec.rb@@ -303,6 +303,30 @@ describe UserGuardian doFabricate(:post, user: user, topic: topic)expect(guardian.can_delete_user?(user)).to eq(false)end++ it "isn't allowed when site admin blocked self deletion" do+ expect(user.first_post_created_at).to be_nil++ SiteSetting.delete_user_self_max_post_count = -1+ expect(guardian.can_delete_user?(user)).to eq(false)+ end++ it "correctly respects the delete_user_self_max_post_count setting" do+ SiteSetting.delete_user_self_max_post_count = 0+ expect(guardian.can_delete_user?(user)).to eq(true)++ Fabricate(:post, user: user)++ expect(guardian.can_delete_user?(user)).to eq(false)+ SiteSetting.delete_user_self_max_post_count = 1+ expect(guardian.can_delete_user?(user)).to eq(true)++ Fabricate(:post, user: user)++ expect(guardian.can_delete_user?(user)).to eq(false)+ SiteSetting.delete_user_self_max_post_count = 2+ expect(guardian.can_delete_user?(user)).to eq(true)+ endendcontext "for moderators" do