yii2检查用户在行为中的角色并授予访问权限
我正在开发一个具有多种用户类型的管理面板。我为能够更新自己数据的机构创建了一个“AgencyBehavior”。 但我需要向superadmin展示所有机构的所有数据 我所做的: 我正在检查当前用户是否是超级管理员,如果是超级管理员,那么我将向AgencyBehavior发送空值 但当超级管理员试图插入或更新来自不同机构的任何数据时,这是行不通的 AgencyBehavior.phpyii2检查用户在行为中的角色并授予访问权限,yii2,yii2-advanced-app,yii2-user,Yii2,Yii2 Advanced App,Yii2 User,我正在开发一个具有多种用户类型的管理面板。我为能够更新自己数据的机构创建了一个“AgencyBehavior”。 但我需要向superadmin展示所有机构的所有数据 我所做的: 我正在检查当前用户是否是超级管理员,如果是超级管理员,那么我将向AgencyBehavior发送空值 但当超级管理员试图插入或更新来自不同机构的任何数据时,这是行不通的 AgencyBehavior.php <?php namespace app\components; use Yii; use yii\be
<?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;
}
}
您正在寻找的是一种模仿其他用户的方式。似乎可以处理模拟(而不是测试)
如果我必须这样做,我会制作一个superadmin按钮(或下拉菜单)以作为另一个代理登录,将代理用户id存储在管理员会话中,并让您编写的行为返回此存储的代理id(如果可用),而不是空值