We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22827
    • 129 Posts
    The manager service does not seem to update the username if you change it.

    As the username is independent of the user id and internalKey, it seems that this should be updateable. I have had a quick look at the source, but if anyone else has already done this, it would be good to hear the approach.

    Cheers,

    Paul
      • 22827
      • 129 Posts
      Ok, I added the following to SaveUserProfile (1.3.1):

      		// Update username - Paul
        
      		//escape proposed username
      		$newusername = $modx->db->escape($_POST['username']);
      
      		//Check if used
      
      		$newusernameres = $modx->db->select("username",$web_users,"`username`='".$newusername."'");
      		if(!$modx->db->getRecordCount($newusernameres)) // Not already used
      		{
      		  // Get current username
      		  $usernameres = $modx->db->select("username",$web_users,"`id`='".$internalKey."'");
      	  
      		  if($modx->db->getRecordCount($usernameres)) // If this user exists - how could it not?
      		  {
      		      $userrow = $modx->db->getRow($usernameres);
      		      if($userrow['username'] != $newusername)
      		      {
      			$modx->db->update("`username`='".$newusername."'",$web_users,"`id`='".$internalKey."'");
      		      }
      		  }
      		}
      		else
      		{
      		  $this->FormatMessage($this->LanguageArray[7]);
      		  return;
      		}
      


      You can add it where you like - it bails if the username already exists that you are trying to set to, so if you’d prefer to test this before any other updates, then put it at the beginning. I put it last.

      I expect that the SaveUserProfile function will be updated at some point, so that all the checks happen first, and then all the updates happen once all the checks are complete.