Discourse 内での 0Auth2 ユーザー連携

たまに、ユーザーがログインを試みるたびに新しい Discourse アカウントのセットアップを求められるという問題が発生することがあります。これは、以前からあるアカウントが久しぶりに Discourse にログインした場合や、ユーザー名とパスワードで登録していたものが、現在は OAuth でのログインを試みている場合に頻繁に起こります。過去にはこの問題を解決するために、Discourse インスタンス内の Rails コンソールから以下のコマンドを実行し、Auth0 OAuth2 資格情報を Discourse のユーザー ID にリンクさせていました。

PluginStore.set('oauth2_basic', 'oauth2_basic_user_google-apps|tony.danza@aol.com', {"user_id":7235})

しかし、どうやらこの設定が保存されなくなったようです。以前は Ok が返っていたところ、現在は =>True が返っています。また、以前は考えられなかったことに、pry プロンプトが表示される状態になっています。Pry を終了すると、Rails コンソールからも退出してしまいます。

何が起きているのかご存知でしょうか?よろしくお願いいたします。

使用しているプラグイン:GitHub - discourse/discourse-oauth2-basic: A basic OAuth2 plugin for use with Discourse · GitHub

That command is outdated, now the plugin uses the user associations table.

Can you share an example of the command that I will now run @Falco in replacement of it? Thank you for you help :bowing_man:

Following up for the latest command @Falco, thanks!

Any update or insight on the command @Falco?

This file shound give you an idea of how to construct the command you need:

Sorry for the delay, I had to upgrade discourse before I tried again. I gave the change a shot but didn’t experience success. Below is the example of what I have since tried that return true.

[1] pry(main)> PluginStore.set('oauth2_basic', 'user_associated_accounts_google-apps|tony.danza@gmail.com', {"user_id":1234})
=> true

Following up on this topic, thanks for the help!

Damn you pinged me 7 times while I was on vacation…

Instead of using the PluginStore.set method, you need to create a new UserAssociatedAccount, like for example:

UserAssociatedAccount.create!(provider_name: 'oauth2_basic', provider_uid: 'google-apps|tony.danza@aol.com', user_id: 7235)

Sorry about that @Falco, we too have had a hard time when having Community members go on vacation and not being able to keep everyone in the loop. I hope you had a great vacation and I apologize.

I will give your suggestion a go tomorrow and share the results. Thank you again for your help :pray:

こんにちは、@Falco さん。この件についてフォローアップです。上記の推奨コマンドを実行しようとすると、以下のようなエラーが表示されます。

root@ip-172-31-20-121-app:/var/www/discourse# rails c
NOTE: Inheriting Faraday::Error::ClientError is deprecated; use Faraday::ClientError instead. It will be removed in or after version 1.0
Faraday::Error::ClientError.inherited called from /var/www/discourse/plugins/discourse-github/gems/2.6.5/gems/octokit-4.14.0/lib/octokit/middleware/follow_redirects.rb:14.
[1] pry(main)> UserAssociatedAccount.create!(provider_name: 'oauth2_basic', provider_uid: 'google-apps|nico@auth0.com', user_id: 1722)
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "associated_accounts_provider_uid"
DETAIL:  Key (provider_name, provider_uid)=(oauth2_basic, google-apps|nico@auth0.com) already exists.
from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.3/lib/patches/db/pg.rb:69:in `async_exec_params'
Caused by PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "associated_accounts_provider_uid"
DETAIL:  Key (provider_name, provider_uid)=(oauth2_basic, google-apps|nico@auth0.com) already exists.

from /var/www/discours