Skip to content

Commit

Permalink
Merge branch 'master' into 251-user-has-role
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Feb 14, 2024
2 parents 82eb212 + 98cf8d7 commit 2637afb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- Enh #245: Handle same names during renaming item in `AssignmentsStorage` (@arogachev)
- Chg #208: Rename `getAccessTree()` to `getHierarchy()` in `ItemsStorageInterface` (@arogachev)
- Enh #251: Allow checking for user's roles in `ManagerInterface::userHasPermission()` (@arogachev)
- Enh #252: Return `$this` instead of throwing "already assigned" exception in `Manager::assign()` (@arogachev)

## 1.0.2 April 20, 2023

Expand Down
4 changes: 1 addition & 3 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public function assign(string $itemName, int|Stringable|string $userId, ?int $cr
}

if ($this->assignmentsStorage->exists($itemName, $userId)) {
throw new InvalidArgumentException(
"\"$itemName\" {$item->getType()} has already been assigned to user $userId.",
);
return $this;
}

$assignment = new Assignment($userId, $itemName, $createdAt ?? time());
Expand Down
20 changes: 5 additions & 15 deletions tests/Common/ManagerLogicTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,23 +509,13 @@ public function testAssignUnknownItem(): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('There is no item named "nonExistRole".');

$manager->assign(
'nonExistRole',
'reader'
);
$manager->assign(itemName: 'nonExistRole', userId: 'reader');
}

public function testAssignAlreadyAssignedItem(): void
{
$manager = $this->createFilledManager();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"reader" role has already been assigned to user reader A.');

$manager->assign(
'reader',
'reader A'
);
$this->assertSame($manager, $manager->assign(itemName: 'reader', userId: 'reader A'));
}

public function testAssignPermissionDirectlyWhenItIsDisabled(): void
Expand All @@ -535,7 +525,7 @@ public function testAssignPermissionDirectlyWhenItIsDisabled(): void

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Assigning permissions directly is disabled. Prefer assigning roles only.');
$manager->assign('readPost', 'id7');
$manager->assign(itemName: 'readPost', userId: 'id7');
}

public function testAssignPermissionDirectlyWhenEnabled(): void
Expand Down Expand Up @@ -1045,8 +1035,8 @@ public function testDataPersistency(): void
->addRole((new Role('role1'))->withCreatedAt(1_694_502_936)->withUpdatedAt(1_694_502_936))
->addRole((new Role('role2'))->withCreatedAt(1_694_502_976)->withUpdatedAt(1_694_502_976))
->addChild('role1', 'role2');
$manager->assign('role1', 1);
$manager->assign('role2', 2);
$manager->assign(itemName: 'role1', userId: 1);
$manager->assign(itemName: 'role2', userId: 2);

$this->assertEquals(
[
Expand Down

0 comments on commit 2637afb

Please sign in to comment.