mining checker
This commit is contained in:
71
src/ModulesCore/ModuleHttp.php
Normal file
71
src/ModulesCore/ModuleHttp.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ModulesCore;
|
||||
|
||||
use App\AccountGate;
|
||||
use App\ConfigLoader;
|
||||
use App\KeycloakAuth;
|
||||
|
||||
final class ModuleHttp
|
||||
{
|
||||
public static function requireDesktopAccess(string $projectRoot, bool $json = false): void
|
||||
{
|
||||
$auth = new KeycloakAuth(ConfigLoader::load($projectRoot, 'keycloak'));
|
||||
$accountGate = new AccountGate(ConfigLoader::load($projectRoot, 'registration'));
|
||||
|
||||
if ($auth->isAuthenticated()) {
|
||||
$currentUser = is_array($_SESSION['desktop_auth']['user'] ?? null) ? $_SESSION['desktop_auth']['user'] : [];
|
||||
$accountCheck = $accountGate->checkUsername((string) ($currentUser['username'] ?? ''));
|
||||
|
||||
if (!($accountCheck['allowed'] ?? false)) {
|
||||
$auth->logout();
|
||||
$message = (string) ($accountCheck['message'] ?? 'Dieses Konto ist noch nicht freigeschaltet.');
|
||||
|
||||
if ($json) {
|
||||
self::respondJson(['error' => $message], 403);
|
||||
}
|
||||
|
||||
$target = (string) ($accountCheck['state'] ?? '') === 'pending'
|
||||
? '/auth/pending/?username=' . urlencode((string) ($currentUser['username'] ?? ''))
|
||||
: '/auth/inactive/?message=' . urlencode($message);
|
||||
header('Location: ' . $target, true, 302);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$auth->shouldShowDesktop()) {
|
||||
if ($json) {
|
||||
self::respondJson(['error' => 'Nicht autorisiert.'], 401);
|
||||
}
|
||||
|
||||
header('Location: /auth/keycloak', true, 302);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public static function currentUserScope(): string
|
||||
{
|
||||
$auth = $_SESSION['desktop_auth'] ?? null;
|
||||
|
||||
if (!is_array($auth)) {
|
||||
return 'guest';
|
||||
}
|
||||
|
||||
$user = is_array($auth['user'] ?? null) ? $auth['user'] : [];
|
||||
|
||||
return (string) ($user['sub'] ?? $user['username'] ?? 'guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
private static function respondJson(array $payload, int $statusCode): never
|
||||
{
|
||||
http_response_code($statusCode);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
105
src/ModulesCore/ModuleRegistry.php
Normal file
105
src/ModulesCore/ModuleRegistry.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ModulesCore;
|
||||
|
||||
final class ModuleRegistry
|
||||
{
|
||||
/** @var array<int, array<string, mixed>>|null */
|
||||
private ?array $desktopApps = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $modulesPath,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function desktopApps(): array
|
||||
{
|
||||
if ($this->desktopApps !== null) {
|
||||
return $this->desktopApps;
|
||||
}
|
||||
|
||||
$apps = [];
|
||||
|
||||
foreach ($this->moduleDirectories() as $moduleName => $modulePath) {
|
||||
$desktopConfigPath = $modulePath . '/desktop.php';
|
||||
if (!is_file($desktopConfigPath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$desktopConfig = require $desktopConfigPath;
|
||||
if (!is_array($desktopConfig)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$manifest = $this->readJson($modulePath . '/module.json');
|
||||
$desktopConfig['module_id'] = $moduleName;
|
||||
$desktopConfig['title'] ??= (string) ($manifest['title'] ?? ucfirst($moduleName));
|
||||
$desktopConfig['summary'] ??= (string) ($manifest['description'] ?? '');
|
||||
$desktopConfig['required_groups'] ??= array_values(array_map(
|
||||
'strval',
|
||||
(array) ($manifest['auth']['groups'] ?? [])
|
||||
));
|
||||
$desktopConfig['enabled_by_default'] ??= (bool) ($manifest['enabled_by_default'] ?? false);
|
||||
$apps[] = $desktopConfig;
|
||||
}
|
||||
|
||||
return $this->desktopApps = $apps;
|
||||
}
|
||||
|
||||
public function modulePath(string $moduleName): ?string
|
||||
{
|
||||
$directories = $this->moduleDirectories();
|
||||
|
||||
return $directories[$moduleName] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function moduleDirectories(): array
|
||||
{
|
||||
$paths = glob(rtrim($this->modulesPath, '/') . '/*', GLOB_ONLYDIR);
|
||||
if (!is_array($paths)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$directories = [];
|
||||
|
||||
foreach ($paths as $path) {
|
||||
$moduleName = basename($path);
|
||||
if ($moduleName === '' || $moduleName[0] === '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$directories[$moduleName] = $path;
|
||||
}
|
||||
|
||||
ksort($directories);
|
||||
|
||||
return $directories;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function readJson(string $path): array
|
||||
{
|
||||
if (!is_file($path)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$raw = file_get_contents($path);
|
||||
if ($raw === false || trim($raw) === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$decoded = json_decode($raw, true);
|
||||
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user