0

我正在为 RBAC 使用 CPHPAuthmanager。我不知道我的代码有什么问题?为什么每当有人登录时它不分配角色,除了正常?

//protected/config/app.php

<?php
  return array(
'userTypes' => array(
    'normal' => 0,
    'admin' => 1,
    'student' => 2,
    'teacher' => 3

),
'userTypesArray' => array(
    0 => 'normal',
    1 => 'admin',
    2 => 'student',
    3 => 'teacher'
),
    );
?>

//受保护的/数据/auth.php

<?php
  return array(
'admin' => array(
        'type' => 2,
        'description' => '',
        'bizRule' => NULL,
        'data' => NULL
    ),

'student' => array(
        'type' => 2,
        'description' => '',
        'bizRule' => NULL,
        'data' => NULL
    ),

'teacher' => array(
        'type' => 2,
        'description' => '',
        'bizRule' => NULL,
        'data' => NULL
    ),

'normal' => array(
        'type' => 2,
        'description' => '',
        'bizRule' => NULL,
        'data' => NULL
    )
 );

?>

//protected/components/UserIdentity.php

if(count($users) == 0)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    elseif ($users[0]->password!==$this->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        Yii::app()->authManager->clearAuthAssignments();
        $this->errorCode=self::ERROR_NONE;
        $this->setState('rol', $users[0]->type);
        if($users[0]->type == Yii::app()->params['app']['userTypes']['admin'])
            Yii::app()->authManager->assign('admin', $users[0]->UId);
        elseif($users[0]->type == Yii::app()->params['app']['userTypes']['student'])
            Yii::app()->authManager->assign('student', $users[0]->UId);
        elseif($users[0]->type == Yii::app()->params['app']['userTypes']['teacher'])
            Yii::app()->authManager->assign('teacher', $users[0]->UId);
        else
        {
            Yii::app()->authManager->assign('normal', $users[0]->UId);
        }
        Yii::app()->authManager->save();
    }
    return !$this->errorCode;

//配置/主

$auth=Yii::app()->authManager;

$bizRule='return !Yii::app()->user->isGuest;';

$auth->createRole('normal', 'Normal User', $bizRule);

$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "admin") : false;';
$auth->createRole('admin', 'System Admin', $bizRule);

$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "student") : false;';
$auth->createRole('student', 'Student', $bizRule);

$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "teacher") : false;';
$auth->createRole('teacher', 'Teacher', $bizRule);

//控制器/一些控制器

public function accessRules()
{
return array(
            array('allow', 
            'actions'=>array('index', 'view', 'create', 'update', 'admin','delete'),
            'roles'=>array('admin'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

请帮助我,在此先感谢。:)

4

1 回答 1

0

在 config/main components 部分定义如下。

'authManager' =>array(
   ...
   'defaultRoles' => array('normal', 'admin', 'student', 'teacher'),
),

然后用 bizrules 创建这样的角色

$auth=Yii::app()->authManager;

$bizRule='return !Yii::app()->user->isGuest;';
$auth->createRole('normal', 'Normal User', $bizRule);

$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "admin") : false;';
$auth->createRole('admin', 'System Admin', $bizRule);

$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "student") : false;';
$auth->createRole('student', 'Student', $bizRule);

$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "teacher") : false;';
$auth->createRole('teacher', 'Teacher', $bizRule);

$auth->save();

注意我没有使用'role' 我使用'rol' 而是我们现在将在UserIdentity 中定义。在 UserIdentity 中获取记录的用户数据后,只需使用以下代码将 rol 状态设置为用户对象:

$this->setState('rol', $user->type); //can be 'normal', 'admin', 'student', 'teacher'
于 2013-04-10T23:18:56.833 回答