私はyiiの初心者です。私は、yii でロールを作成するための yii-tutorials に頭を悩ませています。しかし、yiiでロールを作成する方法がわかりません。は、2 つのロールを作成し、それらに異なる権限を付与することを意味しますadmin
。
ユーザーテーブルを作成しましたが、ロールを作成して特権を割り当てる方法がわかりません。助けてくださいstaff
role
前もって感謝します
私はyiiの初心者です。私は、yii でロールを作成するための yii-tutorials に頭を悩ませています。しかし、yiiでロールを作成する方法がわかりません。は、2 つのロールを作成し、それらに異なる権限を付与することを意味しますadmin
。
ユーザーテーブルを作成しましたが、ロールを作成して特権を割り当てる方法がわかりません。助けてくださいstaff
role
前もって感謝します
あなたのcopenents/UserIdentity.phpで
class UserIdentity extends CUserIdentity{
private $_id;
public function authenticate()
{
$record=Members::model()->findByAttributes(array('username'=>trim($this->username)));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5(trim($this->password)))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('username', $record->username);
$this->setState('name', $record->name);
$this->setState('type', $record->role);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
public function setId($id)
{
$this->_id = $id;
}
}
「ロール」として新しい列名を作成できます。メンバータイプ「admin」または「staff」をロール列に設定します。
その行に注意してください。
$this->setState('type', $record->role);
新しいヘルパーファイルを作成します。/protected/helpers/RoleHelper.php
class RoleHelper {
public static function GetRole(){
if (Yii::app()->user->type == "admin"){
//set the actions which admin can access
$actionlist = "'index','view','create','update','admin','delete'";
}
elseif (Yii::app()->user->type = "staff"){
//set the actions which staff can access
$actionlist = "'index','view','create','update'";
}
else {
$actionlist = "'index','view'";
}
return $actionlist;
}
}
コントローラー内->accessRules関数
public function accessRules()
{
return array(
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array(RoleHelper::GetRole()),
'users'=>array('@'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
そして、「application.helpers。*」を/config/main.phpに追加することを忘れないでください
'import'=>array(
'application.models.*',
'application.components.*',
'application.helpers.*',
),
このソースは特に初心者向けです。私は今まで次の方法を使用しています: Simple RBAC in YII
必要な変更を加えながら、与えられた指示に従ってください。
具体例:
WebUser.php (/コンポーネント/WebUser.php)
<?php
class WebUser extends CWebUser
{
/**
* Overrides a Yii method that is used for roles in controllers (accessRules).
*
* @param string $operation Name of the operation required (here, a role).
* @param mixed $params (opt) Parameters for this operation, usually the object to access.
* @return bool Permission granted?
*/
public function checkAccess($operation, $params=array())
{
if (empty($this->id)) {
// Not identified => no rights
return false;
}
$role = $this->getState("evalRoles");
if ($role === 'SuperAdmin') {
return 'SuperAdmin'; // admin role has access to everything
}
if ($role === 'Administrator') {
return 'Administrator'; // admin role has access to everything
}
if ($role === 'Professor') {
return 'Professor'; //Regular Teaching Professor, has limited access
}
// allow access if the operation request is the current user's role
return ($operation === $role);
}
}
それをcomponents/UserIdentity.phpとconfig/main.phpに接続するだけです:
'components' => array(
// ...
'user' => array(
'class' => 'WebUser',
),
以上です..
ログインしたユーザーの役割を確認するには:
Yii::app->checkAccess("roles");
where checkAccess is the name of your function in WebUser...
evalRoles は、アカウントの役割を提供するテーブル内の列であることに注意してください (提供された私のリンクでは、主要な部分のスニペットで使用されている役割という言葉になります)