where user is yii A component. Need to be defined in protected/config/main.php
Here we do not specify the class name of user. Because user is an instance of the CWebUser class by default in yii.
We have now implemented the user Login verification and logout. But now users can access all actions regardless of whether they are logged in, so in the next step we need to authorize user access. In Yii, user authorization is achieved through Access Control Filter, that is, access control filter. We Take a look at a simple Controller with access control:Php code
The code is as follows:
class AdminDefaultController extends CController
{
public function filters()
{
return array('accessControl');
}
Public function accessRules()
{ ; array('@'),
'deny',
'users' => array('*')
🎜>
We are in filters method Set a specific filter in. We can see that there is an accessControl parameter in the array returned by the filters method.
There is a filterAccessControl method in the CController class:
Php code
Copy code
The code is as follows:
public function filterAccessControl($filterChain)
{ $filter=new CAccessControlFilter;
$filter-> ;setRules($this->accessRules());
$filter->filter($filterChain); } Create a new CAccessControlFilter instance inside, and in When setRules, the parameters returned by the accessRules() method are passed in.
$filter->filter($filterChain) continues to call other filters.
And all specific authorization rules are defined in accessRules Medium:
Php code
Copy code
The code is as follows:
public function accessRules()
{ return array(
array('deny',
'actions'=>array('create', 'edit'), 'users'=>array(' ?' ), ), array('allow', 'actions'=>array('delete'),
'roles'=>array('admin' ),
array('deny', 🎜> ),
); CWebUser inherits a CAdminUser:
Php code
Copy code
The code is as follows:
class CAdminWebUser extends CWebUser
{
public $loginUrl = array('admin/admin/login');
}
We need to place it in components
If it is a global application, pass the components section of protected/config/main.php:
Php code
Copy code The code is as follows:
'user'=>array(
// enable cookie-based authentication
'class' => ; 'CAdminUser',
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
If it is in modules, add the following code in the init method of the module class:
Php code
Copy the code The code is as follows:
$this->setComponents(array(
'adminUser' => array(
'CAdminWebUser',
'allowAutoLogin ' => false,
) )
));
Last calling method
Php code
Copy code The code is as follows:
//Global application
Yii::app()->getComponent('adminUser');
//In module
Yii::app()->controller->module->getComponent('adminUser');
But this is not enough, we also need to modify the Controller's filter, we need Customize a filter to implement verification and authorization of another user
The first step is to customize a filter:
Php code
Copy code The code is as follows:
class CAdminAccessControlFilter extends CAccessControlFilter
{
protected function preFilter($filterChain)
{
$app=Yii::app ();
$request=$app->getRequest();
$user = Yii::app()->controller->module->getComponent('adminUser');
$verb=$request->getRequestType();
$ip=$request->getUserHostAddress();
foreach($this->getRules() as $rule)
{ 🎜> return false; ;
return true; >
Copy code
The code is as follows:
public function filterAccessControl($filterChain)
{
$filter = new CAdminAccessControlFilter();
$ filter->setRules($this->accessRules());
$filter->filter($filterChain);
}
//Here we use a custom filter class to replace Original filter
OK, here we can specify the authorization of adminUser in the accessRules() of this Controller
http://www.bkjia.com/PHPjc/327749.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327749.htmlTechArticle1. Why do I need to add a new user verification: Because I want to build the website backend and frontend in the same yii In the application. But the front desk also contains a member management center. And these two user verifications are complete...