MicroFrameWork/kernel/modules/secure/controllers/SecureController.php

173 lines
5.7 KiB
PHP
Raw Normal View History

2024-09-24 17:22:09 +03:00
<?php
2024-09-25 14:17:36 +03:00
namespace kernel\modules\secure\controllers;
2024-09-24 17:22:09 +03:00
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
2024-12-05 14:31:04 +03:00
use kernel\App;
2024-10-17 14:55:00 +03:00
use kernel\Flash;
use kernel\helpers\Debug;
2024-12-05 14:31:04 +03:00
use kernel\Mailing;
use kernel\modules\secure\models\forms\LoginEmailForm;
2024-09-25 14:17:36 +03:00
use kernel\modules\secure\models\forms\LoginForm;
2024-12-04 12:42:53 +03:00
use kernel\modules\secure\models\forms\RegisterForm;
2024-12-09 13:46:10 +03:00
use kernel\modules\secure\services\SecureService;
2024-12-05 14:31:04 +03:00
use kernel\modules\user\models\User;
2024-09-24 17:22:09 +03:00
use kernel\modules\user\service\UserService;
2024-12-05 14:31:04 +03:00
use kernel\Request;
use PHPMailer\PHPMailer\Exception;
use Random\RandomException;
2024-09-24 17:22:09 +03:00
class SecureController extends AdminController
{
protected UserService $userService;
protected function init(): void
{
parent::init();
2024-09-25 14:17:36 +03:00
$this->cgView->viewPath = KERNEL_MODULES_DIR. "/secure/views/";
2024-09-24 17:22:09 +03:00
$this->cgView->layout = "/login.php";
$this->userService = new UserService();
}
public function actionLogin(): void
{
2024-12-05 14:31:04 +03:00
$this->cgView->render(match (App::$secure['web_auth_type']) {
"login_password" => "login.php",
"email_code" => "email_login.php",
});
// $this->cgView->render('login.php');
2024-09-24 17:22:09 +03:00
}
2024-10-17 14:55:00 +03:00
#[NoReturn] public function actionAuth(): void
2024-09-24 17:22:09 +03:00
{
$loginForm = new LoginForm();
$loginForm->load($_REQUEST);
if(filter_var($loginForm->getItem("username"), FILTER_VALIDATE_EMAIL)) {
$field = "email";
}
else {
$field = "username";
}
$user = $this->userService->getByField($field, $loginForm->getItem("username"));
if (!$user){
2024-10-17 14:55:00 +03:00
Flash::setMessage("error", "User not found.");
$this->redirect("/admin/login", code: 302);
2024-09-24 17:22:09 +03:00
}
if (password_verify($loginForm->getItem("password"), $user->password_hash)) {
setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false);
2024-10-17 14:55:00 +03:00
$this->redirect("/admin", code: 302);
2024-09-24 17:22:09 +03:00
} else {
2024-10-17 14:55:00 +03:00
Flash::setMessage("error", "Username or password incorrect.");
$this->redirect("/admin/login", code: 302);
2024-09-24 17:22:09 +03:00
}
}
2024-12-05 14:31:04 +03:00
/**
* @throws RandomException
* @throws Exception
*/
public function actionEmailAuth(): void
{
2024-12-05 15:16:39 +03:00
$mailing = new Mailing();
2024-12-05 14:31:04 +03:00
$loginForm = new LoginEmailForm();
$loginForm->load($_REQUEST);
2024-12-05 15:16:39 +03:00
$email = $loginForm->getItem("email");
2024-12-05 14:31:04 +03:00
$user = $this->userService->getByField('email', $email);
2024-12-05 15:16:39 +03:00
2024-12-05 14:31:04 +03:00
if (!$user){
2024-12-05 16:50:58 +03:00
$password = bin2hex(random_bytes(8));
2024-12-05 15:16:39 +03:00
UserService::createUserByEmailAndPassword($email, $password);
$user = $this->userService->getByField('email', $email);
2024-12-09 13:46:10 +03:00
SecureService::createSecretCode($user);
$secretCode = SecureService::getByField("user_id", $user->id);
$mailing->send_html("register_by_code.php", ['code' => $secretCode->code, 'password' => $password], [
2024-12-05 15:16:39 +03:00
'address' => $email,
'subject' => "Код регистрации",
"from_name" => $_ENV['APP_NAME']
]);
2024-12-05 14:31:04 +03:00
} else {
2024-12-09 13:46:10 +03:00
SecureService::updateSecretCode($user);
$secretCode = SecureService::getByField("user_id", $user->id);
$mailing->send_html("login_by_code.php", ['code' => $secretCode->code], [
2024-12-05 15:16:39 +03:00
'address' => $email,
'subject' => "Код авторизации",
"from_name" => $_ENV['APP_NAME']
]);
}
2024-12-05 14:31:04 +03:00
setcookie('user_email', $email, time()+60*15, '/', $_SERVER['SERVER_NAME'], false);
$this->cgView->render("enter_code.php", ['email' => $email]);
}
/**
* @throws Exception
*/
public function actionCodeCheck(): void
{
$request = new Request();
if (isset($_COOKIE['user_email'])) {
$user = User::where('email', $_COOKIE["user_email"])->first();
if (!$user) {
throw new exception("User not found.");
}
$code = $request->post("code");
2024-12-09 13:46:10 +03:00
$secretCode = SecureService::getByField("user_id", $user->id);
if ($secretCode->code == $code && time() <= strtotime($secretCode->code_expires_at)) {
2024-12-05 14:31:04 +03:00
setcookie('user_id', $user->id, time() + 60 * 60 * 24, '/', $_SERVER['SERVER_NAME'], false);
$this->redirect("/admin", code: 302);
} else {
Flash::setMessage("error", "Wrong code.");
2024-12-05 16:50:58 +03:00
$this->cgView->render("enter_code.php", ['email' => $_COOKIE["user_email"]]);
2024-12-05 14:31:04 +03:00
}
}
}
2024-09-24 17:22:09 +03:00
#[NoReturn] public function actionLogout(): void
{
unset($_COOKIE['user_id']);
2024-10-17 14:55:00 +03:00
setcookie('user_id', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false);
2024-12-05 14:31:04 +03:00
setcookie('user_email', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false);
2024-09-26 14:47:13 +03:00
$this->redirect("/", code: 302);
2024-09-24 17:22:09 +03:00
}
2024-12-05 14:31:04 +03:00
public function actionRegister(): void
2024-12-04 12:42:53 +03:00
{
$this->cgView->render('register.php');
}
public function actionRegistration(): void
{
$regForm = new RegisterForm();
$regForm->load($_REQUEST);
if ($this->userService->getByField('username', $regForm->getItem("username"))) {
Flash::setMessage("error", "Username already exists.");
$this->redirect("/admin/register", code: 302);
}
if ($this->userService->getByField('email', $regForm->getItem("email"))) {
Flash::setMessage("error", "Email already exists.");
$this->redirect("/admin/register", code: 302);
}
$user = $this->userService->create($regForm);
if ($user){
setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false);
$this->redirect("/admin", code: 302);
}
}
2024-09-24 17:22:09 +03:00
}