Updating "username" for a user on mattermost server is returning "Field 'username' must be set through user's login provider."

I am currently facing an issue with updating a Mattermost user’s username using the API. Below is the function that updates the username .

     function update_mattermost_username($user_id,$user_name){
        $mattermost_user_id = $this->get_mattermost_user($user_id,get_userdata( $user_id));
        $url = MM_URL . '/users/'.$mattermost_user_id.'/patch';
        $data = array(
        $response = $this->talk_to_mm( $url, $data,"PUT");         

and the function that is used to talk to mattermost :slight_smile:

    public function talk_to_mm($url,$data,$method='POST',$custom_headers=null,$custom_token=""){
        $data = json_encode($data);
        $curl = curl_init();
        $token =  $custom_token== ""?MM_PERSONAL_TOKEN:$custom_token;
        $headers = array(
            "Authorization: Bearer ".$token,
            "Content-Type: application/json",
            foreach($custom_headers as $cHeader=>$value){
               array_push($headers,$cHeader.":". $value);
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS =>$data,
            CURLOPT_HTTPHEADER => $headers,
        $response = curl_exec($curl);
        $return_data = null;
        if($response === false)
            $return_data['status'] = 500;
            $return_data['data'] = curl_error($curl);

            $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            $return_data['status'] = $http_code;
            $return_data['data'] = json_decode($response);
        return $return_data;


I can update nickname etc but when updating the username it says:

  "id": "api.user.patch_user.login_provider_attribute_set.app_error",
  "message": "Field 'username' must be set through user's login provider.",
  "detailed_error": "",
  "request_id": "gm7c1oeb8tgu881c3s18atfaxc",
  "status_code": 409

an example of api call

I did cross check about the token and user id i am passing . They both are correct . Do i need to modify this function or from where i can allow updating the username . The login provider here is Gitlab . The version being used in v4 .

The username is getting updated for the wordpress but it is not able to update the same on mattermost server .

Expected behavior
Should update the username after updating it from wordpress .

Thank you in advance for the help .

Hi @tanmaykaushik451 and welcome to the Mattermost forums!

If you’re using Gitlab as a login provider, you will have to change the username in Gitlab. The username cannot be changed in Mattermost if the authentication source is external because otherwise Mattermost would lose the mapping to the authentication provider for this specific user.

Also can you please explain what wordpress has to do with that?

Hey @agriesser. Thanks for the reply.

For our application we have WordPress for the main website which leads to a platform where mattermost is integrated. There is a plugin we are using named username updater which updates the username in WordPress and makes the api call to mattermost after the update is done.

Currently it updates fine in WordPress but when the next api call goes it fails.

Even if we don’t consider WordPress here and look at the screenshot of api testing above. Is there any way I can pass some data in the header related to gitlab, for the api call to update the username via mattermost to gitlab ?

No, you would need to direct this request to GitLab’s API, not to Mattermost.

Thank you for the clarification