Let the roles of the host decide next to the groups
This commit is contained in:
1 parent
258c25efe1
commit
69ed8c742a
10 files changed
+185
-3
No files matched your search
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use IriksIT\Manager\Contracts\PermissionResolver;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
use IriksIT\Manager\Tests\Fixtures\User;
|
||||
use IriksIT\Manager\Tests\TestCase;
|
||||
|
||||
uses(TestCase::class);
|
||||
|
||||
class FromTheSite implements PermissionResolver
|
||||
{
|
||||
public function permissions(Authenticatable $user): array
|
||||
{
|
||||
return str_starts_with($user->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' => '[email protected]'])->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');
|
||||
});
|
||||
Reference in new issue
Block a user