Extending Yii Web User Functionality

Over the past couple of months, we have started using Yii on a number of projects. It’s a great foundation for almost any project, allowing for easy extensibility, it is generally a well thought out MVC framework that has been quick to pickup.

Built into Yii is a secure authorised user identity system, AKA a user login. Having this built in is already a massive help to any and all projects as it is already very secure. In most cases, we have extended this to link in with FileMaker to check against someones details or to retrieve some information about the person.

We have also extended it in 1 other way; extending information stored for the user in the browser.

By default, the system gives us 2 calls we can make to retrieve information regarding the user; “getID()” and “getName()”. To start with, these are both pretty useless as they both return the user’s username. Instead, we’d obviously want an actual ID, the users full name and also the username, this gives us the ability to do database calls with the ID and also have both the username and full name for use on the front end views.

As an example, our database will consist of 4 fields; id, email, password, and fullName.

To extend and change these, we first need to open the UserIdentity.php file in the website components folder. This will have our basic setup in place straight from installation. It should look something similar to this:


/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
 /**
  * Authenticates a user.
  * Authenticate against our Database
  * @return boolean whether authentication succeeds.
  */
    private $_id;
    private $_username;
    public function getName()
    {
       return $this->_username;
    }
    public function getId()
    {
       return $this->_id;
    }
    public function authenticate()
    {
        $user= Users::model()->find('LOWER(username)=?', array(strtolower($this->username)));
        if($user === null)
         {
             $this->errorCode= self::ERROR_UNKNOWN_IDENTITY;
         }
         elseif($user->password !== md5($this->password))
         {
             $this->errorCode= self::ERROR_PASSWORD_INVALID;
         }
         else
         {
             $this->_id = $user->id;
             $this->_username = $user->email;
             $user->last_login_time=new CDbExpression("NOW()");
             $user->save();
             $this->errorCode= self::ERROR_NONE;
         }
             return !$this->errorCode;
     }
} 

To add the new user detail areas that we want, first of all we need to set a private variable. In this demo, we will add a new field for the users actual name.

Within the class, we set the private variable like so

 private $_fullName;

This means that the variable is only accessible within that class and within that file. This variable is used to set the data and be called in another function.

To set the data within that variable, we just need to find where the other variables are being set and at it at that point. In our current file, the verification process is complete on line 35 and variables are being set, this is where we will add the setting of the new variable.

$this->_id = $user->id;
$this->_username = $user->email;
$this->_fullName = $user->fullName;
$user->last_login_time=new CDbExpression("NOW()");
$user->save();
$this->errorCode= self::ERROR_NONE; 

Lastly, so that the rest of the system can read this, we will need to generate a function which will be able to return the private variable. We can very simply copy the already existing getID() function and just change the names.

public function getFullName()
{
   return $this->_fullName;
} 

Now everything is in place and we can start to call out the user’s full name within the components and views using a very simple line of code

Yii::app()->user->fullName;

This is greatly helpful for saving extra little pieces of data which are used in a large amount of places across a website. It will save you having to do a database lookup every single time you only want a user’s name or could be used for other pieces of information such as a users image URL.

Rhys Stewart

Rhys Stewart

Rhys is our Apprenticeship Web Developer who joined December 2012. He has always been interested in IT and creating websites and has built a small number of websites for friends and family. Rhys is currently enrolled in an advanced level apprenticeship with Linear Blue & Reading College

More Posts - Website

Leave a Reply