複数のユーザータイプの管理パネルを開発しています。自分のデータを更新できる代理店のための "AgencyBehaviour"を作成しました。 しかし、すべての代理店のすべてのデータをsuperadminに表示する必要があります。yii2ユーザーの行動を確認してアクセスする
は私がやっていること:私はそれは私がAgencyBehaviourにnull値を送信し、その後superadminされている場合、現在のユーザーは、スーパー管理者であるかどうかをチェックしています
。
スーパー管理者が別の代理店のデータを挿入または更新しようとしているときにこれは機能しません。
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;
}
}
ありがとう....私はこれを解決する最良の方法を教えてください。