How to create front and admin side login form

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

  1. Scenario:
  2. Default setup:
  3. LoginForm:
  4. UserIdentity:
  5. Code in action:

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:

I am using this article... 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:

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

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
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

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

Path: Controllers/Back/SiteController.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! :)

4 0
15 followers
Viewed: 99 804 times
Version: Unknown (update)
Category: How-tos
Written by: vibhaJadwani
Last updated by: vibhaJadwani
Created on: Jul 19, 2012
Last updated: 11 years ago
Update Article

Revisions

View all history