Loheide-eu-mitarbeiterplatt.../app/Plugins/PluginAccess.php
Friederich Loheide 1877a0922f
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m45s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m49s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m21s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Rollen ausschließen: bei Inhalten (Zielgruppen) und für ganze Module
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:30:11 +00:00

86 lines
2.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Plugins;
use App\Models\User;
use App\Support\Settings;
/**
* Module für Rollen sperren (Administration → Plugins → „Zugang“).
*
* Für ausgeschlossene Rollen verschwindet ein Plugin vollständig: Menüpunkt,
* Seiten (403), Startseiten-Kacheln, Pagebuilder-Blöcke und Suchtreffer. Zugeordnet
* wird über das Kürzel – Schlüssel wie „news“ oder „news.hero“ gehören zu „news“.
* Wer Plugins verwalten darf, ist nie ausgeschlossen.
*/
class PluginAccess
{
public const SETTING = 'plugins.excluded_roles';
/** @var array<int, list<string>> Person => gesperrte Kürzel (je Anfrage) */
private array $blocked = [];
public function __construct(private readonly Settings $settings) {}
/**
* @return array<string, list<int>> Kürzel => Rollen-IDs
*/
public function all(): array
{
return array_filter(array_map(
fn ($ids) => array_values(array_unique(array_map('intval', (array) $ids))),
(array) $this->settings->get(self::SETTING, []),
));
}
/**
* @return list<int>
*/
public function excludedRoles(string $slug): array
{
return $this->all()[$slug] ?? [];
}
/**
* @param list<int|string> $roleIds
*/
public function exclude(string $slug, array $roleIds): void
{
$all = $this->all();
$all[$slug] = array_values(array_unique(array_map('intval', $roleIds)));
$this->settings->set(self::SETTING, array_filter($all));
$this->blocked = [];
}
public function allows(string $key, ?User $user): bool
{
return ! in_array(self::slugOf($key), $this->blockedFor($user), true);
}
/**
* @return list<string>
*/
public function blockedFor(?User $user): array
{
if (! $user) {
return [];
}
return $this->blocked[$user->id] ??= (function () use ($user) {
$all = $this->all();
if ($all === [] || $user->can('plugins.manage')) {
return [];
}
$roleIds = array_map('intval', $user->permissions()->roleIds);
return array_values(array_keys(array_filter($all, fn (array $ids) => array_intersect($ids, $roleIds) !== [])));
})();
}
public static function slugOf(string $key): string
{
return explode('.', $key, 2)[0];
}
}