flash, middleware

This commit is contained in:
Kavalar 2024-10-17 14:55:00 +03:00
parent c3c377a4e2
commit 4355651695
22 changed files with 226 additions and 75 deletions

View File

@ -7,5 +7,6 @@
"app_module_path": "{APP}/modules/{slug}",
"module_class": "app\\modules\\tag\\TagModule",
"module_class_file": "{APP}/modules/tag/TagModule.php",
"routs": "routs/tag.php"
"routs": "routs/tag.php",
"dependence": "menu"
}

29
kernel/Flash.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace kernel;
use Josantonius\Session\Facades\Session;
class Flash
{
public static function setMessage(string $type, string $msg): void
{
Session::start();
Session::set($type, $msg);
}
public static function getMessage(string $type): string
{
$msg = Session::get($type, false);
Session::remove($type);
return $msg;
}
public static function hasMessage(string $type): bool
{
return Session::has($type);
}
}

View File

@ -64,4 +64,19 @@ class FormModel
return $this->validator->getProcessedErrors();
}
public function getErrorsStr(): string
{
$errorsArr = $this->getErrors();
$str = '';
if ($errorsArr['errors']) {
foreach ($errorsArr['errors'] as $key => $errorArr) {
foreach ($errorsArr['errors'][$key] as $error){
$str .= "$error <br>";
}
}
}
return $str;
}
}

10
kernel/Middleware.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace kernel;
abstract class Middleware
{
abstract function handler();
}

View File

@ -18,9 +18,19 @@
<link rel="stylesheet" href="<?= $resources ?>/css/style.css">
</head>
<body>
<div class="wrapper d-flex align-items-stretch">
<?php if (\kernel\Flash::hasMessage("error")): ?>
<div class="alert alert-danger alert-dismissible mainAlert">
<?= \kernel\Flash::getMessage("error"); ?>
<button type="button" class="btn-close closeAlertBtn"></button>
</div>
<?php endif; ?>
<?php if (\kernel\Flash::hasMessage("success")): ?>
<div class="alert alert-success alert-dismissible">
<?= \kernel\Flash::getMessage("success"); ?>
<button type="button" class="btn-close closeAlertBtn" ></button>
</div>
<?php endif; ?>
<!-- Page Content -->
<div id="content" class="p-4 p-md-5">
<?= $content ?>

View File

@ -70,11 +70,16 @@
</div>
</div>
</nav>
<?php if (\Josantonius\Session\Facades\Session::get("error", false)): ?>
<div class="alert alert-danger alert-dismissible" id="mainAlert">
<?= \Josantonius\Session\Facades\Session::get("error"); ?>
<?php \Josantonius\Session\Facades\Session::remove("error"); ?>
<button type="button" class="btn-close" id="closeAlertBtn"></button>
<?php if (\kernel\Flash::hasMessage("error")): ?>
<div class="alert alert-danger alert-dismissible mainAlert">
<?= \kernel\Flash::getMessage("error"); ?>
<button type="button" class="btn-close closeAlertBtn"></button>
</div>
<?php endif; ?>
<?php if (\kernel\Flash::hasMessage("success")): ?>
<div class="alert alert-success alert-dismissible">
<?= \kernel\Flash::getMessage("success"); ?>
<button type="button" class="btn-close closeAlertBtn" ></button>
</div>
<?php endif; ?>
<?= $content ?>

View File

@ -5,9 +5,17 @@ namespace kernel\console\controllers;
use kernel\console\ConsoleController;
use kernel\helpers\Files;
use kernel\services\KernelService;
use ZipArchive;
class KernelController extends ConsoleController
{
protected Files $files;
public function __construct()
{
parent::__construct();
$this->files = new Files();
}
/**
* @throws \Exception
@ -34,4 +42,40 @@ class KernelController extends ConsoleController
}
}
/**
* @throws \Exception
*/
public function actionUpdateKernel()
{
if (!isset($this->argv['path'])) {
throw new \Exception('Missing kernel path "--path" specified');
}
$path = $this->argv['path'];
if (file_exists(ROOT_DIR . $path)) {
$zip = new ZipArchive;
$tmpKernelDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/kernel/' . $tmpKernelDir . "/";
$zip->extractTo($tmpKernelDirFull);
$zip->close();
$this->files->copy_folder($tmpKernelDirFull , ROOT_DIR . "/kernel");
$this->files->recursiveRemoveDir($tmpKernelDirFull);
$this->out->r('Ядро обновлено.', 'green');
} else {
$this->out->r('unable to open zip archive', 'red');
return false;
}
} else {
$this->out->r("Модуль не найден", 'red');
}
}
public function test()
{
}
}

View File

@ -132,9 +132,9 @@ class Slug
* @param $model
* @return string
*/
public static function createSlug(string $title, $model = null): string
public static function createSlug(string $data, $model = null): string
{
$slug = Slug::url_slug($title, ['transliterate' => true, 'lowercase' => true]);
$slug = Slug::url_slug($data, ['transliterate' => true, 'lowercase' => true]);
if ($model === null) {
return $slug;
}

View File

@ -0,0 +1,19 @@
<?php
namespace kernel\middlewares;
use kernel\Middleware;
class AuthMiddleware extends Middleware
{
function handler()
{
if(!isset($_COOKIE['user_id']))
{
header('Location: /admin/login', true, 302);
return false;
}
}
}

View File

@ -4,7 +4,10 @@ use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector;
App::$collector->filter("auth", [\kernel\middlewares\AuthMiddleware::class, "handler"]);
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) {
App::$collector->group(["before" => "auth"], function (RouteCollector $router){
App::$collector->group(["prefix" => "settings"], function (RouteCollector $router){
App::$collector->group(["prefix" => "menu"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\modules\menu\controllers\MenuController::class, 'actionIndex']);
@ -19,6 +22,8 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router)
});
});
});
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
$router->rest("menu", [\kernel\modules\menu\controllers\MenuRestController::class]);
});

View File

@ -6,11 +6,11 @@
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\action_column\DeleteActionColumn;
use kernel\IGTabel\action_column\EditActionColumn;
use kernel\IGTabel\action_column\ViewActionColumn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\models\Menu;
use kernel\modules\menu\table\columns\MenuDeleteActionColumn;
use kernel\modules\menu\table\columns\MenuEditActionColumn;
use kernel\modules\menu\table\columns\MenuViewActionColumn;
$table = new ListEloquentTable(new EloquentDataProvider(Menu::class, [
'currentPage' => $page_number,
@ -28,8 +28,8 @@ $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/settings/menu/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
});
$table->addAction(MenuViewActionColumn::class);
$table->addAction(MenuEditActionColumn::class);
$table->addAction(MenuDeleteActionColumn::class);
$table->addAction(ViewActionColumn::class);
$table->addAction(EditActionColumn::class);
$table->addAction(DeleteActionColumn::class);
$table->create();
$table->render();

View File

@ -4,6 +4,7 @@ namespace kernel\modules\option\controllers;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\modules\option\models\forms\CreateOptionForm;
use kernel\modules\option\models\Option;
@ -33,9 +34,11 @@ class OptionController extends AdminController
if ($optionForm->validate()) {
$option = $this->optionService->create($optionForm);
if ($option) {
Flash::setMessage("success", "Опция успешно создана.");
$this->redirect('/admin/option');
}
}
Flash::setMessage("error", $optionForm->getErrorsStr());
$this->redirect('/admin/option/create');
}
@ -95,6 +98,7 @@ class OptionController extends AdminController
#[NoReturn] public function actionDelete(int $id): void
{
Option::find($id)->delete();
Flash::setMessage("success", "Опция успешно удалена.");
$this->redirect('/admin/option');
}

View File

@ -16,7 +16,7 @@ class CreateOptionForm extends FormModel
public function rules(): array
{
return [
'key' => 'required|min-str-len:1|max-str-len:50',
'key' => 'required|min-str-len:3|max-str-len:50',
'value' => '',
'label' => '',
'status' => ''

View File

@ -6,11 +6,11 @@
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\action_column\DeleteActionColumn;
use kernel\IGTabel\action_column\EditActionColumn;
use kernel\IGTabel\action_column\ViewActionColumn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\modules\option\models\Option;
use kernel\modules\option\table\columns\OptionDeleteActionColumn;
use kernel\modules\option\table\columns\OptionEditActionColumn;
use kernel\modules\option\table\columns\OptionViewActionColumn;
$table = new ListEloquentTable(new EloquentDataProvider(Option::class, [
'current_page' => $page_number,
@ -31,8 +31,8 @@ $table->columns([
}]
]);
$table->addAction(OptionViewActionColumn::class);
$table->addAction(OptionEditActionColumn::class);
$table->addAction(OptionDeleteActionColumn::class);
$table->addAction(ViewActionColumn::class);
$table->addAction(EditActionColumn::class);
$table->addAction(DeleteActionColumn::class);
$table->create();
$table->render();

View File

@ -42,7 +42,7 @@ class PostController extends AdminController
* @throws Exception
*/
public function actionIndex($page_number = 1): void
public function actionIndex(int $page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
@ -50,7 +50,7 @@ class PostController extends AdminController
/**
* @throws Exception
*/
public function actionView($id): void
public function actionView(int $id): void
{
$content = Post::find($id);
@ -63,7 +63,7 @@ class PostController extends AdminController
/**
* @throws Exception
*/
public function actionUpdate($id): void
public function actionUpdate(int $id): void
{
$model = Post::find($id);
if (!$model){
@ -76,17 +76,16 @@ class PostController extends AdminController
/**
* @throws Exception
*/
public function actionEdit($id): void
public function actionEdit(int $id): void
{
$post = Post::find($id);
if (!$post){
throw new Exception(message: "The post not found");
}
$postForm = new CreatePostForm();
$postService = new PostService();
$postForm->load($_REQUEST);
if ($postForm->validate()) {
$post = $postService->update($postForm, $post);
$post = $this->postService->update($postForm, $post);
if ($post) {
$this->redirect("/admin/post/" . $post->id);
}
@ -94,9 +93,15 @@ class PostController extends AdminController
$this->redirect("/admin/post/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
/**
* @throws Exception
*/
#[NoReturn] public function actionDelete(int $id): void
{
$post = Post::find($id)->first();
if (!$post){
throw new Exception(message: "The post not found");
}
$post->delete();
$this->redirect("/admin/post/");
}

View File

@ -5,14 +5,11 @@
* @var int $page_number
*/
use kernel\IGTabel\action_column\DeleteActionColumn;
use kernel\IGTabel\action_column\EditActionColumn;
use kernel\IGTabel\action_column\ViewActionColumn;
use kernel\modules\post\models\Post;
use kernel\modules\post\table\columns\PostDeleteActionColumn;
use kernel\modules\post\table\columns\PostEditActionColumn;
use kernel\modules\post\table\columns\PostViewActionColumn;
use kernel\modules\user\models\User;
//use app\tables\columns\post\PostDeleteActionColumn;
//use app\tables\columns\post\PostEditActionColumn;
//use app\tables\columns\post\PostViewActionColumn;
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\btn\PrimaryBtn;
@ -46,8 +43,8 @@ $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/post/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
});
$table->addAction(PostViewActionColumn::class);
$table->addAction(PostEditActionColumn::class);
$table->addAction(PostDeleteActionColumn::class);
$table->addAction(ViewActionColumn::class);
$table->addAction(EditActionColumn::class);
$table->addAction(DeleteActionColumn::class);
$table->create();
$table->render();

View File

@ -4,6 +4,8 @@ namespace kernel\modules\secure\controllers;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\modules\secure\models\forms\LoginForm;
use kernel\modules\user\service\UserService;
@ -25,7 +27,7 @@ class SecureController extends AdminController
$this->cgView->render('login.php');
}
public function actionAuth(): void
#[NoReturn] public function actionAuth(): void
{
$loginForm = new LoginForm();
$loginForm->load($_REQUEST);
@ -39,21 +41,23 @@ class SecureController extends AdminController
$user = $this->userService->getByField($field, $loginForm->getItem("username"));
if (!$user){
throw new \Exception(message: "User not found");
Flash::setMessage("error", "User not found.");
$this->redirect("/admin/login", code: 302);
}
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");
$this->redirect("/admin", code: 302);
} else {
$this->redirect("/admin/login");
Flash::setMessage("error", "Username or password incorrect.");
$this->redirect("/admin/login", code: 302);
}
}
#[NoReturn] public function actionLogout(): void
{
unset($_COOKIE['user_id']);
setcookie('user_id', "", -1, '/', $_SERVER['SERVER_NAME'], false);
setcookie('user_id', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false);
$this->redirect("/", code: 302);
}

View File

@ -3,14 +3,7 @@
use kernel\App;
use Phroute\Phroute\RouteCollector;
App::$collector->filter("auth", function (){
if(!isset($_COOKIE['user_id']))
{
header('Location: /admin/login', true, 302);
return false;
}
});
App::$collector->filter("auth", [\kernel\middlewares\AuthMiddleware::class, "handler"]);
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->group(["before" => "auth"], function (RouteCollector $router){
@ -20,3 +13,4 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']);
App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']);
});

View File

@ -7,11 +7,11 @@
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\action_column\DeleteActionColumn;
use kernel\IGTabel\action_column\EditActionColumn;
use kernel\IGTabel\action_column\ViewActionColumn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\modules\user\models\User;
use kernel\modules\user\table\columns\UserDeleteActionColumn;
use kernel\modules\user\table\columns\UserEditActionColumn;
use kernel\modules\user\table\columns\UserViewActionColumn;
$table = new ListEloquentTable(new EloquentDataProvider(User::class, [
'currentPage' => $page_number,
@ -45,8 +45,8 @@ $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/user/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
});
$table->addAction(UserViewActionColumn::class);
$table->addAction(UserEditActionColumn::class);
$table->addAction(UserDeleteActionColumn::class);
$table->addAction(ViewActionColumn::class);
$table->addAction(EditActionColumn::class);
$table->addAction(DeleteActionColumn::class);
$table->create();
$table->render();

View File

@ -32,7 +32,6 @@ class AdminThemeService
public function setActiveAdminTheme(string $theme): void
{
$active_admin_theme = Option::where("key", "active_admin_theme")->first();
Debug::prn(getConst($theme));
$active_admin_theme->value = getConst($theme);
$active_admin_theme->save();
}

View File

@ -346,6 +346,9 @@ class ModuleService
$fileHelper->copy_folder(APP_DIR . '/modules/' . $moduleName, $tmpModuleDirFull . 'app/');
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
if (!is_dir(RESOURCES_DIR . '/tmp/module')) {
mkdir(RESOURCES_DIR . '/tmp/modules', 0777, true);
}
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.itguild');
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);

View File

@ -16,8 +16,15 @@
$('#sidebar').toggleClass('active');
});
$("#closeAlertBtn").click(function () {
$("#mainAlert").alert("close");
})(jQuery);
window.addEventListener("load", function () {
let closeBtn = document.querySelectorAll(".closeAlertBtn");
closeBtn.forEach((item) => {
item.addEventListener("click", closeAlert);
});
});
})(jQuery);
function closeAlert() {
this.parentNode.setAttribute("style", "display: none;")
}