如何在yii中获得或创建角色

如何在yii中获得或创建角色,yii,yii-components,Yii,Yii Components,我是yii的新手。我一直专注于在yii中创建角色的yii教程。但我不明白如何在yii中创造角色。这意味着我想创建两个角色管理员和员工,我想赋予他们不同的权限。 我已经在用户表中创建了一个角色,但我不明白如何创建角色并为其分配权限,请帮助我 在您的copents/UserIdentity.php中提前感谢 class UserIdentity extends CUserIdentity{ private $_id; public function authenticate() { $

我是yii的新手。我一直专注于在yii中创建角色的yii教程。但我不明白如何在yii中创造角色。这意味着我想创建两个角色
管理员
员工
,我想赋予他们不同的权限。
我已经在用户表中创建了一个
角色
,但我不明白如何创建角色并为其分配权限,请帮助我


在您的copents/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;

}

}
在控制器->访问规则功能中

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('*'),
        ),
    );
}
别忘了在/config/main.php中添加“application.helpers.*”

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.helpers.*',
),

在您的copents/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;

}

}
在控制器->访问规则功能中

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('*'),
        ),
    );
}
别忘了在/config/main.php中添加“application.helpers.*”

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.helpers.*',
),

此源代码非常适合初学者。我一直在使用此方法:

在进行所需修改的同时,只需按照给出的说明进行操作

具体例子:

WebUser.php(/components/WebUser.php)

就这样

要检查已登录用户的角色,请执行以下操作:

Yii::app->checkAccess("roles");
where checkAccess is the name of your function in WebUser...
请注意,evalRoles是表中提供帐户角色的一列(在我提供的链接中,这将是主要部分片段中使用的角色一词)


此源代码非常适合初学者。我一直在使用此方法:

在进行所需修改的同时,只需按照给出的说明进行操作

具体例子:

WebUser.php(/components/WebUser.php)

就这样

要检查已登录用户的角色,请执行以下操作:

Yii::app->checkAccess("roles");
where checkAccess is the name of your function in WebUser...
请注意,evalRoles是表中提供帐户角色的一列(在我提供的链接中,这将是主要部分片段中使用的角色一词)


此链接可能会有所帮助:我没有得到一件事,我应该在哪里使用此代码逻辑是,在控制器访问规则中,您首先检查用户是否登录,然后检查他们的角色是什么,并为每个角色类型分配不同的访问规则此链接可能会有所帮助:我没有得到一件事我应该在哪里使用此代码逻辑是,在控制器访问规则中,您首先检查用户是否登录,然后检查他们的角色是什么,并为每个角色类型分配不同的访问规则谢谢Burhan,感谢Burhan提供了这个示例程序。