Users module: accounts, groups, permissions with needs, manager:create-user
This commit is contained in:
1 parent
08289db744
commit
f86f5aea28
25 files changed
+744
-58
No files matched your search
@@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use IriksIT\Manager\Facades\Manager;
|
||||
use IriksIT\Manager\Models\Group;
|
||||
use IriksIT\Manager\Tests\Fixtures\User;
|
||||
use IriksIT\Manager\Tests\TestCase;
|
||||
|
||||
uses(TestCase::class);
|
||||
@@ -28,18 +28,24 @@ it('adds all of it for a module that is on', function () {
|
||||
});
|
||||
|
||||
it('lets a super admin in and keeps others out without the permission', function () {
|
||||
$admin = User::create(['name' => 'a', 'email' => '[email protected]', 'password' => 'x', 'manager_super_admin' => true]);
|
||||
$other = User::create(['name' => 'b', 'email' => '[email protected]', 'password' => 'x']);
|
||||
|
||||
$this->actingAs($admin)->getJson('/manager/posts')->assertOk();
|
||||
$this->actingAs($other)->getJson('/manager/posts')->assertForbidden();
|
||||
$this->actingAs(makeUser(superAdmin: true))->getJson('/manager/posts')->assertOk();
|
||||
$this->actingAs(makeUser())->getJson('/manager/posts')->assertForbidden();
|
||||
});
|
||||
|
||||
it('gives a group member the permissions of the group', function () {
|
||||
$user = User::create(['name' => 'c', 'email' => '[email protected]', 'password' => 'x']);
|
||||
$group = Group::create(['name' => 'Editors', 'permissions' => ['posts.view']]);
|
||||
$user->managerGroups()->attach($group);
|
||||
it('gives a group member the permissions of the group, with what they need', function () {
|
||||
$user = makeUser(['posts.publish']);
|
||||
|
||||
expect($user->managerPermissions())->toBe(['posts.view', 'posts.write', 'posts.publish']);
|
||||
$this->actingAs($user)->getJson('/manager/posts')->assertOk();
|
||||
$this->actingAs($user)->getJson('/manager/pages')->assertForbidden();
|
||||
});
|
||||
|
||||
it('forgets what a switched-off module allowed', function () {
|
||||
$group = Group::create(['name' => 'Old', 'permissions' => ['posts.view', 'reviews.manage']]);
|
||||
|
||||
expect($group->allowed())->toBe(['posts.view']);
|
||||
});
|
||||
|
||||
it('does not answer for abilities of the host app', function () {
|
||||
expect(Gate::forUser(makeUser(superAdmin: true))->allows('host.something'))->toBeFalse();
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use IriksIT\Manager\Models\Group;
|
||||
use IriksIT\Manager\Tests\Fixtures\User;
|
||||
use IriksIT\Manager\Tests\TestCase;
|
||||
|
||||
uses(TestCase::class);
|
||||
|
||||
describe('groups', function () {
|
||||
beforeEach(fn () => $this->actingAs(makeUser(superAdmin: true)));
|
||||
|
||||
it('lists the groups with what they allow and everything that can be allowed', function () {
|
||||
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
||||
makeUser()->managerGroups()->attach($group);
|
||||
|
||||
$this->getJson('/manager/groups')->assertOk()
|
||||
->assertJsonPath('groups.0.name', 'Writers')
|
||||
->assertJsonPath('groups.0.permissions', ['posts.view', 'posts.write'])
|
||||
->assertJsonPath('groups.0.accounts', 1);
|
||||
|
||||
$values = collect($this->getJson('/manager/groups')->json('catalogue'))->flatMap(fn ($s) => collect($s['permissions'])->pluck('value'))->all();
|
||||
expect($values)->toContain('accounts.manage', 'media.upload')->not->toContain('groups.manage', 'reviews.manage');
|
||||
});
|
||||
|
||||
it('creates a group, with what its permissions need', function () {
|
||||
$this->postJson('/manager/groups', ['name' => ' Editors ', 'description' => 'Publish', 'permissions' => ['posts.publish', 'media.upload']])->assertCreated();
|
||||
|
||||
expect(Group::sole()->permissions)->toBe(['media.view', 'media.upload', 'posts.view', 'posts.write', 'posts.publish']);
|
||||
});
|
||||
|
||||
it('refuses a name that is taken, a permission that does not exist and groups.manage', function () {
|
||||
Group::create(['name' => 'Writers']);
|
||||
|
||||
$this->postJson('/manager/groups', ['name' => 'Writers'])->assertJsonValidationErrors('name');
|
||||
$this->postJson('/manager/groups', ['name' => 'Others', 'permissions' => ['groups.manage']])->assertJsonValidationErrors('permissions.0');
|
||||
expect(Group::count())->toBe(1);
|
||||
});
|
||||
|
||||
it('changes what a group allows, which its accounts feel at once', function () {
|
||||
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
||||
$writer = makeUser();
|
||||
$writer->managerGroups()->attach($group);
|
||||
|
||||
$this->putJson("/manager/groups/{$group->id}", ['name' => 'Writers', 'permissions' => ['pages.edit']])->assertOk();
|
||||
|
||||
$this->actingAs($writer->refresh())->getJson('/manager/posts')->assertForbidden();
|
||||
$this->getJson('/manager/pages')->assertOk();
|
||||
});
|
||||
|
||||
it('deletes a group and leaves its accounts, without what it allowed', function () {
|
||||
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
||||
$writer = makeUser();
|
||||
$writer->managerGroups()->attach($group);
|
||||
|
||||
$this->deleteJson("/manager/groups/{$group->id}")->assertOk();
|
||||
|
||||
expect(Group::count())->toBe(0)->and($writer->refresh()->managerPermissions())->toBe([]);
|
||||
});
|
||||
|
||||
it('is not for someone who manages accounts but is no super admin', function () {
|
||||
$this->actingAs(makeUser(['accounts.manage']));
|
||||
|
||||
$this->getJson('/manager/groups')->assertForbidden();
|
||||
$this->postJson('/manager/groups', ['name' => 'Mine'])->assertForbidden();
|
||||
expect(Group::where('name', 'Mine')->exists())->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('accounts', function () {
|
||||
it('creates an account with a hashed password, never as super admin', function () {
|
||||
$this->actingAs(makeUser(superAdmin: true));
|
||||
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
||||
|
||||
$this->postJson('/manager/accounts', ['name' => 'Ann', 'email' => ' [email protected] ', 'password' => 'a-long-password-1', 'groups' => [$group->id], 'is_super_admin' => true])->assertCreated();
|
||||
|
||||
$ann = User::where('email', '[email protected]')->sole();
|
||||
expect(Hash::check('a-long-password-1', $ann->password))->toBeTrue()
|
||||
->and($ann->isManagerSuperAdmin())->toBeFalse()
|
||||
->and($ann->managerGroups->pluck('name')->all())->toBe(['Writers']);
|
||||
});
|
||||
|
||||
it('refuses a short password and an address that is in use', function () {
|
||||
$me = makeUser(superAdmin: true);
|
||||
$this->actingAs($me);
|
||||
|
||||
$this->postJson('/manager/accounts', ['name' => 'A', 'email' => '[email protected]', 'password' => 'short'])->assertJsonValidationErrors('password');
|
||||
$this->postJson('/manager/accounts', ['name' => 'A', 'email' => $me->email, 'password' => 'a-long-password-1'])->assertJsonValidationErrors('email');
|
||||
});
|
||||
|
||||
it('does not let someone hand out more than they have', function () {
|
||||
$this->actingAs(makeUser(['accounts.manage', 'posts.write']));
|
||||
$big = Group::create(['name' => 'Big', 'permissions' => ['pages.edit']]);
|
||||
|
||||
$this->postJson('/manager/accounts', ['name' => 'B', 'email' => '[email protected]', 'password' => 'a-long-password-1', 'groups' => [$big->id]])->assertJsonValidationErrors('groups');
|
||||
expect(User::where('email', '[email protected]')->exists())->toBeFalse();
|
||||
});
|
||||
|
||||
it('does not let someone change or delete a mightier account, or themselves', function () {
|
||||
$me = makeUser(['accounts.manage']);
|
||||
$mighty = makeUser(['accounts.manage', 'posts.write']);
|
||||
$boss = makeUser(superAdmin: true);
|
||||
$this->actingAs($me);
|
||||
|
||||
$this->putJson("/manager/accounts/{$mighty->id}", ['name' => 'X', 'email' => '[email protected]', 'password' => 'a-long-password-1'])->assertForbidden();
|
||||
$this->deleteJson("/manager/accounts/{$boss->id}")->assertForbidden();
|
||||
$this->deleteJson("/manager/accounts/{$me->id}")->assertStatus(422);
|
||||
});
|
||||
|
||||
it('keeps the password when it is left empty, and removes the group links on delete', function () {
|
||||
$this->actingAs(makeUser(superAdmin: true));
|
||||
$user = makeUser(['posts.view']);
|
||||
$hash = $user->password;
|
||||
|
||||
$this->putJson("/manager/accounts/{$user->id}", ['name' => 'New name', 'email' => $user->email, 'password' => ''])->assertOk();
|
||||
expect($user->refresh()->name)->toBe('New name')->and($user->password)->toBe($hash);
|
||||
|
||||
$this->deleteJson("/manager/accounts/{$user->id}")->assertOk();
|
||||
expect(DB::table('manager_group_user')->count())->toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('makes a super admin only with the command', function () {
|
||||
$this->artisan('manager:create-user', ['--name' => 'Root', '--email' => '[email protected]', '--password' => 'a-long-password-1'])->assertSuccessful();
|
||||
|
||||
$root = User::where('email', '[email protected]')->sole();
|
||||
expect($root->isManagerSuperAdmin())->toBeTrue()->and(Hash::check('a-long-password-1', $root->password))->toBeTrue();
|
||||
|
||||
$this->artisan('manager:create-user', ['--name' => 'Root', '--email' => '[email protected]', '--password' => 'a-long-password-1'])->assertFailed();
|
||||
$this->artisan('manager:create-user', ['--name' => 'Root', '--email' => '[email protected]', '--password' => 'short'])->assertFailed();
|
||||
});
|
||||
Reference in new issue
Block a user