In this WIKI you can learn how to create front and admin login form, using database.
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.
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.
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.'); } }
Path: components/UserIdentity.php
Same like LoginForm, adding new member to the class.
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; } } }
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! :)
Total 6 comments
plz provide the source code of front controller and back controller
@redguy: Thanks for info.
You should rather use built in user id this way:
This way you can access it with Yii::app()->user->id
Without this code - Yii::app()->user->id will hold user login...
I just missed that, wroking now. +1 fot this.
@kiran: Once you follow http://www.yiiframework.com/wiki/63/organize-directories-for-applications-with-front-end-and-back-end-using-webapplicationend-behavior then make sure you define name field in config/front.php & config/back.php file as shown below. Sorry i don't know the reason but once i have added both different name it solves session issue.
On following above structure it overwrite front/backend logged user.
Leave a comment
Please login to leave your comment.