[ 'class' => AccessControl::class, 'rules' => [ [ 'actions' => ['login', 'error', 'maintenance'], 'allow' => true, ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserManager()->hasAccessBackend(); } ], [ 'actions' => ['switch-producer'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserManager()->getCurrentStatus() == User::STATUS_ADMIN; } ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ ], ], ]; } /** * @inheritdoc */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], ]; } /** * Affiche la page de connexion. */ public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(\Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } else { return $this->render('login', [ 'model' => $model, ]); } } /** * Déconnecte l'utilisateur et le redirige à la page d'accueil. */ public function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } /** * Change le producteur courant de l'utilisateur connecté. * Permet de passer d'un producteur à un autre en tant qu'administrateur. */ public function actionSwitchProducer(int $id) { $user = $this->getUserCurrent(); $producer = $this->getProducerContainer()->getRepository()->findOneProducerById($id); if($producer) { $this->getUserContainer()->getBuilder()->switchProducer($user, $producer); } else { $this->addFlash('error', 'Producteur introuvable.'); } $this->redirect(['dashboard/index']); } }