105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace app\controllers;
|
||
|
||
use yii;
|
||
use yii\filters\AccessControl;
|
||
use yii\web\Controller;
|
||
use yii\web\Response;
|
||
|
||
use app\models\AccountForm;
|
||
|
||
class RegistrationController extends Controller
|
||
{
|
||
// public function behaviors()
|
||
// {
|
||
// return [
|
||
// 'access' => [
|
||
// 'class' => AccessControl::class,
|
||
// 'rules' => [
|
||
// [
|
||
// 'allow' => true,
|
||
// 'roles' => ['?'],
|
||
// ]
|
||
// ],
|
||
// ]
|
||
// ];
|
||
// }
|
||
|
||
public function actionIndex()
|
||
{
|
||
// Инициализация
|
||
$model = new AccountForm(yii::$app->request->post('AccountForm') ?? yii::$app->request->get('AccountForm'));
|
||
$model->scenario = $model::SCENARIO_REGISTRATION;
|
||
|
||
if (yii::$app->request->isPost) {
|
||
// POST-запрос
|
||
|
||
yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
||
if (!yii::$app->user->isGuest || $model->registration()) {
|
||
// Данные прошли проверку и аккаунт был создан
|
||
|
||
// Аутентификация
|
||
$model->scenario = $model::SCENARIO_AUTHENTICATION;
|
||
$model->authentication();
|
||
|
||
// Инициализация
|
||
$notifications_button = $this->renderPartial('/notification/button');
|
||
$notifications_panel_full = true;
|
||
$notifications_panel = $this->renderPartial('/notification/panel', compact('notifications_panel_full'));
|
||
|
||
// Запись ответа
|
||
$return = [
|
||
'menu' => $this->renderPartial('/account/panel/authenticated', compact(
|
||
'notifications_button',
|
||
'notifications_panel'
|
||
)),
|
||
'_csrf' => yii::$app->request->getCsrfToken()
|
||
];
|
||
|
||
if (($cookies = yii::$app->response->cookies)->has('redirect')) {
|
||
// Найдено cookie с переадресацией
|
||
|
||
// Запись ответа
|
||
$return['redirect'] = '/' . $cookies['redirect'];
|
||
$return['main'] = $this->renderPartial($return['redirect'] . '/index');
|
||
|
||
// Очистка cookie
|
||
unset(yii::$app->response->cookies['redirect']);
|
||
} else {
|
||
// Не найдено cookie с переадресацией
|
||
|
||
if (yii::$app->request->pathInfo === 'authentication' || yii::$app->request->pathInfo === 'registration') {
|
||
// Если клиент на промежуточном URI
|
||
|
||
// Запись ответа
|
||
$return['redirect'] = '/';
|
||
$return['main'] = $this->renderPartial('/index');
|
||
}
|
||
}
|
||
|
||
return $return;
|
||
} else {
|
||
// Данные не прошли проверку
|
||
|
||
yii::$app->response->statusCode = 400;
|
||
|
||
return [
|
||
'main' => $this->renderPartial('/account/index', compact('model')),
|
||
'redirect' => '/registration',
|
||
'_csrf' => yii::$app->request->getCsrfToken()
|
||
];
|
||
}
|
||
}
|
||
|
||
if (!yii::$app->user->isGuest) {
|
||
yii::$app->response->redirect('/');
|
||
} else {
|
||
return $this->render('/account/index', compact('model'));
|
||
}
|
||
}
|
||
}
|