Summary
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';
//http://your-mattermost-url.com/api/v4/users/{user_id}/patch
$data = array(
"user_id"=>$mattermost_user_id,
"username"=>$user_name
);
$response = $this->talk_to_mm( $url, $data,"PUT");
}
and the function that is used to talk to mattermost
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",
);
if(!is_null($custom_headers)){
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_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
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);
}else{
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$return_data['status'] = $http_code;
$return_data['data'] = json_decode($response);
}
curl_close($curl);
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 .