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;
|
|
|
|
use api\models\authentication\RecoverPasswordForm;
|
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-12 03:43:19 +05:30
|
|
|
'except' => ['login', 'forgot-password', 'recover-password'],
|
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-12 03:43:19 +05:30
|
|
|
'actions' => ['login', 'forgot-password', 'recover-password'],
|
2016-01-03 05:48:37 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['?'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs() {
|
|
|
|
return [
|
2016-01-21 02:44:29 +05:30
|
|
|
'login' => ['POST'],
|
2016-05-11 01:55:04 +05:30
|
|
|
'forgot-password' => ['POST'],
|
2016-05-12 03:43:19 +05:30
|
|
|
'recover-password' => ['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
|
|
|
|
|
|
|
if (ArrayHelper::getValue($data['errors'], 'login') === 'error.account_not_activated') {
|
|
|
|
$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-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()),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (ArrayHelper::getValue($data['errors'], 'login') === 'error.email_frequency') {
|
|
|
|
$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-01-03 05:48:37 +05:30
|
|
|
}
|