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})

但问题似乎是该操作不再生效,实际上它返回的是 =>True,而以前会返回 Ok。我现在处于 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.

1 个赞

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:

1 个赞

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: 继承 Faraday::Error::ClientError 已弃用;请使用 Faraday::ClientError。它将在 1.0 版本或之后被移除。
Faraday::Error::ClientError.inherited 调用自 /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: 错误:重复键值违反了唯一约束 "associated_accounts_provider_uid"
详情:键 (provider_name, provider_uid)=(oauth2_basic, google-apps|nico@auth0.com) 已存在。
来自 /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'
由 PG::UniqueViolation 引起:错误:重复键值违反了唯一约束 "associated_accounts_provider_uid"
详情:键 (provider_name, provider_uid)=(oauth2_basic, google-apps|nico@auth0.com) 已存在。

来自 /var/www/discours