Difference between #2 and #3 of How to create front and admin side login form

unchanged
Title
How to create front and admin side login form
unchanged
Category
How-tos
unchanged
Tags
login admin front LoginForm UserIdentity
changed
Content
In this WIKI you can learn how to create front and admin login form, using
database.

Scenario:
--------
As a beginner once you setup default yii project, next step will be how to
change this static login form to dynamic, i.e. username and password comes from
database.

Default setup:
-----
[II am using this
article](http://www.yiiframework.com/wiki/63/organize-directories-for-applications-with-front-end-and-back-end-using-webapplicationend-behavior[this
article...](http://www.yiiframework.com/wiki/63/organize-directories-for-applications-with-front-end-and-back-end-using-webapplicationend-behavior
"this tutorial") to manage project directory structure for front and
admin. Thanks Andy for this WIKI. Follow all the steps and you will have
separate front and admin panels like:

- http://localhost/index.php // Front End
- http://localhost/backend.php // Admin Panel

I am using 2 different models to handle front and admin user database.

1. User
2. AdminUser

LoginForm:
----------

_Path: models/LoginForm.php_

Here i am adding new variable to the class. i.e. userType


~~~
[php]
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;
	public $userType; // added new member

	private $_identity;

	public function __construct($arg='Front') { // default it is set to Front	  
		$this->userType = $arg;
	}
	//==== rest code will be as it is ====
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->userType = $this->userType; // this will pass flag
to the UserIdentity class
			if(!$this->_identity->authenticate())
				$this->addError('password','Incorrect username or password.');
		}
	}
~~~

UserIdentity:
------------

_Path: components/UserIdentity.php_

Same like LoginForm, adding new member to the class.

~~~
[php]
<?php
class UserIdentity extends CUserIdentity
{	
	public $userType = 'Front';
	
	public function authenticate()
	{
		if($this->userType=='Front') // This is front login
		{
			// check if login details exists in database
                       
$record=User::model()->findByAttributes(array('username'=>$this->username));

			if($record===null)
			{ 
				$this->errorCode=self::ERROR_USERNAME_INVALID;
			}
			else if($record->password!==$this->password)            // here I
compare db password with password field
			{ 
				$this->errorCode=self::ERROR_PASSWORD_INVALID;
			}
			else
			{  
				$this->setState('userId',$record->userId);
				$this->setState('name', $record->firstName.' '.$record->lastName);
				$this->errorCode=self::ERROR_NONE;
			}
			return !$this->errorCode;
		}
		if($this->userType=='Back')// This is admin login
		{
			// check if login details exists in database
                       
$record=AdminUser::model()->findByAttributes(array('email'=>$this->username));
 // here I use Email as user name which comes from database
			if($record===null)
			{ 
				$this->errorCode=self::ERROR_USERNAME_INVALID;
			}
			else if($record->password!==base64_encode($this->password)) // let we
have base64_encode password in database
			{ 
				$this->errorCode=self::ERROR_PASSWORD_INVALID;
			}
			else
			{  
				$this->setState('isAdmin',1);
				$this->setState('userId',$record->userId);
				$this->setState('name', $record->name);
				$this->errorCode=self::ERROR_NONE;
			}
			return !$this->errorCode;
		}
	}
}
~~~

Code in action:
------------------

Now all is set, we just need to use LoginForm object in controller files.

_Path: Controllers/Front/SiteController.php_


~~~
[php]

$model=new LoginForm('Front'); // Front side login form which will use 'User'
module
~~~

_Path: Controllers/Back/SiteController.php_


~~~
[php]

$model=new LoginForm('Back'); // Admin side login form which will use
'AdminUser' module
~~~

You may also find some good articles on how to manage user access levels..etc.
But as a beginner i tried this code, It may help you. Share your thoughts and
comments.

Happy Coding! :)
Write new article