Answer the question
In order to leave comments, you need to log in
Why do non-logged in users have access to the controller?
With this code in the model, I define access to pages depending on the role (admin is role_id=1, user is role_id=2).
class User extends BaseUser
{
public static function getNameById($id) {
$row = self::find()->andWhere('id = :userId', array('userId' => $id))->one();
if($row)
return $row->username;
return '';
}
public static function getMenuItemsByRoleUser($isAdmin,$isGuest) {
if($isGuest)
return [];
if(Yii::$app->user->identity->role_id == 1) {
return [
['label' => 'Пользователи', 'url' => ['/user/admin/index']],
['label' => 'Права доступа', 'url' => ['/access/index']],
['label' => 'Каталог разделов', 'url' => ['/catalog/index']],
['label' => 'Сверка дат', 'url' => ['/user/admin/index1']],
['label' => 'Импорт', 'url' => ['/user/admin/index1']],
['label' => 'Экспорт', 'url' => ['/user/admin/index1']],
['label' => '', 'url' => ['/user/admin/index1']],
[
'label' => 'Выйти (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
];
} else if(Yii::$app->user->identity->role_id == 2){
return [
['label' => 'Каталог разделов', 'url' => ['/catalog/index']],
['label' => 'Каталог материалов', 'url' => ['/tasks-manager/index']],
['label' => 'Отчет', 'url' => ['/tasks-manager/index']],
[
'label' => 'Выйти (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
];
}
}
public function getRole()
{
return $this->hasOne(Role::className(), ['id' => 'role_id']);
}
}
Answer the question
In order to leave comments, you need to log in
Here, only the menu is formed from the user role, you also need to configure access to controllers / actions. In yii there is an access control filter behavior for this
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question