Using the API is it possible find users by their sso username

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.

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?

/users/by-external/23

4 Likes

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.