public function getRolesByUser($userId)
{
if (!isset($userId) || $userId === '') {
return [];
}
$query = (new Query())->select('b.*')->from(['a' => $this->assignmentTable, 'b' => $this->itemTable])->where('{{a}}.[[item_name]]={{b}}.[[name]]')->andWhere(['a.user_id' => (string) $userId])->andWhere(['b.type' => Item::TYPE_ROLE]);
$roles = [];
foreach ($query->all($this->db) as $row) {
$roles[$row['name']] = $this->populateItem($row);
}
return $roles;
}
/** * when user login in backend , it should be 'Administrator' or ,'Merchant' */ public static function beforeLogin() { Event::on(\yii\web\User::className(), \yii\web\User::EVENT_BEFORE_LOGIN, function ($event) { $user = $event->identity; $auth = new DbManager(); $auth->init(); $role = $auth->getRolesByUser($user->id); $event->isValid = in_array(current($role)->name, ['Administrator', 'Merchant']); }); }