mirror of
https://github.com/elyby/accounts.git
synced 2024-11-16 18:23:29 +05:30
2a4da87fd5
Логика проверки пароля вынесена в отдельный валидатор В composer.json докинута зависимость от php7
115 lines
3.3 KiB
PHP
115 lines
3.3 KiB
PHP
<?php
|
|
namespace api\controllers;
|
|
|
|
use api\models\ForgotPasswordForm;
|
|
use api\models\LoginForm;
|
|
use api\models\RecoverPasswordForm;
|
|
use common\helpers\StringHelper;
|
|
use Yii;
|
|
use yii\filters\AccessControl;
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
class AuthenticationController extends Controller {
|
|
|
|
public function behaviors() {
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
'authenticator' => [
|
|
'except' => ['login', 'forgot-password', 'recover-password'],
|
|
],
|
|
'access' => [
|
|
'class' => AccessControl::class,
|
|
'rules' => [
|
|
[
|
|
'actions' => ['login', 'forgot-password', 'recover-password'],
|
|
'allow' => true,
|
|
'roles' => ['?'],
|
|
],
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function verbs() {
|
|
return [
|
|
'login' => ['POST'],
|
|
'forgot-password' => ['POST'],
|
|
'recover-password' => ['POST'],
|
|
];
|
|
}
|
|
|
|
public function actionLogin() {
|
|
$model = new LoginForm();
|
|
$model->load(Yii::$app->request->post());
|
|
if (($jwt = $model->login()) === false) {
|
|
$data = [
|
|
'success' => false,
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
];
|
|
|
|
if (ArrayHelper::getValue($data['errors'], 'login') === 'error.account_not_activated') {
|
|
$data['data']['email'] = $model->getAccount()->email;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'jwt' => $jwt,
|
|
];
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function actionRecoverPassword() {
|
|
$model = new RecoverPasswordForm();
|
|
$model->load(Yii::$app->request->post());
|
|
if (($jwt = $model->recoverPassword()) === false) {
|
|
return [
|
|
'success' => false,
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'jwt' => $jwt,
|
|
];
|
|
}
|
|
|
|
}
|