|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\logic\User\User\Model\User;
- use Yii;
- use yii\filters\AccessControl;
- use common\forms\LoginForm;
- use yii\filters\VerbFilter;
-
-
- class SiteController extends BackendController
- {
-
-
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'actions' => ['login', 'error'],
- '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' => [
- ],
- ],
- ];
- }
-
-
-
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- ];
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- public function actionLogout()
- {
- Yii::$app->user->logout();
-
- return $this->goHome();
- }
-
-
-
- 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']);
- }
- }
|