mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-26 12:59:31 +00:00
Save permissions as JSON in user/group entities.
This commit is contained in:
parent
33f8d2ba9e
commit
687ee80255
9 changed files with 316 additions and 40 deletions
104
tests/Entity/UserSystem/PermissionDataTest.php
Normal file
104
tests/Entity/UserSystem/PermissionDataTest.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Entity\UserSystem;
|
||||
|
||||
use App\Entity\UserSystem\PermissionData;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PermissionDataTest extends TestCase
|
||||
{
|
||||
|
||||
public function testGetSetIs()
|
||||
{
|
||||
$perm_data = new PermissionData();
|
||||
|
||||
//Empty object should have all permissions set to inherit
|
||||
$this->assertNull($perm_data->getPermissionValue('not_existing', 'not_existing'));
|
||||
$this->assertFalse($perm_data->isPermissionSet('not_existing', 'not_existing'));
|
||||
|
||||
$this->assertNull($perm_data->getPermissionValue('p1', 'op1'));
|
||||
$this->assertNull($perm_data->getPermissionValue('p1', 'op2'));
|
||||
$this->assertNull($perm_data->getPermissionValue('p2', 'op1'));
|
||||
|
||||
//Set values
|
||||
$perm_data->setPermissionValue('p1', 'op1', PermissionData::ALLOW);
|
||||
$perm_data->setPermissionValue('p1', 'op2', PermissionData::DISALLOW);
|
||||
$perm_data->setPermissionValue('p2', 'op1', PermissionData::ALLOW);
|
||||
|
||||
//Check that values were set
|
||||
$this->assertTrue($perm_data->isPermissionSet('p1', 'op1'));
|
||||
$this->assertTrue($perm_data->isPermissionSet('p1', 'op2'));
|
||||
$this->assertTrue($perm_data->isPermissionSet('p2', 'op1'));
|
||||
|
||||
//Check that values are correct
|
||||
$this->assertTrue($perm_data->getPermissionValue('p1', 'op1'));
|
||||
$this->assertFalse($perm_data->getPermissionValue('p1', 'op2'));
|
||||
$this->assertTrue($perm_data->getPermissionValue('p2', 'op1'));
|
||||
|
||||
//Set values to null
|
||||
$perm_data->setPermissionValue('p1', 'op1', null);
|
||||
$this->assertNull($perm_data->getPermissionValue('p1', 'op1'));
|
||||
//Values should be unset now
|
||||
$this->assertFalse($perm_data->isPermissionSet('p1', 'op1'));
|
||||
}
|
||||
|
||||
public function testJSONSerialization()
|
||||
{
|
||||
$perm_data = new PermissionData();
|
||||
|
||||
$perm_data->setPermissionValue('perm1', 'op1', PermissionData::ALLOW);
|
||||
$perm_data->setPermissionValue('perm1', 'op2', PermissionData::DISALLOW);
|
||||
$perm_data->setPermissionValue('perm1', 'op3', PermissionData::ALLOW);
|
||||
|
||||
$perm_data->setPermissionValue('perm2', 'op1', PermissionData::ALLOW);
|
||||
$perm_data->setPermissionValue('perm2', 'op2', PermissionData::DISALLOW);
|
||||
|
||||
//Ensure that JSON serialization works
|
||||
$this->assertJsonStringEqualsJsonString(json_encode([
|
||||
'perm1' => [
|
||||
'op1' => true,
|
||||
'op2' => false,
|
||||
'op3' => true,
|
||||
],
|
||||
'perm2' => [
|
||||
'op1' => true,
|
||||
'op2' => false,
|
||||
],
|
||||
], JSON_THROW_ON_ERROR), json_encode($perm_data, JSON_THROW_ON_ERROR));
|
||||
|
||||
//Set values to inherit to ensure they do not show up in the json
|
||||
$perm_data->setPermissionValue('perm1', 'op3', null);
|
||||
$perm_data->setPermissionValue('perm2', 'op1', null);
|
||||
$perm_data->setPermissionValue('perm2', 'op2', null);
|
||||
|
||||
//Ensure that JSON serialization works
|
||||
$this->assertJsonStringEqualsJsonString(json_encode([
|
||||
'perm1' => [
|
||||
'op1' => true,
|
||||
'op2' => false,
|
||||
],
|
||||
], JSON_THROW_ON_ERROR), json_encode($perm_data, JSON_THROW_ON_ERROR));
|
||||
|
||||
}
|
||||
|
||||
public function testFromJSON()
|
||||
{
|
||||
$json = json_encode([
|
||||
'perm1' => [
|
||||
'op1' => true,
|
||||
'op2' => false,
|
||||
'op3' => true,
|
||||
],
|
||||
'perm2' => [
|
||||
'op1' => true,
|
||||
'op2' => false,
|
||||
],
|
||||
], JSON_THROW_ON_ERROR);
|
||||
|
||||
$perm_data = PermissionData::fromJSON($json);
|
||||
|
||||
//Ensure that values were set correctly
|
||||
$this->assertTrue($perm_data->getPermissionValue('perm1', 'op1'));
|
||||
$this->assertFalse($perm_data->getPermissionValue('perm2', 'op2'));
|
||||
}
|
||||
}
|
||||
|
|
@ -43,6 +43,7 @@ declare(strict_types=1);
|
|||
namespace App\Tests\Services;
|
||||
|
||||
use App\Entity\UserSystem\Group;
|
||||
use App\Entity\UserSystem\PermissionData;
|
||||
use App\Entity\UserSystem\PermissionsEmbed;
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Services\PermissionResolver;
|
||||
|
|
@ -69,42 +70,42 @@ class PermissionResolverTest extends WebTestCase
|
|||
$this->service = self::$container->get(PermissionResolver::class);
|
||||
|
||||
//Set up a mocked user
|
||||
$user_embed = new PermissionsEmbed();
|
||||
$user_embed->setPermissionValue('parts', 0, true) //read
|
||||
->setPermissionValue('parts', 2, false) //edit
|
||||
->setPermissionValue('parts', 4, null) //create
|
||||
->setPermissionValue('parts', 30, null) //move
|
||||
->setPermissionValue('parts', 8, null); //delete
|
||||
$user_perms = new PermissionData();
|
||||
$user_perms->setPermissionValue('parts', 'read', true) //read
|
||||
->setPermissionValue('parts', 'edit', false) //edit
|
||||
->setPermissionValue('parts', 'create', null) //create
|
||||
->setPermissionValue('parts', 'move', null) //move
|
||||
->setPermissionValue('parts', 'delete', null); //delete
|
||||
|
||||
$this->user = $this->createMock(User::class);
|
||||
$this->user->method('getPermissions')->willReturn($user_embed);
|
||||
$this->user->method('getPermissions')->willReturn($user_perms);
|
||||
|
||||
$this->user_withoutGroup = $this->createMock(User::class);
|
||||
$this->user_withoutGroup->method('getPermissions')->willReturn($user_embed);
|
||||
$this->user_withoutGroup->method('getPermissions')->willReturn($user_perms);
|
||||
$this->user_withoutGroup->method('getGroup')->willReturn(null);
|
||||
|
||||
//Set up a faked group
|
||||
$group1_embed = new PermissionsEmbed();
|
||||
$group1_embed->setPermissionValue('parts', 6, true)
|
||||
->setPermissionValue('parts', 8, false)
|
||||
->setPermissionValue('parts', 10, null)
|
||||
->setPermissionValue('parts', 0, false)
|
||||
->setPermissionValue('parts', 30, true)
|
||||
->setPermissionValue('parts', 2, true);
|
||||
$group1_perms = new PermissionData();
|
||||
$group1_perms
|
||||
->setPermissionValue('parts', 'delete', false)
|
||||
->setPermissionValue('parts', 'search', null)
|
||||
->setPermissionValue('parts', 'read', false)
|
||||
->setPermissionValue('parts', 'show_history', true)
|
||||
->setPermissionValue('parts', 'edit', true);
|
||||
|
||||
$this->group = $this->createMock(Group::class);
|
||||
$this->group->method('getPermissions')->willReturn($group1_embed);
|
||||
$this->group->method('getPermissions')->willReturn($group1_perms);
|
||||
|
||||
//Set this group for the user
|
||||
$this->user->method('getGroup')->willReturn($this->group);
|
||||
|
||||
//parent group
|
||||
$parent_group_embed = new PermissionsEmbed();
|
||||
$parent_group_embed->setPermissionValue('parts', 12, true)
|
||||
->setPermissionValue('parts', 14, false)
|
||||
->setPermissionValue('parts', 16, null);
|
||||
$parent_group_perms = new PermissionData();
|
||||
$parent_group_perms->setPermissionValue('parts', 'all_parts', true)
|
||||
->setPermissionValue('parts', 'no_price_parts', false)
|
||||
->setPermissionValue('parts', 'obsolete_parts', null);
|
||||
$parent_group = $this->createMock(Group::class);
|
||||
$parent_group->method('getPermissions')->willReturn($parent_group_embed);
|
||||
$parent_group->method('getPermissions')->willReturn($parent_group_perms);
|
||||
|
||||
$this->group->method('getParent')->willReturn($parent_group);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue