復制代碼 代碼如下:
public function filters()
{
return array(
'accessControl',// 實現CRUD操作的訪問控制。
'postOnly + delete',
);
}
public function accessRules()//這里就是訪問規則的設置。
{
return array(
array('allow',// 允許所有用戶執行index,view動作。
'actions'=>array('index','view'),
'users'=>array('*'), <span></span>
),
array('allow',// 只允許經過驗證的用戶執行create, update動作。
'actions'=>array('create','update'),
'users'=>array('@'),// @號指所有注冊的用戶
),
array('allow',// 只允許用戶名是admin的用戶執行admin,delete動作
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),//admin就是指用戶名是admin的用戶,以硬編碼的形式分配用戶權限。
array('deny',// 拒絕所有的訪問。
'users'=>array('*'),
),
);
}
復制代碼 代碼如下:
<strong><?php
// this file must be stored in:
// protected/components/WebUser.php
class WebUser extends CWebUser {
// Store model to not repeat query.
private $_model;
// Return first name.
// access it by Yii::app()->user->first_name
function getFirst_Name(){
$user = $this->loadUser(Yii::app()->user->id);
return $user->first_name;
}
// This is a function that checks the field 'role'
// in the User model to be equal to 1, that means it's admin
// access it by Yii::app()->user->isAdmin()
function isAdmin(){
$user = $this->loadUser(Yii::app()->user->id);
if ($user==null)
return 0;
else
return $user->role == "管理員";
}
// Load user model.
protected function loadUser($id=null)
{
if($this->_model===null)
{
if($id!==null)
$this->_model=User::model()->findByPk($id);
}
return $this->_model;
}
}
?></strong>
復制代碼 代碼如下:
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'class'=>'WebUser',
),
復制代碼 代碼如下:
public function accessRules() //這里就是訪問規則的設置。{
return array(
array('allow',// 允許所有用戶執行index,view動作。
'actions'=>array('index','view'),
'users'=>array('*'),//*號標識所有用戶包括注冊的、沒注冊的、一般的、管理員級的
),
array('allow',// 只允許經過驗證的用戶執行create, update動作。
'actions'=>array('create','update'),
'users'=>array('@'),// @號指所有注冊的用戶
),
array('allow',// 只允許用戶名是admin的用戶執行admin,delete動作
'actions'=>array('admin','delete'),
'expression'=>'yii::app()->user->isAdmin()',
//這樣只有標識為“管理員”的用戶才能訪問admin,delete動作
),
array('deny', // 拒絕所有的訪問。
'users'=>array('*'),
),
);
新聞熱點
疑難解答