-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;
}
}
请告诉我解决这个最好的办法.... 谢谢。