2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Support;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Http\Request;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\Router;
|
|
|
|
|
use MintyPHP\Service\Access\AccessServicesFactory;
|
|
|
|
|
use MintyPHP\Service\Access\PermissionGateway;
|
|
|
|
|
use MintyPHP\Service\Auth\AuthServicesFactory;
|
|
|
|
|
use MintyPHP\Service\Directory\DirectoryServicesFactory;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
class Guard
|
|
|
|
|
{
|
|
|
|
|
private const TENANT_REFRESH_INTERVAL = 60;
|
2026-02-23 12:58:19 +01:00
|
|
|
private static ?AccessServicesFactory $accessServicesFactory = null;
|
|
|
|
|
private static ?PermissionGateway $permissionGateway = null;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
public static function requireLogin(string $redirect = 'login'): void
|
|
|
|
|
{
|
|
|
|
|
if (!isset($_SESSION['user'])) {
|
|
|
|
|
Flash::error('Login required', 'login', 'login_required');
|
|
|
|
|
Router::redirect($redirect);
|
|
|
|
|
}
|
|
|
|
|
self::refreshTenantContext();
|
|
|
|
|
if (!empty($_SESSION['no_active_tenant'])) {
|
2026-02-23 12:58:19 +01:00
|
|
|
(new AuthServicesFactory())->createAuthService()->logout();
|
2026-02-04 23:31:53 +01:00
|
|
|
Flash::error('No active tenant assigned', 'login', 'no_active_tenant');
|
|
|
|
|
Router::redirect($redirect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function validateCurrentTenant(): void
|
|
|
|
|
{
|
|
|
|
|
$currentTenant = $_SESSION['current_tenant'] ?? null;
|
|
|
|
|
if (!$currentTenant || empty($currentTenant['id'])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$tenantId = (int) $currentTenant['id'];
|
2026-02-23 12:58:19 +01:00
|
|
|
$tenant = (new DirectoryServicesFactory())->createTenantService()->findById($tenantId);
|
2026-02-04 23:31:53 +01:00
|
|
|
if ($tenant) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Current tenant was deleted, refresh session data
|
|
|
|
|
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
|
|
|
|
if ($userId > 0) {
|
2026-02-23 12:58:19 +01:00
|
|
|
(new AuthServicesFactory())->createAuthService()->loadTenantDataIntoSession($userId);
|
2026-02-04 23:31:53 +01:00
|
|
|
} else {
|
|
|
|
|
unset($_SESSION['current_tenant']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function refreshTenantContext(): void
|
|
|
|
|
{
|
|
|
|
|
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$last = (int) ($_SESSION['tenant_context_refreshed_at'] ?? 0);
|
|
|
|
|
$now = time();
|
|
|
|
|
if ($last > 0 && ($now - $last) < self::TENANT_REFRESH_INTERVAL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-23 12:58:19 +01:00
|
|
|
(new AuthServicesFactory())->createAuthService()->loadTenantDataIntoSession($userId);
|
2026-02-04 23:31:53 +01:00
|
|
|
$_SESSION['tenant_context_refreshed_at'] = $now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function requirePermission(string $permissionKey, string $redirect = 'admin'): void
|
|
|
|
|
{
|
|
|
|
|
self::requireLogin();
|
|
|
|
|
self::validateCurrentTenant();
|
|
|
|
|
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
2026-02-23 12:58:19 +01:00
|
|
|
if (!$userId || !self::permissionGateway()->userHas($userId, $permissionKey)) {
|
2026-02-04 23:31:53 +01:00
|
|
|
Flash::error('Permission denied', $redirect, 'permission_denied');
|
|
|
|
|
Router::redirect($redirect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function requirePermissionOrForbidden(string $permissionKey): void
|
|
|
|
|
{
|
|
|
|
|
self::requireLogin();
|
|
|
|
|
self::validateCurrentTenant();
|
|
|
|
|
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
2026-02-23 12:58:19 +01:00
|
|
|
if (!$userId || !self::permissionGateway()->userHas($userId, $permissionKey)) {
|
2026-02-04 23:31:53 +01:00
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
|
echo json_encode(['error' => 'forbidden']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
Router::redirect('error/forbidden?url=' . urlencode(Request::pathWithQuery()));
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-23 12:58:19 +01:00
|
|
|
|
|
|
|
|
private static function permissionGateway(): PermissionGateway
|
|
|
|
|
{
|
|
|
|
|
if (self::$permissionGateway instanceof PermissionGateway) {
|
|
|
|
|
return self::$permissionGateway;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$permissionGateway = self::accessServicesFactory()->createPermissionGateway();
|
|
|
|
|
return self::$permissionGateway;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function accessServicesFactory(): AccessServicesFactory
|
|
|
|
|
{
|
|
|
|
|
if (self::$accessServicesFactory instanceof AccessServicesFactory) {
|
|
|
|
|
return self::$accessServicesFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$accessServicesFactory = new AccessServicesFactory();
|
|
|
|
|
return self::$accessServicesFactory;
|
|
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|