Can I change the user locale through the API or sync_sso


(Jeff Willette) #1

Unofrtunately, I don’t know enough ruby to add a PR myself and I have been looking through the api code to see if I could find anything.

I need to be able to change the user locale based on the locale in my main site. so as far as I can see this is the process that needs to happen.

User changes locale on the main site:

####option 1: Send an API request (if the API is capable) to change the user in discourse. I don’t think the API can do that but I might be mistaken.

####option 2: Run an sso_sync and send the data to the discourse, but unfortunately I do not know the whole user data structure and I don’t know how discourse interprets what it receives. I have tried adding "locale": "en" to my payload that gets urlencoded in python but that does not work.

Any ideas on how to do this?

on a side note

Shouldn’t there be an api endpoint that would let admins change whatever they want in the users from the API? basically be able to GET and see the whole user data structure and then PUT or DELETE to modify it? I wish I could code this up myself and submit a PR but it will be a while before I can learn enough ruby in practice to make it happen


(Simon Cossar) #2

You can change the user locale through the api by making a put request to ("/users/#{username}", params)

It’s possible to figure a lot about how the API works by looking at the code in discourse_api


(Jeff Willette) #3

Thanks for the info. I also found this post which helped me How to reverse engineer the Discourse API

Would I be correct in assuming that since the sync_sso function cannot handle the locale as an input? I am trying to understand the ruby but I can’t quite see what is possible with it. Here is what I can see…

sync_sso uses the AdminDetailedUserSerializer

If I look in the AdminDetailedUserSerializer I see that it looks like the class inherits from some other serializers and it also has_one BasicUserSerializer…How can I tell which attributes I can change from sync_sso? Should I follow the class inheritance? or the has_one includes? or both?


(Simon Cossar) #4

I’m still learning this stuff. For figuring out the SSO login path I start here: discourse/session_controller.rb at master · discourse/discourse · GitHub

Look at the line 96: if user = sso.lookup_or_create_user(request.remote_ip)
That takes you to here:

That calls the method match_email_or_create_user - it’s in the same class. If the user doesn’t exist it creates a new user with the following parameters:

      user_params = {
        email: email,
        name: try_name || User.suggest_name(try_username || email),
        username: UserNameSuggester.suggest(try_username || try_name || email),
        ip_address: ip_address
      }

For existing users there is a function change_external_attributes_and_override that will override a few attributes, but not the locale. It looks like it could be changed to set the locale, but it’s also quite straightforward to set it with the api.


(Jeff Willette) #5

Thanks for your help