diff --git a/AGENTS.md b/AGENTS.md index 8cee59e..dd01a61 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,4 +21,4 @@ between lil-koneko.com, CarsHub, iriks-it.nl and bouwbedrijf.iriks-it.nl, as one 1. Frontend: decided and built: the screens are in the package (`ui/`, built into `resources/dist`, committed; PR CI checks it is up to date). Still open: a theme editor in the manager, and a `host` mode that publishes the sources for a site to build itself. 2. Moving the rest from bouwbedrijf.iriks-it.nl: real pages and reviews (now skeletons) and revisions. Users, media, posts, audit log and statistics are done, with their screens. Leave out what is site-specific (the IriksIT API disk, the owner's simple screens, client names): this repo is public. -3. How the host's own `User` and a package `Group` relate when the host has its own roles. +3. Decided in 0.5.0: groups stay the default and the host's roles are added next to them (`manager.permissions`: `mode` groups, host or merge; `roles` or a `PermissionResolver`). A host that wants to replace the groups screens entirely is not planned. diff --git a/CHANGELOG.md b/CHANGELOG.md index 0386886..4613ea7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ the section is the text of the release in Gitea. - Statistics: `` puts the counting script (`/pulse/pulse.js`, no cookies, honours Do Not Track) in the public pages of a site. - Login checks: `manager.login.checks` (contract `LoginCheck`, base class `BaseLoginCheck`) adds fields and checks before and after the password; Cloudflare Turnstile is built in (two env keys). No two-factor login in the package itself. +- Roles of the host: `manager.permissions` (`mode` groups/host/merge, `roles` map or a `resolver` implementing `PermissionResolver`) lets a site + with its own roles say what an account may do; groups stay the default. ## v0.4.0 - 2026-12-04 diff --git a/README.md b/README.md index 6e1ad7f..145dc92 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,12 @@ php artisan manager:create-user # the only way to make a super admin - `groups.manage` is a gate for super admins only, not a permission of a group. - The package defines one gate per permission and **no `Gate::before`**: it never answers for the abilities of your own app. +- **A site with roles of its own** (decided in 0.5.0: groups stay the default, the site's roles are added next to them). + `manager.permissions.mode` is `groups` (default), `host` (the groups are ignored) or `merge` (both). The site says what a role may do + with `roles` (`['editor' => ['posts.view', 'posts.write'], 'owner' => ['*']]`, read from the attribute `role_attribute`: a string, a + list or a relation with a `name`) or with a `resolver` class that implements `IriksIT\Manager\Contracts\PermissionResolver`. Values + that are no permission of an active module are ignored and what a permission needs is added. A super admin may always do everything. + The group screens still work in `host` mode but give nothing. - The migration adds `is_super_admin` to your users table (the table of `manager.user_model`). It promotes nobody. diff --git a/TOKENS.md b/TOKENS.md index a420ff2..fda5242 100644 --- a/TOKENS.md +++ b/TOKENS.md @@ -42,3 +42,4 @@ Per stuk werk een rij; zie `dev/token-usage.py`. | 2026-12-04 | De editor: opmaakknoppen, foto's in de tekst, voorbeeld en een scherpstelpunt in de bibliotheek | - | claude-sonnet-5-5 | 15 | 18.397 | 30 | 28.742 | 3.931.230 | 0,07 | | 2026-12-04 | Het telscript en de Blade-component voor de publieke pagina's van de statistieken | - | claude-sonnet-5-5 | 8 | 9.037 | 16 | 15.948 | 2.254.948 | 0,04 | | 2026-12-04 | Loginhaken: eigen velden en controles, en Turnstile | - | claude-sonnet-5-5 | 4 | 9.009 | 8 | 9.515 | 1.178.544 | 0,02 | +| 2026-12-04 | Rollen van de site naast de groepen van het pakket | - | claude-sonnet-5-5 | 3 | 5.308 | 6 | 8.235 | 921.246 | 0,01 | diff --git a/config/manager.php b/config/manager.php index 7dfae4c..a7e8319 100644 --- a/config/manager.php +++ b/config/manager.php @@ -195,6 +195,15 @@ return [ 'turnstile' => ['site_key' => env('MANAGER_TURNSTILE_SITE_KEY'), 'secret' => env('MANAGER_TURNSTILE_SECRET')], ], + /* + | Who may do what. `groups` (default): the groups of the package. A site that has roles of its own can say it itself: + | `host` takes the permissions only from the site, `merge` adds them to what the groups give. The site's side is a + | `resolver` class (IriksIT\Manager\Contracts\PermissionResolver), or, simpler, `roles` per role name (`*` is + | everything) read from the attribute `role_attribute` of the account (a string, a list or a relation with a `name`). + | 'roles' => ['editor' => ['posts.view', 'posts.write'], 'owner' => ['*']]. A super admin may always do everything. + */ + 'permissions' => ['mode' => env('MANAGER_PERMISSIONS', 'groups'), 'resolver' => null, 'roles' => [], 'role_attribute' => 'role'], + /* | The menu. `items` changes what a module gives, by the route name without the prefix: | 'posts.index' => ['order' => 10, 'section' => 'Content', 'icon' => 'file', 'simple' => true, 'hidden' => false]. diff --git a/src/Concerns/HasManagerAccess.php b/src/Concerns/HasManagerAccess.php index 166c995..2611f3c 100644 --- a/src/Concerns/HasManagerAccess.php +++ b/src/Concerns/HasManagerAccess.php @@ -6,12 +6,13 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; use IriksIT\Manager\Models\Group; use IriksIT\Manager\Support\ModuleRegistry; use IriksIT\Manager\Support\Names; +use IriksIT\Manager\Support\Permissions\Sources; /** * Put this on the User model of the host app (needs the Users module). * * A super admin may do everything of the manager. Everyone else may do what any of their - * groups allows. `is_super_admin` is never fillable and no form sets it: only + * groups allows, or (config `manager.permissions`) what the roles of the site say. `is_super_admin` is never fillable and no form sets it: only * `manager:create-user` makes one. Keep it out of `$fillable` in the host model. */ trait HasManagerAccess @@ -40,7 +41,9 @@ trait HasManagerAccess return $registry->permissionValues(); } - return $registry->complete($this->managerGroups->flatMap(fn (Group $group) => $group->allowed())->all()); + $fromGroups = Sources::usesGroups() ? $this->managerGroups->flatMap(fn (Group $group) => $group->allowed())->all() : []; + + return $registry->complete([...$fromGroups, ...Sources::fromHost($this)]); } public function hasManagerPermission(string $permission): bool diff --git a/src/Contracts/PermissionResolver.php b/src/Contracts/PermissionResolver.php new file mode 100644 index 0000000..17ccd59 --- /dev/null +++ b/src/Contracts/PermissionResolver.php @@ -0,0 +1,15 @@ + permission values such as `posts.write` */ + public function permissions(Authenticatable $user): array; +} diff --git a/src/Support/Permissions/RoleMap.php b/src/Support/Permissions/RoleMap.php new file mode 100644 index 0000000..6034bd1 --- /dev/null +++ b/src/Support/Permissions/RoleMap.php @@ -0,0 +1,33 @@ + ['posts.view', 'posts.write'], 'owner' => ['*']]` (`*` is everything). The attribute is + * `manager.permissions.role_attribute`. + */ +final class RoleMap implements PermissionResolver +{ + public function permissions(Authenticatable $user): array + { + $roles = $user->{(string) config('manager.permissions.role_attribute', 'role')} ?? []; + $roles = $roles instanceof Collection ? $roles->all() : (array) $roles; + $map = (array) config('manager.permissions.roles', []); + $out = []; + + foreach ($roles as $role) { + $name = is_object($role) ? (string) ($role->name ?? '') : (string) (is_scalar($role) ? $role : ''); + + foreach ((array) ($map[$name] ?? []) as $permission) { + $out[] = (string) $permission; + } + } + + return array_values(array_unique($out)); + } +} diff --git a/src/Support/Permissions/Sources.php b/src/Support/Permissions/Sources.php new file mode 100644 index 0000000..638f43c --- /dev/null +++ b/src/Support/Permissions/Sources.php @@ -0,0 +1,45 @@ + 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'; + } +} diff --git a/tests/Feature/PermissionSourcesTest.php b/tests/Feature/PermissionSourcesTest.php new file mode 100644 index 0000000..e537723 --- /dev/null +++ b/tests/Feature/PermissionSourcesTest.php @@ -0,0 +1,68 @@ +email, 'editor') ? ['posts.write', 'posts.view', 'nonsense.thing'] : []; + } +} + +function editor(string $role = 'editor', array $groups = []): User +{ + $user = makeUser($groups); + $user->setAttribute('role', $role); + + return $user; +} + +it('uses the groups only, as before', function () { + config(['manager.permissions.roles' => ['editor' => ['posts.view']]]); + + expect(editor('editor', ['media.view'])->managerPermissions())->toBe(['media.view']); +}); + +it('takes the permissions from the roles of the site in host mode, and not from the groups', function () { + config(['manager.permissions' => ['mode' => 'host', 'roles' => ['editor' => ['posts.write', 'unknown.thing'], 'owner' => ['*']]] + config('manager.permissions')]); + + expect(editor('editor', ['media.view'])->managerPermissions())->toBe(['posts.view', 'posts.write']) + ->and(editor('owner')->managerPermissions())->toBe(app(ModuleRegistry::class)->permissionValues()) + ->and(editor('nobody')->managerPermissions())->toBe([]); + + $this->actingAs(editor('editor')); + $this->getJson('/manager/posts')->assertOk(); + $this->getJson('/manager/media/items')->assertForbidden(); +}); + +it('adds the roles to the groups in merge mode, and reads a list of roles', function () { + config(['manager.permissions' => ['mode' => 'merge', 'roles' => ['editor' => ['posts.view'], 'writer' => ['posts.write']]] + config('manager.permissions')]); + + $user = editor('editor', ['media.view']); + $user->setAttribute('role', ['editor', 'writer']); + + expect($user->managerPermissions())->toEqualCanonicalizing(['posts.view', 'posts.write', 'media.view']); +}); + +it('asks a resolver of the site, which can only give permissions that exist', function () { + config(['manager.permissions' => ['mode' => 'host', 'resolver' => FromTheSite::class] + config('manager.permissions')]); + + $user = makeUser(); + $user->forceFill(['email' => 'editor@example.com'])->save(); + + expect($user->managerPermissions())->toBe(['posts.view', 'posts.write']) + ->and(makeUser()->managerPermissions())->toBe([]); +}); + +it('keeps a super admin all-powerful in every mode', function () { + config(['manager.permissions.mode' => 'host']); + + expect(makeUser(superAdmin: true)->managerPermissions())->toContain('posts.view', 'media.view'); +});