PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 5s
PR CI / ui (pull_request) Successful in 17s
PR CI / test (lowest) (pull_request) Successful in 39s
PR CI / security (pull_request) Successful in 48s
PR CI / test (stable) (pull_request) Successful in 53s
PR CI / pr-ci (pull_request) Successful in 2s
PR CI / release-automerge (pull_request) Successful in 4s
46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Support\Permissions;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use IriksIT\Manager\Contracts\PermissionResolver;
|
|
use IriksIT\Manager\Support\ModuleRegistry;
|
|
|
|
/**
|
|
* What an account may do comes from the groups of the package (the default), from the roles of the site, or from both:
|
|
* `manager.permissions.mode` is `groups`, `host` or `merge`. A site's side is its own `resolver` class, or the built-in `RoleMap`
|
|
* when `roles` is filled. A super admin always may everything.
|
|
*/
|
|
final class Sources
|
|
{
|
|
public static function usesGroups(): bool
|
|
{
|
|
return self::mode() !== 'host';
|
|
}
|
|
|
|
/** @return list<string> what the roles of the site allow (empty in `groups` mode) */
|
|
public static function fromHost(Authenticatable $user): array
|
|
{
|
|
if (self::mode() === 'groups') {
|
|
return [];
|
|
}
|
|
|
|
$class = config('manager.permissions.resolver');
|
|
$resolver = is_string($class) && is_subclass_of($class, PermissionResolver::class) ? app($class) : (config('manager.permissions.roles') ? new RoleMap : null);
|
|
|
|
if ($resolver === null) {
|
|
return [];
|
|
}
|
|
|
|
$known = app(ModuleRegistry::class)->permissionValues();
|
|
$given = $resolver->permissions($user);
|
|
|
|
return in_array('*', $given, true) ? $known : array_values(array_filter($given, fn ($value) => is_string($value) && in_array($value, $known, true)));
|
|
}
|
|
|
|
private static function mode(): string
|
|
{
|
|
return in_array(config('manager.permissions.mode'), ['groups', 'host', 'merge'], true) ? config('manager.permissions.mode') : 'groups';
|
|
}
|
|
}
|