You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.2 KiB
94 lines
2.2 KiB
7 years ago
|
<?php
|
||
|
namespace backend\controllers;
|
||
|
|
||
|
use common\auth\Identity;
|
||
|
use core\services\auth\AuthService;
|
||
|
use Yii;
|
||
|
use yii\web\Controller;
|
||
|
use yii\filters\VerbFilter;
|
||
|
use core\forms\auth\LoginForm;
|
||
|
use yii\filters\AccessControl;
|
||
|
|
||
|
class AuthController extends Controller
|
||
|
{
|
||
|
private $authService;
|
||
|
|
||
|
public function __construct($id, $module, AuthService $service, $config = [])
|
||
|
{
|
||
|
parent::__construct($id, $module, $config);
|
||
|
$this->authService = $service;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @inheritdoc
|
||
|
*/
|
||
|
public function behaviors()
|
||
|
{
|
||
|
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'],
|
||
|
],
|
||
|
],
|
||
|
],
|
||
|
'verbs' => [
|
||
|
'class' => VerbFilter::className(),
|
||
|
'actions' => [
|
||
|
'logout' => ['post'],
|
||
|
],
|
||
|
],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function actionLogin()
|
||
|
{
|
||
|
if (!Yii::$app->user->isGuest) {
|
||
|
return $this->goHome();
|
||
|
}
|
||
|
|
||
|
$this->layout = 'main-login';
|
||
|
|
||
|
$form = new LoginForm();
|
||
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
|
||
|
try {
|
||
|
$user = $this->authService->auth($form);
|
||
|
Yii::$app->user->login(new Identity($user), $form->rememberMe ? 3600 * 24 * 30 : 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();
|
||
|
}
|
||
|
}
|