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-01-21 02:44:29 +05:30
|
|
|
use api\models\ConfirmEmailForm;
|
2016-03-13 04:49:00 +05:30
|
|
|
use api\models\NewAccountActivationForm;
|
2016-01-15 14:51:27 +05:30
|
|
|
use api\models\RegistrationForm;
|
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
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
class SignupController extends Controller {
|
2016-01-03 05:48:37 +05:30
|
|
|
|
|
|
|
public function behaviors() {
|
2016-02-24 03:45:04 +05:30
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'authenticator' => [
|
2016-03-13 04:49:00 +05:30
|
|
|
'except' => ['index', 'new-message', 'confirm'],
|
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-03-13 04:49:00 +05:30
|
|
|
'actions' => ['index', 'new-message', 'confirm'],
|
2016-01-03 05:48:37 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['?'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-01-21 02:44:29 +05:30
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'register' => ['POST'],
|
|
|
|
'confirm' => ['POST'],
|
2016-03-13 04:49:00 +05:30
|
|
|
'new-message' => ['POST'],
|
2016-01-21 02:44:29 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-24 03:50:10 +05:30
|
|
|
public function actionIndex() {
|
2016-01-15 14:51:27 +05:30
|
|
|
$model = new RegistrationForm();
|
2016-01-03 05:48:37 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
2016-01-15 14:51:27 +05:30
|
|
|
if (!$model->signup()) {
|
2016-01-03 05:48:37 +05:30
|
|
|
return [
|
|
|
|
'success' => false,
|
2016-01-15 14:51:27 +05:30
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-13 04:49:00 +05:30
|
|
|
public function actionNewMessage() {
|
|
|
|
$model = new NewAccountActivationForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->sendNewMessage()) {
|
|
|
|
$response = [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($response['errors']['email'] === 'error.recently_sent_message') {
|
|
|
|
$activeActivation = $model->getActiveActivation();
|
|
|
|
$response['data'] = [
|
|
|
|
'can_repeat_in' => $activeActivation->created_at - time() + NewAccountActivationForm::REPEAT_FREQUENCY,
|
|
|
|
'repeat_frequency' => NewAccountActivationForm::REPEAT_FREQUENCY,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-21 02:44:29 +05:30
|
|
|
public function actionConfirm() {
|
|
|
|
$model = new ConfirmEmailForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
2016-02-27 04:07:55 +05:30
|
|
|
if (!($jwt = $model->confirm())) {
|
2016-01-21 02:44:29 +05:30
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
2016-02-27 04:07:55 +05:30
|
|
|
'jwt' => $jwt,
|
2016-01-21 02:44:29 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|