FEU Change Username

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
janb

FEU Change Username

Post by janb »

From time to time I have users who want to change their username (or email as username) in FEU.
Actually no problem, but it would be nice if users could fix this on their own.
My desire is to have this implemented in FEU and use some prettier code but this does the job for now.
Want's comments on this UDT and I assume there will be some :)

Personally I use this UDT only when Email address is username, otherwise I don't allow users to change their username.

How to use it?
Create an UDT and name it "changemyusername" and paste in the code below.
Put this UDT in an content page, name it changemyusername, uncheck "Show in Menu" and put the tag {changemyusername} in the content part.
Make a link to the page in FEU "Logout Template" like this:
<a href="{cms_selflink href='changemyusername' }">[Change My Username]</a>

JanB

Code: Select all

/** This User Defined Tag helps FrontEndUsers to change their FEU Username **/
/** Most of the code is loaned from Calguy's excellent FEU module, thank's **/
/** By JanB - for CMSMS **/

global $gCms;
$feu =& $gCms->GetModuleInstance('FrontEndUsers');
$manager =& $gCms->GetHierarchyManager();

$node =& $manager->GetNodeByAlias($gCms->variables['page_id']);
$content =& $node->GetContent();
$pageurl = $content->GetURL();

//echo '<link rel="canonical" href="'.$pageurl.'" />';

$sitename = $smarty->get_template_vars('sitename');
$usenamefldlength = $feu->GetPreference("usernamefldlength");
$passwordfldlength = $feu->GetPreference("passwordfldlength");

$error = array();
$message = array();

if ($feu->LoggedIn())
  {
    $uid = $feu->LoggedInID();
    $email = $feu->GetEmail($uid);
    $userinfo = $feu->GetUserInfo($uid);
    $oldusername = $userinfo[1]['username'];

    /*** Get the submitted values from Change Username Form ***/

    if ($_POST['submit'] == 'Submit' && $_POST['action'] == 'changeusername')
    {
      $newusername = $_POST['newusername'];
      if (!$feu->IsValidUsername($newusername))
      {
        $error[] = $feu->Lang('error_username_exists');
      }
      else
      {
        $cmsmailer =& $gCms->GetModuleInstance('CMSMailer');

        $code = $feu->GenerateRandomPrintableString();
        $feu->SetUserTempCode( $uid, $code );

        $link  =  '<a href="'.$pageurl;
        $link .= '&action=verifyusername';
        $link .= '&oldusername='.$oldusername;
        $link .= '&newusername='.$newusername;
        $link .= '&code='.$code;
        $link .= '" >Please click this link to complete your username change request</a>';

        $body  = '<p>You have requested a new username at site '.$sitename.'.<br />Please click on the link below for completing your request.</p>';
        $body .= '<p>Excisting Username: '.$oldusername.'<br />';
        $body .= 'Your new Username: '.$newusername.'<br />';
        $body .= 'Code: '.$code.'</p>';
        $body .= $link.'<br />';

        $cmsmailer->AddAddress( $email );
        $cmsmailer->SetBody( $body );
        $cmsmailer->IsHTML(true);
        if( strip_tags($body) != $body )
        {
          $cmsmailer->IsHTML(true);
        }
        $cmsmailer->SetSubject('Change Username Request');

        if (!$cmsmailer->Send())
        {
          $error[] = $cmsmailer->ErrorInfo;
        }
        else
        {
          $message[] = 'An Email is sent to your registered Email address.<br />Please check your inbox for further instructions.';
          $feu->Logout($uid);
        }
      } // check username
    } // POST

    /*** Change Username Form ***/

    echo '
    <form id="changeusername" method="post" action="'.$pageurl.'" class="cms_form">
    <input type="hidden" name="action" value="changeusername"/>
    <input type="hidden" name="oldusername" size="'.$usenamefldlength.'" value="'.$oldusername.'"/>
    '.$feu->Lang('prompt_username').': <br />
    <input type="text" name="newusername" size="'.$usenamefldlength.'"/><br />
    <input type="submit" name="submit" value="Submit"/>
    <input type="submit" name="cancel" value="Cancel"/>
    </form>
    ';

  } //If LoggedIn

/*** Verify Username Form ***/

if (isset($_GET['code']) && isset($_GET['newusername']) && isset($_GET['oldusername']))
{
  $oldusername = $_GET['oldusername'];
  $newusername = $_GET['newusername'];
  $code = $_GET['code'] ;

  echo '
  Please enter password and code to complete the change of username from<br /><strong>'.$oldusername.'</strong> to <strong>'.$newusername.'</strong><br />
  <form id="verifyusername" method="post" action="'.$pageurl.'" class="cms_form">
  <input type="hidden" name="action" size="20" value="verifyusername"/>
  <input type="hidden" name="newusername" size="'.$usenamefldlength.'" value="'.$newusername.'"/><br />
  '.$feu->Lang('prompt_username').'<br />
  <input type="text" name="oldusername" size="'.$usenamefldlength.'" value="'.$oldusername.'"/><br />
  '.$feu->Lang('prompt_password').'<br />
  <input type="password" name="password" size="'.$passwordfldlength.'"/><br />
  '.$feu->Lang('prompt_code').'<br />
  <input type="text" name="code" size="20" value="'.$code.'"/><br />
  <input type="checkbox" name="logmein" value="logmein" checked/>
  &nbsp;Log me in with my new username after i hit Submit<br />
  <input type="submit" name="submit" value="Submit"/>
  <input type="submit" name="cancel" value="Cancel"/>
  </form>
  ';
}

/*** Get the submitted values from Verify Username Form ***/

if ($_POST['submit'] == 'Submit' && $_POST['action'] == 'verifyusername' )
{
  $uid = $feu->GetUserID($_POST['oldusername']);
  $checkcode = $feu->GetUserTempCode( $uid );
  $password = $_POST['password'];
  $oldusername = $_POST['oldusername'];
  $newusername = $_POST['newusername'];
  $logmein = $_POST['logmein'];

  if (!$feu->CheckPassword($oldusername,$password,$groups,$md5pw ) || !$checkcode[0] )
  { 
    $error[] = 'Wrong password or wrong code entered, process terminated.<br />Please login to repeat the change username request.';
  }
  else
  {
    $result = $feu->SetUser($uid, $newusername, $password);
    $feu->RemoveUserTempCode( $uid );
    if (!$result[0]) 
    {
       $error[] = 'Something went wrong, could not set new username. Please contact administrator.';
    }
    else
    {
    $message[] = 'Username successfully changed!<br />';
    if ($logmein)
    {
      $feu->Login($newusername,$password);
      $message[] = 'You were logged in automatically with your new username.';
    }
    }
  }
}

if ($message)
{
  foreach ($message as $messages) 
  {
    echo $messages;
  }
}

if ($error)
{
  foreach ($error as $errors) 
  {
    echo '<font color="red">'.$errors.'</font>';
  }
}
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: FEU Change Username

Post by Nullig »

Excellent post.

Nullig
Post Reply

Return to “Tips and Tricks”