How to create different logins for different modules in Yii framework
In this post I’m going to explain how to create different login screens if you are working with multiple modules in Yii framework. Please read my post about How to work with Yii framework modules.
Today most of the websites have two parts:
Backend – which is used for administration purposes (creating new posts, adding new products etc…). Usually, this part of your website is available only to specific users usually called administrators or content managers.
Frontend – The part of the website which is available to normal users or customers
So let say this is your case and you want to create different login screens for backend and frontend part of the website.
Backend module
First step is to set default login action for every module. You can do it in your module class. In our case, backend
module class will be generated in protected/modules/backend/Backend.php
. In init()
function of your model class add this piece of code:
1 2 3 4 5 6 7 8 9 |
Yii::app()->setComponents(array( 'user'=>array( 'class' => 'CWebUser', // enable cookie-based authentication 'allowAutoLogin'=>true, 'baseUrl'=>Yii::app()->createUrl("backend/user/login"), 'stateKeyPrefix' => '_backend', ), ), false); |
Here we define a new module component called user
(you can change the name of the component as I will do in frontend module), which is defined by CWebUser
class (you can extend this class and put your class name here).
We have defined baseUrl
property of the CWebUser
class. We also defined stateKeyPrefix
, which is the prefix that will be added to every variable in the cookie.
With this code, you configured your modul’s default login action and state key prefix which is first step of login process. Every time when user requests a page in backend, and if user is not logged in, user will be redirected to http://website.com/backend/user/login
which is exactly what we wanted.
To access user component somewhere in the code, you can write:
1 2 |
Yii::app()->user->baseUrl; Yii::app()->user->stateKeyPrefix; |
Front end module
No the same thing you should do for the frontend module.
1 2 3 4 5 6 7 8 9 |
Yii::app()->setComponents(array( 'frontendUser'=>array( 'class' => 'CWebUser', // enable cookie-based authentication 'allowAutoLogin'=>true, 'baseUrl'=>Yii::app()->createUrl("frontend/user/login"), 'stateKeyPrefix' => '_frontend', ), ), false); |
Here we defined a new module component called frontendUser
(I intentionally changed the component name to frontendUser
so it is not user
again), which is defined by CWebUser
class. Now if you want to access this component, you can write the following code:
1 2 |
Yii::app()->frontendUser->baseUrl; Yii::app()->frontendUser->stateKeyPrefix; |
Implement login for different user types
Now, Yii will take care for default login URL’s if you are running backend or frontend module. Next step is to implement user/login action. Here are the steps that I usually take:
- I create
User
controller where I implementlogin
action. Create this controller inside bothfrontend
andbackend
module. The body of the Login action you can copy from the Yii’s default Site controller (protected/controllers/SiteController.php
) - In database I create separate table for
Administrators
and normalCustomers
(Users). - Extend
UserIdentity
class forAdministrator
database table and save it in yourprotected/modules/backend/components/
folder - Extend
UserIdentity
class for User database table and save it underprotected/modules/frontend/components/
folder
Here is example how to properly extend Yii’s UserIdentity class so it works with your database table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class AdminIdentity extends UserIdentity { public function authenticate() { //First we want to find user by username $user=Administrator::model()->findByAttributes(array("username"=>$this->username)); if(!isset($user))//User not found $this->errorCode=self::ERROR_USERNAME_INVALID;//Set username invalid error //We have found a user, now let's check user's password else if($this->password != $user->password)//You should salt your passwords $this->errorCode=self::ERROR_PASSWORD_INVALID; else{//Username and password correct, save some values in session, and set no error $this->errorCode=self::ERROR_NONE; $this->setState("firstName", $user->firstName); $this->setState("lastName", $user->lastName); $this->setState("email", $user->email); } return !$this->errorCode; } } |
So we defined AdminIdentity
class which extends UserIdentity
class which submits Administrator database table for usernames.
Similarly, you can do for User table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class CustomerIdentity extends UserIdentity { public function authenticate() { //First we want to find user by username $user=Customer::model()->findByAttributes(array("username"=>$this->username)); if(!isset($user))//User not found $this->errorCode=self::ERROR_USERNAME_INVALID;//Set username invalid error //We have found a user, now let's check user's password else if($this->password != $user->password)//You should salt your passwords $this->errorCode=self::ERROR_PASSWORD_INVALID; else{//Username and password correct, save some values in session, and set no error $this->errorCode=self::ERROR_NONE; $this->setState("firstName", $user->firstName); $this->setState("lastName", $user->lastName); $this->setState("email", $user->email); } return !$this->errorCode; } } |
Now, you can use classes above in your LoginForm
to properly login user.
http://code-epicenter.com/how-to-create-different-logins-for-different-modules-in-yii-framework/How to create different logins for different modules in Yii frameworkPHPProgrammingTutorialsUncategorizedModules,PHP,Yii frameworkIn this post I'm going to explain how to create different login screens if you are working with multiple modules in Yii framework. Please read my post about How to work with Yii framework modules. Today most of the websites have two parts: Backend - which is used for administration purposes (creating new...Amir DuranAmir Duranamir.duran@gmail.comAdministratorAmir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.Code Epicenter