Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Allow admins to disable self-service account deletion #9334

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ module NewTopicDuration
LAST_VISIT = -2
end

MAX_SELF_DELETE_POST_COUNT ||= 1
MAX_STAFF_DELETE_POST_COUNT ||= 5

def self.max_password_length
Expand Down Expand Up @@ -1286,6 +1285,7 @@ def create_reviewable

def 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 < 0

DB.query_single(<<~SQL, user_id: self.id).first > max_post_count
SELECT COUNT(1)
Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
3 changes: 3 additions & 0 deletions config/site_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ users:
client: true
default: 15
min: 1
delete_user_self_max_post_count:
default: 1
min: -1
redirect_users_to_top_page: true
prioritize_username_in_ux:
client: true
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/user_guardian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def can_delete_user?(user)
return 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)
else
is_staff? && (
user.first_post_created_at.nil? ||
Expand Down
24 changes: 24 additions & 0 deletions spec/components/guardian/user_guardian_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@
Fabricate(: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)
end
end

context "for moderators" do
Expand Down