3

我用 YII 框架创建了一个 webapp。我使用了权限模块。我创建了 3 个角色。他们是

  1. 创作者
  2. 审稿人
  3. 导向器

“创作者”有一些动作,“评论者”有一些动作。而“导演”是“创作者”和“评论者”的孩子。

问题是,如果我检查一个角色(例如创建者),它将允许它是子角色(例如“导演”)。

例如,如果我如下选中“Creator”角色,则该语句也必须适用于“Director”角色。

if(Yii::app()->user->checkAccess('Creator'))
{
  statement....;
}

我应该怎么做?

4

2 回答 2

7

例如:

$role = Rights::getAssignedRoles(Yii::app() -> user -> Id);
            foreach ($role as $role)
                $role -> name;
            if ($role -> name == 'Creator'|| $role -> name == 'Director')
               //do what you need

checkAccess() 检查项目而不是角色的访问权限。例如:

if(Yii::app()->user->checkAccess('Creator'))

检查当前角色对 AuthItem 'Creator' 的访问权限。返回真/假。

另一个使用示例 - 不同角色的主菜单:

$role = Rights::getAssignedRoles(Yii::app() -> user -> Id);
            foreach ($role as $role)
                $role->name;
            if ($role->name == 'Manager' or Yii::app()->user->isSuperuser) {
                $this->widget('ext.cssmenu.CssMenu', array('items' => array(array('url' => array('/user/general/index'), 'label' => "General", 'visible' => Yii::app()->user->checkAccess('general')), array('url' => array('/data'), 'label' => "Data", 'visible' => Yii::app()->user->checkAccess('data')), array('url' => Yii::app()->getModule('user')->clientsUrl, 'label' => "Clients", 'visible' => Yii::app()->user->checkAccess('clients')), array('url' => Yii::app()->getModule('user')->providersUrl, 'label' => "Data Providers", 'visible' => Yii::app()->user->checkAccess('providers')), array('url' => Yii::app()->getModule('user')->ordersUrl, 'label' => "Orders", 'visible' => Yii::app()->user->checkAccess('orders')), array('url' => Yii::app()->getModule('user')->profileUrl, 'label' => "Tools", 'visible' => Yii::app()->user->checkAccess('tools')), array('url' => Yii::app()->getModule('user')->logoutUrl, 'label' => Yii::app()->getModule('user')->t("Logout"), 'visible' => !Yii::app()->user->isGuest)),));
            } elseif ($role->name == 'Client') {
                $this->widget('ext.cssmenu.CssMenu', array('items' => array(array('url' => array('/user/general/index'), 'label' => "General", 'visible' => Yii::app()->user->checkAccess('general')), array('url' => array('/user/data'), 'label' => "Data", 'visible' => Yii::app()->user->checkAccess('data')), array('url' => array('/user/client/lists'), 'label' => 'Lists', 'visible' => Yii::app()->user->checkAccess('User.Client.Lists')), array('url' => array('/user/orders/index', "id" => Yii::app()->user->Id), 'label' => 'Orders', 'visible' => Yii::app()->user->checkaccess('User.Orders.Index')), array('url' => Yii::app()->getModule('user')->profileUrl, 'label' => "Profile", 'visible' => Yii::app()->user->checkAccess('tools')), array('url' => Yii::app()->getModule('user')->logoutUrl, 'label' => Yii::app()->getModule('user')->t("Logout"), 'visible' => !Yii::app()->user->isGuest))));
            }
            ;
            if ($role->name == 'Provider') {
                $this->widget('ext.cssmenu.CssMenu', array('items' => array(array('url' => array('/user/general/index'), 'label' => "General", 'visible' => Yii::app()->user->checkAccess('general')), array('url' => array('/user/data'), 'label' => "Data", 'visible' => Yii::app()->user->checkAccess('data')), array('url' => array('/user/provider/data', "id" => Yii::app()->user->Id), 'label' => 'Data', 'visible' => Yii::app()->user->checkAccess('User.Feeds.Feeds')), array('url' => array('/user/feeds/feeds', "id" => Yii::app()->user->Id), 'label' => 'Feeds', 'visible' => Yii::app()->user->checkAccess('User.Feeds.Feeds')), array('url' => array('/user/payments', "id" => Yii::app()->user->Id), 'label' => 'Payments', 'visible' => Yii::app()->user->checkAccess('User.Feeds.Feeds')), array('url' => Yii::app()->getModule('user')->logoutUrl, 'label' => Yii::app()->getModule('user')->t("Logout"), 'visible' => !Yii::app()->user->isGuest))));
            }
于 2013-07-04T08:42:28.540 回答
2

导演拥有创作者和审稿人的权利

if(Yii::app()->user->checkAccess('Creator'))
{
  // creator and director are here
}

if(Yii::app()->user->checkAccess('Reviewer'))
{
  // reviewer and director are here
}

if(Yii::app()->user->checkAccess('Director'))
{
  // director is here
}
于 2013-07-04T08:38:53.190 回答