FIX: Allow to change ownership on deleted users' posts

This commit is contained in:
Kane York 2015-07-15 15:22:01 -07:00
parent 80bf7bed21
commit 61ea8c6f72
6 changed files with 32 additions and 5 deletions

View File

@ -1,14 +1,15 @@
import Presence from 'discourse/mixins/presence';
import SelectedPostsCount from 'discourse/mixins/selected-posts-count';
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import ObjectController from 'discourse/controllers/object';
// Modal related to changing the ownership of posts
export default ObjectController.extend(Presence, SelectedPostsCount, ModalFunctionality, {
export default Ember.Controller.extend(Presence, SelectedPostsCount, ModalFunctionality, {
needs: ['topic'],
topicController: Em.computed.alias('controllers.topic'),
selectedPosts: Em.computed.alias('topicController.selectedPosts'),
saving: false,
new_user: null,
buttonDisabled: function() {
if (this.get('saving')) return true;
@ -38,7 +39,7 @@ export default ObjectController.extend(Presence, SelectedPostsCount, ModalFuncti
username: this.get('new_user')
};
Discourse.Topic.changeOwners(this.get('id'), saveOpts).then(function(result) {
Discourse.Topic.changeOwners(this.get('topicController.model.id'), saveOpts).then(function(result) {
// success
self.send('closeModal');
self.get('topicController').send('toggleMultiSelect');

View File

@ -400,7 +400,7 @@ class Post < ActiveRecord::Base
return if user_id == new_user.id
edit_reason = I18n.t('change_owner.post_revision_text',
old_user: self.user.username_lower,
old_user: (self.user.username_lower rescue nil) || I18n.t('change_owner.deleted_user'),
new_user: new_user.username_lower
)

View File

@ -14,6 +14,12 @@ class PostOwnerChanger
@post_ids.each do |post_id|
post = Post.with_deleted.find(post_id)
@topic.user = @new_owner if post.is_first_post?
if post.user == nil
post.recover!
@topic.recover! if post.is_first_post?
end
post.topic = @topic
post.set_owner(@new_owner, @acting_user)
end

View File

@ -1248,6 +1248,7 @@ en:
change_owner:
post_revision_text: "Ownership transferred from %{old_user} to %{new_user}"
deleted_user: "a deleted user"
emoji:
errors:

View File

@ -202,7 +202,7 @@ class PostRevisor
USER_ACTIONS_TO_REMOVE ||= [UserAction::REPLY, UserAction::RESPONSE]
def update_post
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id && @post.user_id != nil
prev_owner = User.find(@post.user_id)
new_owner = User.find(@fields["user_id"])

View File

@ -242,6 +242,25 @@ describe TopicsController do
expect(p1.user).not_to eq(nil)
expect(p1.user).to eq(p2.user)
end
it "works with deleted users" do
deleted_user = Fabricate(:user)
t2 = Fabricate(:topic, user: deleted_user)
p3 = Fabricate(:post, topic_id: t2.id, user: deleted_user)
deleted_user.save
t2.save
p3.save
UserDestroyer.new(editor).destroy(deleted_user, { delete_posts: true, context: 'test', delete_as_spammer: true })
xhr :post, :change_post_owners, topic_id: t2.id, username: user_a.username_lower, post_ids: [p3.id]
expect(response).to be_success
t2.reload
p3.reload
expect(t2.deleted_at).to be_nil
expect(p3.deleted_at).to be_nil
expect(p3.user).to eq(user_a)
end
end
end