From 003b03d939261f22466707bb23ea37c38e2331a5 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 5 Mar 2018 10:02:23 +0530 Subject: [PATCH] allow staff to delete user if posts are 5 or less irrespective of delete_user_max_post_age --- .../admin/controllers/admin-user-index.js.es6 | 10 +++++++++- .../javascripts/discourse/models/user.js.es6 | 8 +++++--- lib/guardian/user_guardian.rb | 2 +- spec/components/guardian_spec.rb | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 b/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 index 770d5588b06..b80dd29cec2 100644 --- a/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 @@ -67,9 +67,17 @@ export default Ember.Controller.extend(CanCheckEmails, { silence() { return this.get("model").silence(); }, deleteAllPosts() { return this.get("model").deleteAllPosts(); }, anonymize() { return this.get('model').anonymize(); }, - destroy() { return this.get('model').destroy(); }, disableSecondFactor() { return this.get('model').disableSecondFactor(); }, + destroy() { + const postCount = this.get('model.post_count'); + if (postCount <= 5) { + return this.get('model').destroy({ deletePosts: true }); + } else { + return this.get('model').destroy(); + } + }, + viewActionLogs() { this.get('adminTools').showActionLogs(this, { target_user: this.get('model.username'), diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index eef39aa8acc..e54caf27395 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -23,14 +23,16 @@ const User = RestModel.extend({ hasPMs: Em.computed.gt("private_messages_stats.all", 0), hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0), hasUnreadPMs: Em.computed.gt("private_messages_stats.unread", 0), - hasPosted: Em.computed.gt("post_count", 0), - hasNotPosted: Em.computed.not("hasPosted"), - canBeDeleted: Em.computed.and("can_be_deleted", "hasNotPosted"), redirected_to_top: { reason: null, }, + @computed("can_be_deleted", "post_count") + canBeDeleted(canBeDeleted, postCount) { + return canBeDeleted && postCount <= 5; + }, + @computed() stream() { return UserStream.create({ user: this }); diff --git a/lib/guardian/user_guardian.rb b/lib/guardian/user_guardian.rb index c9f056b034c..0aef85c9914 100644 --- a/lib/guardian/user_guardian.rb +++ b/lib/guardian/user_guardian.rb @@ -43,7 +43,7 @@ module UserGuardian if is_me?(user) user.post_count <= 1 else - is_staff? && (user.first_post_created_at.nil? || user.first_post_created_at > SiteSetting.delete_user_max_post_age.to_i.days.ago) + is_staff? && (user.first_post_created_at.nil? || user.post_count <= 5 || user.first_post_created_at > SiteSetting.delete_user_max_post_age.to_i.days.ago) end end diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index f96f4e001a0..bd0a8290c81 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -2014,6 +2014,7 @@ describe Guardian do it "is true if user is not an admin and first post is not too old" do user = Fabricate.build(:user, created_at: 100.days.ago) + user.stubs(:post_count).returns(10) user.stubs(:first_post_created_at).returns(9.days.ago) SiteSetting.delete_user_max_post_age = 10 expect(Guardian.new(actor).can_delete_user?(user)).to be_truthy @@ -2025,20 +2026,33 @@ describe Guardian do it "is false if user's first post is too old" do user = Fabricate.build(:user, created_at: 100.days.ago) + user.stubs(:post_count).returns(10) user.stubs(:first_post_created_at).returns(11.days.ago) SiteSetting.delete_user_max_post_age = 10 expect(Guardian.new(actor).can_delete_user?(user)).to be_falsey end end + shared_examples "can_delete_user staff examples" do + it "is true if posts are less than or equal to 5" do + user = Fabricate.build(:user, created_at: 100.days.ago) + user.stubs(:post_count).returns(4) + user.stubs(:first_post_created_at).returns(11.days.ago) + SiteSetting.delete_user_max_post_age = 10 + expect(Guardian.new(actor).can_delete_user?(user)).to be_truthy + end + end + context "for moderators" do let(:actor) { moderator } include_examples "can_delete_user examples" + include_examples "can_delete_user staff examples" end context "for admins" do let(:actor) { admin } include_examples "can_delete_user examples" + include_examples "can_delete_user staff examples" end end