2016-01-03 05:48:37 +05:30
|
|
|
<?php
|
2016-01-15 14:51:27 +05:30
|
|
|
namespace api\controllers;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\authentication\ForgotPasswordForm;
|
|
|
|
use api\models\authentication\LoginForm;
|
2016-07-17 22:08:04 +05:30
|
|
|
use api\models\authentication\LogoutForm;
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\authentication\RecoverPasswordForm;
|
2016-05-31 03:33:30 +05:30
|
|
|
use api\models\authentication\RefreshTokenForm;
|
2016-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-05-11 01:55:04 +05:30
|
|
|
use common\helpers\StringHelper;
|
2016-01-03 05:48:37 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
2016-02-24 03:45:04 +05:30
|
|
|
use yii\helpers\ArrayHelper;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
|
|
|
class AuthenticationController extends Controller {
|
|
|
|
|
|
|
|
public function behaviors() {
|
2016-02-24 03:45:04 +05:30
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'authenticator' => [
|
2016-05-31 03:33:30 +05:30
|
|
|
'except' => ['login', 'forgot-password', 'recover-password', 'refresh-token'],
|
2016-02-24 03:45:04 +05:30
|
|
|
],
|
2016-01-03 05:48:37 +05:30
|
|
|
'access' => [
|
2016-01-21 02:44:29 +05:30
|
|
|
'class' => AccessControl::class,
|
2016-01-03 05:48:37 +05:30
|
|
|
'rules' => [
|
|
|
|
[
|
2016-05-31 03:33:30 +05:30
|
|
|
'actions' => ['login', 'forgot-password', 'recover-password', 'refresh-token'],
|
2016-01-03 05:48:37 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['?'],
|
|
|
|
],
|
2016-07-17 22:08:04 +05:30
|
|
|
[
|
|
|
|
'actions' => ['logout'],
|
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['@'],
|
|
|
|
],
|
2016-01-03 05:48:37 +05:30
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs() {
|
|
|
|
return [
|
2016-01-21 02:44:29 +05:30
|
|
|
'login' => ['POST'],
|
2016-07-17 22:08:04 +05:30
|
|
|
'logout' => ['POST'],
|
2016-05-11 01:55:04 +05:30
|
|
|
'forgot-password' => ['POST'],
|
2016-05-12 03:43:19 +05:30
|
|
|
'recover-password' => ['POST'],
|
2016-05-31 03:33:30 +05:30
|
|
|
'refresh-token' => ['POST'],
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
public function actionLogin() {
|
|
|
|
$model = new LoginForm();
|
2016-01-03 05:48:37 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
2016-05-30 05:14:17 +05:30
|
|
|
if (($result = $model->login()) === false) {
|
2016-03-14 00:16:22 +05:30
|
|
|
$data = [
|
2016-01-03 05:48:37 +05:30
|
|
|
'success' => false,
|
2016-01-15 14:51:27 +05:30
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
2016-03-14 00:16:22 +05:30
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
if (ArrayHelper::getValue($data['errors'], 'login') === E::ACCOUNT_NOT_ACTIVATED) {
|
2016-03-14 00:16:22 +05:30
|
|
|
$data['data']['email'] = $model->getAccount()->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
return array_merge([
|
2016-01-03 05:48:37 +05:30
|
|
|
'success' => true,
|
2016-05-30 05:14:17 +05:30
|
|
|
], $result->getAsResponse());
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|
|
|
|
|
2016-07-17 22:08:04 +05:30
|
|
|
public function actionLogout() {
|
|
|
|
$form = new LogoutForm();
|
|
|
|
$form->logout();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:55:04 +05:30
|
|
|
public function actionForgotPassword() {
|
|
|
|
$model = new ForgotPasswordForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if ($model->forgotPassword() === false) {
|
|
|
|
$data = [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
if (ArrayHelper::getValue($data['errors'], 'login') === E::RECENTLY_SENT_MESSAGE) {
|
2016-05-11 01:55:04 +05:30
|
|
|
$emailActivation = $model->getEmailActivation();
|
|
|
|
$data['data'] = [
|
|
|
|
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
|
|
|
'repeatFrequency' => $emailActivation->repeatTimeout,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$emailActivation = $model->getEmailActivation();
|
|
|
|
$response = [
|
|
|
|
'success' => true,
|
|
|
|
'data' => [
|
|
|
|
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
|
|
|
'repeatFrequency' => $emailActivation->repeatTimeout,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($model->getLoginAttribute() !== 'email') {
|
|
|
|
$response['data']['emailMask'] = StringHelper::getEmailMask($model->getAccount()->email);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:43:19 +05:30
|
|
|
public function actionRecoverPassword() {
|
|
|
|
$model = new RecoverPasswordForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
2016-05-30 05:14:17 +05:30
|
|
|
if (($result = $model->recoverPassword()) === false) {
|
2016-05-12 03:43:19 +05:30
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
return array_merge([
|
2016-05-12 03:43:19 +05:30
|
|
|
'success' => true,
|
2016-05-30 05:14:17 +05:30
|
|
|
], $result->getAsResponse());
|
2016-05-12 03:43:19 +05:30
|
|
|
}
|
|
|
|
|
2016-05-31 03:33:30 +05:30
|
|
|
public function actionRefreshToken() {
|
|
|
|
$model = new RefreshTokenForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (($result = $model->renew()) === false) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_merge([
|
|
|
|
'success' => true,
|
|
|
|
], $result->getAsResponse());
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|