mirror of
https://github.com/elyby/accounts.git
synced 2024-12-27 23:50:19 +05:30
73 lines
2.0 KiB
PHP
73 lines
2.0 KiB
PHP
<?php
|
||
namespace api\controllers;
|
||
|
||
use api\models\ConfirmEmailForm;
|
||
use api\models\RegistrationForm;
|
||
use Yii;
|
||
use yii\filters\AccessControl;
|
||
use yii\helpers\ArrayHelper;
|
||
|
||
class SignupController extends Controller {
|
||
|
||
public function behaviors() {
|
||
return ArrayHelper::merge(parent::behaviors(), [
|
||
'authenticator' => [
|
||
'except' => ['index', 'confirm'],
|
||
],
|
||
'access' => [
|
||
'class' => AccessControl::class,
|
||
'rules' => [
|
||
[
|
||
'actions' => ['index', 'confirm'],
|
||
'allow' => true,
|
||
'roles' => ['?'],
|
||
],
|
||
],
|
||
],
|
||
]);
|
||
}
|
||
|
||
public function verbs() {
|
||
return [
|
||
'register' => ['POST'],
|
||
'confirm' => ['POST'],
|
||
];
|
||
}
|
||
|
||
public function actionIndex() {
|
||
$model = new RegistrationForm();
|
||
$model->load(Yii::$app->request->post());
|
||
if (!$model->signup()) {
|
||
return [
|
||
'success' => false,
|
||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
||
];
|
||
}
|
||
|
||
return [
|
||
'success' => true,
|
||
];
|
||
}
|
||
|
||
public function actionConfirm() {
|
||
$model = new ConfirmEmailForm();
|
||
$model->load(Yii::$app->request->post());
|
||
if (!$model->confirm()) {
|
||
return [
|
||
'success' => false,
|
||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
||
];
|
||
}
|
||
|
||
// TODO: не уверен, что логин должен быть здесь + нужно разобраться с параметрами установки куки авторизации и сессии
|
||
$activationCode = $model->getActivationCodeModel();
|
||
$account = $activationCode->account;
|
||
Yii::$app->user->login($account);
|
||
|
||
return [
|
||
'success' => true,
|
||
];
|
||
}
|
||
|
||
}
|