secure module add

This commit is contained in:
2024-09-25 14:17:36 +03:00
parent 349c2992dc
commit 959d708daf
8 changed files with 75 additions and 46 deletions

View File

@ -0,0 +1,60 @@
<?php
namespace kernel\modules\secure\controllers;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\modules\secure\models\forms\LoginForm;
use kernel\modules\user\service\UserService;
class SecureController extends AdminController
{
protected UserService $userService;
protected function init(): void
{
parent::init();
// $this->cgView->viewPath = KERNEL_DIR . "/views/secure/";
$this->cgView->viewPath = KERNEL_MODULES_DIR. "/secure/views/";
$this->cgView->layout = "/login.php";
$this->userService = new UserService();
}
public function actionLogin(): void
{
$this->cgView->render('login.php');
}
public function actionAuth(): void
{
$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){
throw new \Exception(message: "User not found");
}
if (password_verify($loginForm->getItem("password"), $user->password_hash)) {
setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false);
$this->redirect("/admin");
} else {
$this->redirect("/admin/login");
}
}
#[NoReturn] public function actionLogout(): void
{
unset($_COOKIE['user_id']);
setcookie('user_id', "", -1, '/', $_SERVER['SERVER_NAME'], false);
$this->redirect("/");
}
}

View File

@ -0,0 +1,8 @@
{
"name": "Secure",
"version": "0.1",
"author": "ITGuild",
"slug": "secure",
"description": "Secure module",
"routs": "routs/secure.php"
}

View File

@ -0,0 +1,18 @@
<?php
namespace kernel\modules\secure\models\forms;
use kernel\FormModel;
class LoginForm extends FormModel
{
public function rules(): array
{
return [
'username' => 'required|min-str-len:5|max-str-len:50',
'password' => 'required|min-str-len:6|max-str-len:50',
];
}
}

View File

@ -0,0 +1,22 @@
<?php
use kernel\App;
use Phroute\Phroute\RouteCollector;
App::$collector->filter("auth", function (){
if(!isset($_COOKIE['user_id']))
{
header('Location: /admin/login');
return false;
}
});
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->group(["before" => "auth"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']);
});
App::$collector->get('/login', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogin']);
App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']);
App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']);
});

View File

@ -0,0 +1,67 @@
<!-- Section: Design Block -->
<section class=" text-center text-lg-start">
<style>
.rounded-t-5 {
border-top-left-radius: 0.5rem;
border-top-right-radius: 0.5rem;
}
@media (min-width: 992px) {
.rounded-tr-lg-0 {
border-top-right-radius: 0;
}
.rounded-bl-lg-5 {
border-bottom-left-radius: 0.5rem;
}
}
</style>
<div class="card mb-3">
<div class="row g-0 d-flex align-items-center">
<div class="col-lg-4 d-none d-lg-flex">
<img src="https://mdbootstrap.com/img/new/ecommerce/vertical/004.jpg" alt="Trendy Pants and Shoes"
class="w-100 rounded-t-5 rounded-tr-lg-0 rounded-bl-lg-5" />
</div>
<div class="col-lg-8">
<div class="card-body py-5 px-md-5">
<form action="/admin/auth" method="post">
<!-- Email input -->
<div data-mdb-input-init class="form-outline mb-4">
<input type="text" id="form2Example1" class="form-control" name="username" />
<label class="form-label" for="form2Example1">Username или Email</label>
</div>
<!-- Password input -->
<div data-mdb-input-init class="form-outline mb-4">
<input type="password" id="form2Example2" class="form-control" name="password" />
<label class="form-label" for="form2Example2">Пароль</label>
</div>
<!-- 2 column grid layout for inline styling -->
<div class="row mb-4">
<div class="col d-flex justify-content-center">
<!-- Checkbox -->
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="form2Example31" checked />
<label class="form-check-label" for="form2Example31"> Запомнить </label>
</div>
</div>
<div class="col">
<!-- Simple link -->
<a href="#!">Забыл пароль?</a>
</div>
</div>
<!-- Submit button -->
<button type="submit" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block mb-4">Вход</button>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- Section: Design Block -->