[ 'class' => AccessControl::class, 'rules' => [ [ 'actions' => ['login', 'error', 'maintenance'], 'allow' => true, ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsProducer($this->getUserCurrent()); } ], [ 'actions' => ['switch-producer'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsAdministrator($this->getUserCurrent()); } ], ], ], '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->getProducerModule()->getRepository()->findOneProducerById($id); if($producer) { $this->getUserModule()->getBuilder()->switchProducer($user, $producer); } else { $this->addFlash('error', 'Producteur introuvable.'); } return $this->redirectReferer(); } }