Rake task for merging users

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 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")
6 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!

6 Likes

I have run into this situation quite a few times. When users are merged, the source user’s primary email address becomes the target user’s secondary email address. When SSO is being used and the sso overrides email setting is enabled, the merged user may be unable to login unless the secondary email is deleted from the Rails console.

I’m wondering if the task should have an option to not set the target secondary email from the source primary email.

10 Likes

Or to have a convenient means to swap them.

9 Likes

I am getting this error, any idea?

cd /var/discourse
./launcher enter app

It is stuck here!

1 Like

Hard to tell with the blurring, but can you double check that there is an open quote ' before the first username? It appears that the console is expecting more input (which may indicate an open string)

10 Likes
> /var/www/discourse# rake users:merge['anon94132497','PavelCherenkovv'] --trace
** Invoke users:merge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute users:merge
rake aborted!
ArgumentError: ArgumentError
/var/www/discourse/app/services/post_owner_changer.rb:12:in `initialize'
/var/www/discourse/app/services/user_merger.rb:63:in `new'
/var/www/discourse/app/services/user_merger.rb:63:in `change_post_owner'
/var/www/discourse/app/services/user_merger.rb:51:in `block in move_posts'
/var/www/discourse/app/services/user_merger.rb:49:in `each'
/var/www/discourse/app/services/user_merger.rb:49:in `move_posts'
/var/www/discourse/app/services/user_merger.rb:13:in `merge!'
/var/www/discourse/lib/tasks/users.rake:50:in `block in <main>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:279:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:279:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:279:in `execute'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:160:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:125:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:110:in `top_level'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:83:in `block in run'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:80:in `run'
bin/rake:13:in `<top (required)>'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => users:merge

I got this error?