2017-04-17 54 views
-1

我正在开发具有多种用户类型的管理面板。我为可以更新自己的数据的机构创建了一个“AgencyBehaviour”。 但我需要显示superadmin从所有机构的所有数据。yii2检查用户在行为中的角色并授予访问权限

我做了什么:

我检查当前用户是否是超级管理员,如果是超级管理员,然后我发送空值到AgencyBehaviour。

但这不工作,而超级管理员尝试插入或更新来自不同机构的任何数据。

AgencyBehavior.php

<?php 

namespace app\components; 

use Yii; 
use yii\behaviors\AttributeBehavior; 
use yii\db\BaseActiveRecord; 

class AgencyBehavior extends AttributeBehavior { 

private $agency_id; 
public $value; 

/** 
* @inheritdoc 
*/ 
public function init() { 
    parent::init(); 

    if (empty($this->attributes)) { 
     $this->attributes = [ 
      BaseActiveRecord::EVENT_BEFORE_VALIDATE => 'agency_id', 
     ]; 
    } 
} 

// return value of thsi method is set to the attribute attched to the event in the init. we can as well define the event handler 
protected function getValue($event) { 
    if ($this->value === null) { 
     $user = Yii::$app->get('user', false); 
     //Check for super admin user role 
     $superAdminId = \app\models\Parameters::getValue('SYSTEM_USER_ID'); 

     if (yii::$app->user->identity->role_id == $superAdminId) { 
      return null; 
     } 

     return ($user && !$user->isGuest) ? yii::$app->user->identity->agency_id : null; 
    } 

    return parent::getValue($event); 
} 

public function getAgency_id() { 
    if ($this->agency_id === null) { 
     $user = Yii::$app->get('user', false); 
     $this->agency_id = $user && !$user->isGuest ? $user->agency_id : null; 
     return $this->agency_id; 
    } 

    return parent::getValue($event); 
} 

public function setAgency_id($value) { 
    $this->agency_id = $value; 
} 

} 

请告诉我解决这个最好的办法.... 谢谢。

回答

0

您正在寻找的是一种模仿另一个用户的方式。 https://github.com/dektrium/yii2-user似乎处理模拟(而不是testet)。

如果我必须这样做,我会让一个超级按钮(或下拉菜单)作为另一个代理登录,将代理用户标识存储在管理员会话中,并让您所写的行为返回此存储的代理标识,如果可用的话,而不是null。

相关问题