big restructure

This commit is contained in:
2026-02-11 19:28:12 +01:00
parent cd59ccd99b
commit 3eb9cc0ac4
209 changed files with 5101 additions and 2459 deletions

View File

@@ -1,16 +1,18 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Access;
use MintyPHP\Repository\RolePermissionRepository;
use MintyPHP\Repository\UserRoleRepository;
use MintyPHP\Repository\PermissionRepository;
use MintyPHP\Repository\Access\RolePermissionRepository;
use MintyPHP\Repository\Access\UserRoleRepository;
use MintyPHP\Repository\Access\PermissionRepository;
class PermissionService
{
public const USERS_CREATE = 'users.create';
public const USERS_DELETE = 'users.delete';
public const USERS_VIEW = 'users.view';
public const USERS_VIEW_META = 'users.view_meta';
public const USERS_VIEW_AUDIT = 'users.view_audit';
public const USERS_UPDATE = 'users.update';
public const USERS_SELF_UPDATE = 'users.self_update';
public const USERS_UPDATE_ASSIGNMENTS = 'users.update_assignments';
@@ -96,6 +98,11 @@ class PermissionService
return PermissionRepository::list();
}
public static function listActive(): array
{
return PermissionRepository::listActive();
}
public static function listPaged(array $options): array
{
return PermissionRepository::listPaged($options);
@@ -152,6 +159,9 @@ class PermissionService
if (!$permission) {
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
}
if ((int) ($permission['is_system'] ?? 0) === 1) {
return ['ok' => false, 'status' => 403, 'error' => 'system_permission_protected'];
}
$deleted = PermissionRepository::delete($id);
if (!$deleted) {
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
@@ -164,6 +174,8 @@ class PermissionService
return [
'key' => trim((string) ($input['key'] ?? '')),
'description' => trim((string) ($input['description'] ?? '')),
'active' => self::normalizeActive($input['active'] ?? 1),
'is_system' => self::normalizeFlag($input['is_system'] ?? 0),
];
}
@@ -177,4 +189,25 @@ class PermissionService
}
return $errors;
}
private static function normalizeActive($value): int
{
$value = strtolower(trim((string) $value));
if (in_array($value, ['0', 'false', 'inactive'], true)) {
return 0;
}
return 1;
}
private static function normalizeFlag($value): int
{
if (is_bool($value)) {
return $value ? 1 : 0;
}
$value = strtolower(trim((string) $value));
if (in_array($value, ['1', 'true', 'yes', 'on'], true)) {
return 1;
}
return 0;
}
}

View File

@@ -1,9 +1,9 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Access;
use MintyPHP\Repository\RoleRepository;
use MintyPHP\Service\SettingService;
use MintyPHP\Repository\Access\RoleRepository;
use MintyPHP\Service\Settings\SettingService;
class RoleService
{
@@ -12,6 +12,11 @@ class RoleService
return RoleRepository::list();
}
public static function listActive(): array
{
return RoleRepository::listActive();
}
public static function listPaged(array $options): array
{
return RoleRepository::listPaged($options);
@@ -30,7 +35,7 @@ class RoleService
public static function createFromAdmin(array $input, int $currentUserId = 0): array
{
$form = self::sanitizeBase($input);
$errors = self::validateBase($form);
$errors = self::validateBase($form, 0);
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'form' => $form];
@@ -38,6 +43,8 @@ class RoleService
$createdId = RoleRepository::create([
'description' => $form['description'],
'code' => $form['code'] ?: null,
'active' => $form['active'],
'created_by' => $currentUserId > 0 ? $currentUserId : null,
]);
@@ -56,7 +63,7 @@ class RoleService
public static function updateFromAdmin(int $roleId, array $input, int $currentUserId = 0): array
{
$form = self::sanitizeBase($input);
$errors = self::validateBase($form);
$errors = self::validateBase($form, $roleId);
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'form' => $form];
@@ -64,6 +71,8 @@ class RoleService
$updated = RoleRepository::update($roleId, [
'description' => $form['description'],
'code' => $form['code'] ?: null,
'active' => $form['active'],
'modified_by' => $currentUserId > 0 ? $currentUserId : null,
]);
@@ -102,15 +111,30 @@ class RoleService
{
return [
'description' => trim((string) ($input['description'] ?? '')),
'code' => trim((string) ($input['code'] ?? '')),
'active' => self::normalizeActive($input['active'] ?? 1),
];
}
private static function validateBase(array $form): array
private static function validateBase(array $form, int $excludeId): array
{
$errors = [];
if ($form['description'] === '') {
$errors[] = t('Description cannot be empty');
}
$code = $form['code'] ?? '';
if ($code !== '' && RoleRepository::existsByCode($code, $excludeId)) {
$errors[] = t('Role code already exists');
}
return $errors;
}
private static function normalizeActive($value): int
{
$value = strtolower(trim((string) $value));
if (in_array($value, ['0', 'false', 'inactive'], true)) {
return 0;
}
return 1;
}
}

View File

@@ -1,15 +1,15 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Auth;
use MintyPHP\Auth;
use MintyPHP\Router;
use MintyPHP\Support\Flash;
use MintyPHP\Service\UserService;
use MintyPHP\Service\PermissionService;
use MintyPHP\Service\RememberMeService;
use MintyPHP\Service\EmailVerificationService;
use MintyPHP\Repository\UserRepository;
use MintyPHP\Service\User\UserService;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Auth\RememberMeService;
use MintyPHP\Service\Auth\EmailVerificationService;
use MintyPHP\Repository\User\UserRepository;
class AuthService
{
@@ -60,6 +60,7 @@ class AuthService
'flash_key' => 'login_no_active_tenant',
];
}
UserRepository::updateLastLogin($userId);
}
return ['ok' => true];

View File

@@ -1,9 +1,9 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Auth;
use MintyPHP\Repository\EmailVerificationRepository;
use MintyPHP\Repository\UserRepository;
use MintyPHP\Repository\Auth\EmailVerificationRepository;
use MintyPHP\Repository\User\UserRepository;
use MintyPHP\I18n;
use MintyPHP\Http\Request;

View File

@@ -1,12 +1,12 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Auth;
use MintyPHP\Repository\PasswordResetRepository;
use MintyPHP\Repository\UserRepository;
use MintyPHP\Repository\Auth\PasswordResetRepository;
use MintyPHP\Repository\User\UserRepository;
use MintyPHP\I18n;
use MintyPHP\Http\Request;
use MintyPHP\Service\RememberMeService;
use MintyPHP\Service\Auth\RememberMeService;
class PasswordResetService
{

View File

@@ -1,13 +1,13 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Auth;
use MintyPHP\Session;
use MintyPHP\Repository\RememberTokenRepository;
use MintyPHP\Repository\UserRepository;
use MintyPHP\Repository\Auth\RememberTokenRepository;
use MintyPHP\Repository\User\UserRepository;
use MintyPHP\I18n;
use MintyPHP\Service\PermissionService;
use MintyPHP\Service\AuthService;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Auth\AuthService;
class RememberMeService
{
@@ -83,6 +83,9 @@ class RememberMeService
if ($userId > 0) {
PermissionService::getUserPermissions($userId, true);
AuthService::loadTenantDataIntoSession($userId);
if (empty($_SESSION['no_active_tenant'])) {
UserRepository::updateLastLogin($userId);
}
}
$newToken = bin2hex(random_bytes(32));
@@ -115,6 +118,11 @@ class RememberMeService
RememberTokenRepository::deleteByUserId($userId);
}
public static function expireAllTokensByAdmin(): int
{
return RememberTokenRepository::expireAllByAdmin();
}
private static function setCookie(string $selector, string $token): void
{
$value = $selector . ':' . $token;

View File

@@ -1,6 +1,6 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Branding;
class BrandingFaviconService
{
@@ -19,7 +19,7 @@ class BrandingFaviconService
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
return rtrim(APP_STORAGE_PATH, '/');
}
return rtrim(dirname(__DIR__, 2) . '/storage', '/');
return rtrim(dirname(__DIR__, 3) . '/storage', '/');
}
public static function storageDir(): string
@@ -29,7 +29,7 @@ class BrandingFaviconService
public static function publicDir(): string
{
return rtrim(dirname(__DIR__, 2) . '/web/favicon', '/');
return rtrim(dirname(__DIR__, 3) . '/web/favicon', '/');
}
public static function hasFavicon(): bool
@@ -195,8 +195,8 @@ class BrandingFaviconService
}
$title = null;
if (class_exists('MintyPHP\\Service\\SettingService')) {
$title = \MintyPHP\Service\SettingService::getAppTitle();
if (class_exists('MintyPHP\\Service\\Settings\\SettingService')) {
$title = \MintyPHP\Service\Settings\SettingService::getAppTitle();
}
if ($title === null || $title === '') {
$title = defined('APP_NAME') ? APP_NAME : 'IMVS';

View File

@@ -1,6 +1,6 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Branding;
class BrandingLogoService
{
@@ -13,7 +13,7 @@ class BrandingLogoService
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
return rtrim(APP_STORAGE_PATH, '/');
}
return rtrim(dirname(__DIR__, 2) . '/storage', '/');
return rtrim(dirname(__DIR__, 3) . '/storage', '/');
}
public static function brandingDir(): string

View File

@@ -1,9 +1,9 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Content;
use MintyPHP\Repository\PageRepository;
use MintyPHP\Repository\PageContentRepository;
use MintyPHP\Repository\Content\PageRepository;
use MintyPHP\Repository\Content\PageContentRepository;
use MintyPHP\I18n;
class PageService

View File

@@ -1,8 +1,8 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Mail;
use MintyPHP\Repository\MailLogRepository;
use MintyPHP\Repository\Mail\MailLogRepository;
class MailLogService
{

View File

@@ -1,10 +1,10 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Mail;
use MintyPHP\Repository\MailLogRepository;
use MintyPHP\Repository\Mail\MailLogRepository;
use MintyPHP\I18n;
use MintyPHP\Service\SettingService;
use MintyPHP\Service\Settings\SettingService;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception as MailerException;
@@ -69,7 +69,7 @@ class MailService
private static function renderTemplate(string $template, array $vars, string $locale): array
{
$base = dirname(__DIR__, 2) . '/templates/emails';
$base = dirname(__DIR__, 3) . '/templates/emails';
$htmlPath = $base . '/' . $locale . '/' . $template . '.html';
$textPath = $base . '/' . $locale . '/' . $template . '.txt';
$htmlHeaderPath = $base . '/' . $locale . '/partials/header.html';

View File

@@ -1,12 +1,12 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Org;
use MintyPHP\Repository\DepartmentRepository;
use MintyPHP\Repository\TenantDepartmentRepository;
use MintyPHP\Repository\UserDepartmentRepository;
use MintyPHP\Service\SettingService;
use MintyPHP\Service\TenantScopeService;
use MintyPHP\Repository\Org\DepartmentRepository;
use MintyPHP\Repository\Tenant\TenantDepartmentRepository;
use MintyPHP\Repository\Org\UserDepartmentRepository;
use MintyPHP\Service\Settings\SettingService;
use MintyPHP\Service\Tenant\TenantScopeService;
class DepartmentService
{
@@ -71,13 +71,17 @@ class DepartmentService
{
$form = self::sanitizeBase($input);
$errors = self::validateBase($form);
$warnings = self::warningsForCode($form, 0);
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'form' => $form];
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
}
$createdId = DepartmentRepository::create([
'description' => $form['description'],
'code' => $form['code'] ?: null,
'cost_center' => $form['cost_center'] ?: null,
'active' => $form['active'],
'created_by' => $currentUserId > 0 ? $currentUserId : null,
]);
@@ -90,20 +94,24 @@ class DepartmentService
if (!empty($input['is_default']) && $createdId) {
SettingService::setDefaultDepartmentId((int) $createdId);
}
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
return ['ok' => true, 'form' => $form, 'warnings' => $warnings, 'uuid' => $uuid, 'id' => (int) $createdId];
}
public static function updateFromAdmin(int $departmentId, array $input, int $currentUserId = 0): array
{
$form = self::sanitizeBase($input);
$errors = self::validateBase($form);
$warnings = self::warningsForCode($form, $departmentId);
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'form' => $form];
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
}
$updated = DepartmentRepository::update($departmentId, [
'description' => $form['description'],
'code' => $form['code'] ?: null,
'cost_center' => $form['cost_center'] ?: null,
'active' => $form['active'],
'modified_by' => $currentUserId > 0 ? $currentUserId : null,
]);
@@ -111,7 +119,7 @@ class DepartmentService
return ['ok' => false, 'errors' => [t('Department can not be updated')], 'form' => $form];
}
return ['ok' => true, 'form' => $form];
return ['ok' => true, 'form' => $form, 'warnings' => $warnings];
}
public static function syncTenants(int $departmentId, array $tenantIds): int
@@ -147,6 +155,9 @@ class DepartmentService
{
return [
'description' => trim((string) ($input['description'] ?? '')),
'code' => trim((string) ($input['code'] ?? '')),
'cost_center' => trim((string) ($input['cost_center'] ?? '')),
'active' => self::normalizeActive($input['active'] ?? 1),
];
}
@@ -158,4 +169,23 @@ class DepartmentService
}
return $errors;
}
private static function warningsForCode(array $form, int $excludeId): array
{
$warnings = [];
$code = $form['code'] ?? '';
if ($code !== '' && DepartmentRepository::existsByCode($code, $excludeId)) {
$warnings[] = t('Department code already exists');
}
return $warnings;
}
private static function normalizeActive($value): int
{
$value = strtolower(trim((string) $value));
if (in_array($value, ['0', 'false', 'inactive'], true)) {
return 0;
}
return 1;
}
}

View File

@@ -1,11 +1,11 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Settings;
use MintyPHP\Repository\SettingRepository;
use MintyPHP\Repository\TenantRepository;
use MintyPHP\Repository\RoleRepository;
use MintyPHP\Repository\DepartmentRepository;
use MintyPHP\Repository\Settings\SettingRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Repository\Access\RoleRepository;
use MintyPHP\Repository\Org\DepartmentRepository;
class SettingService
{
@@ -16,6 +16,7 @@ class SettingService
public const APP_LOCALE_KEY = 'app_locale';
public const APP_THEME_KEY = 'app_theme';
public const APP_THEME_USER_KEY = 'app_theme_user';
public const APP_REGISTRATION_KEY = 'app_registration';
public const APP_PRIMARY_COLOR_KEY = 'app_primary_color';
public const SMTP_HOST_KEY = 'smtp_host';
public const SMTP_PORT_KEY = 'smtp_port';
@@ -149,7 +150,7 @@ class SettingService
{
$value = SettingRepository::getValue(self::APP_THEME_KEY);
$value = $value !== null ? strtolower(trim((string) $value)) : '';
if (!in_array($value, ['light', 'dark'], true)) {
if (!in_array($value, self::allowedThemes(), true)) {
return null;
}
return $value;
@@ -158,7 +159,7 @@ class SettingService
public static function setAppTheme(?string $theme, ?string $description = null): bool
{
$value = $theme !== null ? strtolower(trim((string) $theme)) : '';
if ($value === '' || !in_array($value, ['light', 'dark'], true)) {
if ($value === '' || !in_array($value, self::allowedThemes(), true)) {
$value = null;
}
$desc = $description ?? 'setting.app_theme';
@@ -181,6 +182,43 @@ class SettingService
return SettingRepository::set(self::APP_THEME_USER_KEY, $value, $desc);
}
private static function allowedThemes(): array
{
$file = dirname(__DIR__, 3) . '/config/themes.php';
if (!is_file($file)) {
return ['light', 'dark'];
}
$themes = include $file;
if (!is_array($themes)) {
return ['light', 'dark'];
}
$keys = [];
foreach ($themes as $key => $label) {
$key = strtolower(trim((string) $key));
$label = trim((string) $label);
if ($key !== '' && $label !== '') {
$keys[] = $key;
}
}
return $keys ?: ['light', 'dark'];
}
public static function isRegistrationEnabled(): bool
{
$value = SettingRepository::getValue(self::APP_REGISTRATION_KEY);
if ($value === null || $value === '') {
return true;
}
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
}
public static function setRegistrationEnabled(bool $allowed, ?string $description = null): bool
{
$value = $allowed ? '1' : '0';
$desc = $description ?? 'setting.app_registration';
return SettingRepository::set(self::APP_REGISTRATION_KEY, $value, $desc);
}
public static function getAppPrimaryColor(): ?string
{
$value = SettingRepository::getValue(self::APP_PRIMARY_COLOR_KEY);

View File

@@ -1,6 +1,6 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Tenant;
class TenantAvatarService
{
@@ -18,7 +18,7 @@ class TenantAvatarService
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
return rtrim(APP_STORAGE_PATH, '/');
}
return rtrim(dirname(__DIR__, 2) . '/storage', '/');
return rtrim(dirname(__DIR__, 3) . '/storage', '/');
}
public static function tenantDir(string $uuid): string

View File

@@ -1,6 +1,6 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Tenant;
class TenantFaviconService
{
@@ -24,7 +24,7 @@ class TenantFaviconService
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
return rtrim(APP_STORAGE_PATH, '/');
}
return rtrim(dirname(__DIR__, 2) . '/storage', '/');
return rtrim(dirname(__DIR__, 3) . '/storage', '/');
}
public static function storageDir(string $uuid): string
@@ -34,7 +34,7 @@ class TenantFaviconService
public static function publicDir(string $uuid): string
{
return rtrim(dirname(__DIR__, 2) . '/web/favicon/tenants/' . $uuid, '/');
return rtrim(dirname(__DIR__, 3) . '/web/favicon/tenants/' . $uuid . '/favicon', '/');
}
public static function hasFavicon(string $uuid): bool

View File

@@ -1,11 +1,11 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Tenant;
use MintyPHP\Repository\TenantDepartmentRepository;
use MintyPHP\Repository\UserTenantRepository;
use MintyPHP\Repository\TenantRepository;
use MintyPHP\Service\PermissionService;
use MintyPHP\Repository\Tenant\TenantDepartmentRepository;
use MintyPHP\Repository\Tenant\UserTenantRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Service\Access\PermissionService;
class TenantScopeService
{

View File

@@ -1,9 +1,9 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\Tenant;
use MintyPHP\Repository\TenantRepository;
use MintyPHP\Service\SettingService;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Service\Settings\SettingService;
class TenantService
{

View File

@@ -1,6 +1,6 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\User;
class UserAvatarService
{
@@ -18,7 +18,7 @@ class UserAvatarService
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
return rtrim(APP_STORAGE_PATH, '/');
}
return rtrim(dirname(__DIR__, 2) . '/storage', '/');
return rtrim(dirname(__DIR__, 3) . '/storage', '/');
}
public static function userDir(string $uuid): string

View File

@@ -1,18 +1,18 @@
<?php
namespace MintyPHP\Service;
namespace MintyPHP\Service\User;
use MintyPHP\I18n;
use MintyPHP\Repository\DepartmentRepository;
use MintyPHP\Repository\RoleRepository;
use MintyPHP\Repository\TenantRepository;
use MintyPHP\Repository\UserRepository;
use MintyPHP\Repository\UserDepartmentRepository;
use MintyPHP\Repository\UserRoleRepository;
use MintyPHP\Repository\UserTenantRepository;
use MintyPHP\Repository\TenantDepartmentRepository;
use MintyPHP\Service\PermissionService;
use MintyPHP\Service\TenantScopeService;
use MintyPHP\Repository\Org\DepartmentRepository;
use MintyPHP\Repository\Access\RoleRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Repository\User\UserRepository;
use MintyPHP\Repository\Org\UserDepartmentRepository;
use MintyPHP\Repository\Access\UserRoleRepository;
use MintyPHP\Repository\Tenant\UserTenantRepository;
use MintyPHP\Repository\Tenant\TenantDepartmentRepository;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Tenant\TenantScopeService;
class UserService
{
@@ -447,7 +447,7 @@ class UserService
{
$ids = array_values(array_unique(array_map('intval', $roleIds)));
$ids = array_filter($ids, static fn ($id) => $id > 0);
$validIds = RoleRepository::listIds();
$validIds = RoleRepository::listActiveIds();
if ($validIds) {
$validMap = array_fill_keys($validIds, true);
$ids = array_values(array_filter($ids, static fn ($id) => isset($validMap[$id])));
@@ -520,8 +520,9 @@ class UserService
private static function normalizeTheme($value): string
{
$theme = strtolower(trim((string) $value));
if (!in_array($theme, ['dark', 'light'], true)) {
return 'light';
$themes = function_exists('appThemes') ? appThemes() : ['light' => 'Light', 'dark' => 'Dark'];
if ($theme === '' || !isset($themes[$theme])) {
return function_exists('appDefaultTheme') ? appDefaultTheme() : 'light';
}
return $theme;
}