# 使用API是否可以通过其SSO用户名查找用户

**URL:** <https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845>\
**Category:** Support\
**Created:** [2015年七月7日 00:07 UTC](https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845 "2015-07-07T00:07:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eriko](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eriko/32/1915_2.png) [@eriko](https://meta.discourse.org/u/eriko)\
**Post date:** [2015年七月7日 00:07 UTC](https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845/1 "2015-07-07T00:07:05Z")

</div>

I can see in the api that once you have a user you can get their sso information. If you user are using sso you probably have a data source with this name. What you do not have is a source of the username they may have chosen in a discourse instance. This makes it very hard to find users. So it there a way to find a user by their sso username?

Since I moved to docker I have replacing some automation I that ran via adding it as a rake task. It added and disabled users. I do not want to do that anymore and would like the use the API to replace it.

---

<div class="post-metadata">

**Author:** ![eriko](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eriko/32/1915_2.png) [@eriko](https://meta.discourse.org/u/eriko)\
**Post date:** [2015年七月7日 00:44 UTC](https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845/2 "2015-07-07T00:44:04Z")

</div>

In am thinking something like

```
 def show
  @user = User.find_by(username_lower: params[:id])
  if SiteSetting.enable_sso && !@user
    sso_record = SingleSignOnRecord.find_by(external_id: params[:id])
    if sso_record && user = sso_record.user
      @user = sso_record.user
    end
  end
  raise Discourse::NotFound unless @user
  render_serialized(@user, AdminDetailedUserSerializer, root: false)
end

```

The idea is that if sso is enable using the sso records as a back up. As I think this through the more I think this is a bad idea. Hmm how to get the user via their external\_id?

---

<div class="post-metadata">

**Author:** ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)\
**Post date:** [2015年七月7日 20:44 UTC](https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845/3 "2015-07-07T20:44:29Z")

</div>

`/users/by-external/23`

---

<div class="post-metadata">

**Author:** ![eriko](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eriko/32/1915_2.png) [@eriko](https://meta.discourse.org/u/eriko)\
**Post date:** [2015年七月7日 21:22 UTC](https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845/4 "2015-07-07T21:22:30Z")

</div>

With a little tweak @riking that is it.  
/users/by-external/#{sso\_username}.json

The json part is needed as it will not do the search otherwise. This needs to be added to the api gem.

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [2024年六月8日 12:46 UTC](https://meta.discourse.org/t/using-the-api-is-it-possible-find-users-by-their-sso-username/30845/5 "2024-06-08T12:46:08Z")

</div>

此主题已在 3259 天后自动关闭。不再允许回复。
