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
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
86 lines
2.4 KiB
PHP
86 lines
2.4 KiB
PHP
<?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];
|
||
}
|
||
}
|