|  |  |  | <?php
 | 
					
						
							|  |  |  | namespace frontend\controllers\auth;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use common\auth\Identity;
 | 
					
						
							|  |  |  | use core\services\auth\AuthService;
 | 
					
						
							|  |  |  | use frontend\components\FrontendController;
 | 
					
						
							|  |  |  | use Yii;
 | 
					
						
							|  |  |  | use core\forms\auth\LoginForm;
 | 
					
						
							|  |  |  | use yii\filters\AccessControl;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AuthController extends FrontendController
 | 
					
						
							|  |  |  | {
 | 
					
						
							|  |  |  |     public $layout = 'auth';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private $service;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct($id, $module, AuthService $service, $config = [])
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         parent::__construct($id, $module, $config);
 | 
					
						
							|  |  |  |         $this->service = $service;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function behaviors(): array
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		return [
 | 
					
						
							|  |  |  | 			'access' => [
 | 
					
						
							|  |  |  | 				'class' => AccessControl::className(),
 | 
					
						
							|  |  |  | 				'rules' => [
 | 
					
						
							|  |  |  | 					[
 | 
					
						
							|  |  |  | 						'actions' => ['login'],
 | 
					
						
							|  |  |  | 						'allow' => true,
 | 
					
						
							|  |  |  | 						'roles' => ['?'],
 | 
					
						
							|  |  |  | 					],
 | 
					
						
							|  |  |  | 					[
 | 
					
						
							|  |  |  | 						'actions' => ['logout'],
 | 
					
						
							|  |  |  | 						'allow' => true,
 | 
					
						
							|  |  |  | 						'roles' => ['@'],
 | 
					
						
							|  |  |  | 					],
 | 
					
						
							|  |  |  | 					[    // all the action are accessible to admin
 | 
					
						
							|  |  |  | 						'allow' => true,
 | 
					
						
							|  |  |  | 						'roles' => ['admin'],
 | 
					
						
							|  |  |  | 					],
 | 
					
						
							|  |  |  | 				],
 | 
					
						
							|  |  |  | 			],
 | 
					
						
							|  |  |  | 		];
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @return mixed
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function actionLogin()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         if (!Yii::$app->user->isGuest) {
 | 
					
						
							|  |  |  |             return $this->goHome();
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $form = new LoginForm();
 | 
					
						
							|  |  |  |         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |                 $user = $this->service->auth($form);
 | 
					
						
							|  |  |  |                 Yii::$app->user->login(new Identity($user), $form->rememberMe ? Yii::$app->params['user.rememberMeDuration'] : 0);
 | 
					
						
							|  |  |  |                 return $this->goBack();
 | 
					
						
							|  |  |  |             } catch (\DomainException $e) {
 | 
					
						
							|  |  |  |                 Yii::$app->errorHandler->logException($e);
 | 
					
						
							|  |  |  |                 Yii::$app->session->setFlash('error', $e->getMessage());
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->render('login', [
 | 
					
						
							|  |  |  |             'model' => $form,
 | 
					
						
							|  |  |  |         ]);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @return mixed
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function actionLogout()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         Yii::$app->user->logout();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->goHome();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |