2016-01-03 03:18:37 +03:00
|
|
|
<?php
|
2016-01-15 12:21:27 +03:00
|
|
|
namespace api\controllers;
|
2016-01-03 03:18:37 +03:00
|
|
|
|
2016-05-14 02:47:17 +03:00
|
|
|
use api\models\authentication\ConfirmEmailForm;
|
|
|
|
use api\models\authentication\RegistrationForm;
|
2018-04-17 23:47:25 +03:00
|
|
|
use api\models\authentication\RepeatAccountActivationForm;
|
2016-06-16 23:32:23 +03:00
|
|
|
use common\helpers\Error as E;
|
2016-01-03 03:18:37 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
2016-02-24 01:15:04 +03:00
|
|
|
use yii\helpers\ArrayHelper;
|
2016-01-03 03:18:37 +03:00
|
|
|
|
2016-01-15 12:21:27 +03:00
|
|
|
class SignupController extends Controller {
|
2016-01-03 03:18:37 +03:00
|
|
|
|
2017-09-19 20:06:16 +03:00
|
|
|
public function behaviors(): array {
|
2016-02-24 01:15:04 +03:00
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'authenticator' => [
|
2016-03-13 21:24:49 +03:00
|
|
|
'except' => ['index', 'repeat-message', 'confirm'],
|
2016-02-24 01:15:04 +03:00
|
|
|
],
|
2016-01-03 03:18:37 +03:00
|
|
|
'access' => [
|
2016-01-21 00:14:29 +03:00
|
|
|
'class' => AccessControl::class,
|
2016-01-03 03:18:37 +03:00
|
|
|
'rules' => [
|
|
|
|
[
|
2016-03-13 21:24:49 +03:00
|
|
|
'actions' => ['index', 'repeat-message', 'confirm'],
|
2016-01-03 03:18:37 +03:00
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['?'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-01-21 00:14:29 +03:00
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'register' => ['POST'],
|
|
|
|
'confirm' => ['POST'],
|
2016-03-13 02:19:00 +03:00
|
|
|
'new-message' => ['POST'],
|
2016-01-21 00:14:29 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-24 01:20:10 +03:00
|
|
|
public function actionIndex() {
|
2016-01-15 12:21:27 +03:00
|
|
|
$model = new RegistrationForm();
|
2016-01-03 03:18:37 +03:00
|
|
|
$model->load(Yii::$app->request->post());
|
2016-01-15 12:21:27 +03:00
|
|
|
if (!$model->signup()) {
|
2016-01-03 03:18:37 +03:00
|
|
|
return [
|
|
|
|
'success' => false,
|
2016-11-30 20:57:30 +03:00
|
|
|
'errors' => $model->getFirstErrors(),
|
2016-01-03 03:18:37 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-13 21:24:49 +03:00
|
|
|
public function actionRepeatMessage() {
|
|
|
|
$model = new RepeatAccountActivationForm();
|
2016-03-13 02:19:00 +03:00
|
|
|
$model->load(Yii::$app->request->post());
|
2016-03-13 21:24:49 +03:00
|
|
|
if (!$model->sendRepeatMessage()) {
|
2016-03-13 02:19:00 +03:00
|
|
|
$response = [
|
|
|
|
'success' => false,
|
2016-11-30 20:57:30 +03:00
|
|
|
'errors' => $model->getFirstErrors(),
|
2016-03-13 02:19:00 +03:00
|
|
|
];
|
|
|
|
|
2016-08-03 15:57:41 +03:00
|
|
|
if (ArrayHelper::getValue($response['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
2016-05-10 22:40:06 +03:00
|
|
|
$activation = $model->getActivation();
|
2016-03-13 02:19:00 +03:00
|
|
|
$response['data'] = [
|
2016-05-10 22:40:06 +03:00
|
|
|
'canRepeatIn' => $activation->canRepeatIn(),
|
|
|
|
'repeatFrequency' => $activation->repeatTimeout,
|
2016-03-13 02:19:00 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-21 00:14:29 +03:00
|
|
|
public function actionConfirm() {
|
|
|
|
$model = new ConfirmEmailForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
2016-05-30 02:44:17 +03:00
|
|
|
if (!($result = $model->confirm())) {
|
2016-01-21 00:14:29 +03:00
|
|
|
return [
|
|
|
|
'success' => false,
|
2016-11-30 20:57:30 +03:00
|
|
|
'errors' => $model->getFirstErrors(),
|
2016-01-21 00:14:29 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-05-30 02:44:17 +03:00
|
|
|
return array_merge([
|
2016-01-21 00:14:29 +03:00
|
|
|
'success' => true,
|
2016-05-30 02:44:17 +03:00
|
|
|
], $result->getAsResponse());
|
2016-01-21 00:14:29 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 03:18:37 +03:00
|
|
|
}
|