Rake task for merging users

I have spent some time using this super handy user merge rake command combined with @gerhard’s rails commands to remove secondary emails. Thank you both.

These commands have become an important and handy part of my workflow to bring back defunct users who have become unreachable, or to invite people who attend or apply for our events to join our community. Often there is a need to change the email address of the existing account, while retaining the correspondence with a new, staged user.

I do share @pfaffman’s challenge.

The answer for now seems to be to merge the user, delete the secondary email address of the remaining user using @gerhard’s rails commands, then change the email address via the web interface. (in my case it’s via wordpress SSO). Presto, done. But it’s three commands involving some copy pasting and is a bit time consuming.

Some suggestions follow. Curious to hear reactions.

How about a users:swapemail command to run before the merge?

rake users:swapemail[user1,user2]
rake users:merge[user1,user2] 

Add ability to edit secondary email addresses via the admin UI?

I have SSO on my site so can’t use this, but for those who don’t it would be useful to be able to edit the primary and secondary email addresses directly via the user admin.

Add ability to update secondary email via SSO?

This might already be possible via API, but support for a secondary email field would need to be added to the SSO provider, in my case wordpress.

Move primary email to secondary when primary email is changed?

Perhaps a simpler, more sustainable long-term solution that doesn’t require alot of other changes would be to simply change it so when a user or admin (via web or SSO) changes the primary email, it moves the old email to the secondary email field first. This could be enabled with an admin site setting.

8 Likes

Is there any risk to merging someone’s shadow/anonymous account into their account? I just did it and it seemed to work fine.

This seems incredibly helpful!

I’m trying to do the following:

root@snap-forum-app:/var/www/discourse# rake users:merge ['blob', 'tjvr'] --trace
** Invoke users:merge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute users:merge
ERROR: Expecting rake users:merge[source_username,target_username]

Any ideas on how to proceed? I’ve got SSO enabled, and am trying to merge the non-sso user into the SSO user. I have a few accounts which existed prior to enabling SSO on the forum.

Try removing the space after the comma. You also shouldn’t need quotes around the username.

6 Likes

Ah, thanks! Quotes break it… I originally tried no spaces, and no quotes, but somehow missed no quotes and no spaces… :frowning:

So, now when I run this I encounter the following exception, PostsExistError. This seems a bit odd to me, since isn’t goal to move all posts? I also ran the change ownership task for good measure and I got the same result. The admin page says this user has “-6” posts created, and none listed on the user replies view.

root@snap-forum-app:/var/www/discourse# rake users:merge[blob,tjvr] --trace
** Invoke users:merge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute users:merge
rake aborted!
UserDestroyer::PostsExistError: UserDestroyer::PostsExistError
/var/www/discourse/app/services/user_destroyer.rb:18:in `destroy'
/var/www/discourse/app/services/user_merger.rb:360:in `delete_source_user'
/var/www/discourse/app/services/user_merger.rb:22:in `merge!'
/var/www/discourse/lib/tasks/users.rake:48:in `block in <top (required)>'

Edit: Well, I went into the console and just updated the owner of 5 posts that seems to be assigned to blob’s account. Not sure what happened, but after that the merge was successful. :slight_smile:

I’m running into trouble with this command and my SSO setup, which appears to fail silently if a user changes their email address to an address that exists already in the secondary email field. The email address changes in wordpress and all appears to be well, but it does not change in discourse.

When I run @gerhard’s rails commands to remove secondary emails first, then change the email address in wordpress, it changes successfully in discourse.

The primary email address is not an issue, because if someone tries to change their email address to an address that already exists in wordpress, it doesn’t let them.

Does anyone have any suggestions? Is it possible to disable the secondary email address field, or to have the API ignore it or use it better? E.g. if the email address exists as secondary, just move what was primary to secondary and move what was secondary to primary?

1 Like

If you’re using Wordpress as the authority for SSO what benefit is there to storing a second email in Discourse?

right-o. unless I store it in wordpress too for the same reasons. but I don’t know how to prevent this rake task from storing a secondary email when merging users.

If secondary emails are an issue can’t they just be removed?

1 Like

Yes, that is one easy solution. Is there a rake task someone can share to list all the users with secondary emails, and to delete their secondary emails?

That said, I’d prefer to see discourse evolve to take advantage of a cool new feature like secondary emails rather than have to find ways to work around it. Other people are likely to bump up against this issue too at some point and wonder why the email address is not updating in discourse when updated in wordpress.

I don’t think there’s a rake task, but you could do it from the rails console. There should be some example queries about secondary emails for data explorer and some sample .update_all code for other rails examples. It’ll take more time than I can spare here today. If you’re stuck and want to pay, my contact info is in my profile.

See Additional email address per user account support

Here’s what you’re looking for: Additional email address per user account support

3 Likes

I’ve written a plugin which makes it easier to run this task:

8 Likes

So, if you’re using SSO on your site is it not possible to complete the rake merge task? The fine folks over at Communiteq (formerly DiscourseHosting) say they get this error message:

ActiveRecord::RecordNotSaved: Failed to save the new associated primary_email
/var/www/discourse/app/services/user_merger.rb:359:in `delete_source_user'

This also causes issues when trying to use the plugin from @Dannii because it looks like his process is trying to do this step first.

Any suggested workarounds for merging users for SSO sites? What we have happen is that a person changes from one organization to another and so they get a new account through the SSO process (they have a new email), but since it is the same person, we’d like to be able to merge the accounts once they’ve established themselves at their new location.

Disable SSO, merge, then re-enable SSO?

Thanks, I gave it a shot (disable/merge/enable) but no luck. Maybe after disabling a restart is required? Which I’m assuming would mean I couldn’t login any longer since I’m an SSO user too.

I don’t get this. I use sso using the wp_discourse Wordpress plugin and have no problem running this rake task. I then delete the Wordpress user that got merged.

3 Likes

I have a specific case I need some help with, similar one mentioned above.

I want to merge two user accounts, but:

  • Account #1 has the old username that the user would like to keep.
  • Account #2 has the user’s new email. (The user does not have access to the email in Account #1, because it’s an institutional account.)

Can I use the “Merge” UI to combine the two accounts so that the resulting account has the old username but the new email?

I don’t think that’s doable through the Admin, but it can be accomplished with an additional step:

Merge via Admin
Change email for that user via the rails console:

cd /var/discourse
./launcher enter app
rails c
UserEmail.find_by(email: "old@example.com").update(email: "new@example.com")
7 Likes

During the merge the new email was added a Secondary Email. I wasn’t quite sure how this might interface with my custom SSO solution. So I removed the secondary email first (following these steps), and then followed your suggestion.

Thanks @omarfilip!

7 Likes