|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApplicationTest\Acl; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Acl\Acl; |
|
8
|
|
|
use Application\Enum\CollectionVisibility; |
|
9
|
|
|
use Application\Enum\Site; |
|
10
|
|
|
use Application\Model\Card; |
|
11
|
|
|
use Application\Model\Change; |
|
12
|
|
|
use Application\Model\Collection; |
|
13
|
|
|
use Application\Model\User; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
class AclTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testIsCurrentUserAllowed(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$acl = new Acl(); |
|
21
|
|
|
$card = new Card(); |
|
22
|
|
|
$card->setSite(Site::Dilps); |
|
23
|
|
|
|
|
24
|
|
|
$ownerStudent = new User(); |
|
25
|
|
|
$ownerStudent->setSite(Site::Dilps); |
|
26
|
|
|
$ownerStudent->setLogin('Sarah'); |
|
27
|
|
|
User::setCurrent($ownerStudent); |
|
28
|
|
|
$card->timestampCreation(); |
|
29
|
|
|
|
|
30
|
|
|
User::setCurrent(null); |
|
31
|
|
|
self::assertFalse($acl->isCurrentUserAllowed($card, 'update'), 'anonymous cannot update'); |
|
32
|
|
|
self::assertSame('Non-logged user with role anonymous is not allowed on resource "Card#null" with privilege "update"', $acl->getLastDenialMessage()); |
|
33
|
|
|
|
|
34
|
|
|
User::setCurrent($ownerStudent); |
|
35
|
|
|
self::assertFalse($acl->isCurrentUserAllowed($card, 'update'), 'student cannot update even if owner'); |
|
36
|
|
|
self::assertSame('User "Sarah" with role student is not allowed on resource "Card#null" with privilege "update"', $acl->getLastDenialMessage()); |
|
37
|
|
|
|
|
38
|
|
|
$ownerJunior = new User(User::ROLE_JUNIOR); |
|
39
|
|
|
$ownerJunior->setSite(Site::Dilps); |
|
40
|
|
|
$ownerJunior->setLogin('Kyle'); |
|
41
|
|
|
User::setCurrent($ownerJunior); |
|
42
|
|
|
$card->timestampCreation(); |
|
43
|
|
|
|
|
44
|
|
|
self::assertTrue($acl->isCurrentUserAllowed($card, 'update'), 'only junior owner can update'); |
|
45
|
|
|
self::assertNull($acl->getLastDenialMessage()); |
|
46
|
|
|
self::assertTrue($acl->isCurrentUserAllowed($card, 'delete'), 'junior can delete his card'); |
|
47
|
|
|
self::assertNull($acl->getLastDenialMessage()); |
|
48
|
|
|
|
|
49
|
|
|
$change = new Change(); |
|
50
|
|
|
$change->setSuggestion($card); |
|
51
|
|
|
self::assertFalse($acl->isCurrentUserAllowed($card, 'delete'), 'junior cannot delete his card if it is a suggestion'); |
|
52
|
|
|
self::assertSame('User "Kyle" with role junior is not allowed on resource "Card#null" with privilege "delete"', $acl->getLastDenialMessage()); |
|
53
|
|
|
|
|
54
|
|
|
$otherStudent = new User(); |
|
55
|
|
|
$otherStudent->setSite(Site::Dilps); |
|
56
|
|
|
$otherStudent->setLogin('John'); |
|
57
|
|
|
User::setCurrent($otherStudent); |
|
58
|
|
|
self::assertFalse($acl->isCurrentUserAllowed($card, 'update'), 'other user cannot update'); |
|
59
|
|
|
self::assertSame('User "John" with role student is not allowed on resource "Card#null" with privilege "update" because it is not the owner, nor one of the responsible', $acl->getLastDenialMessage()); |
|
60
|
|
|
|
|
61
|
|
|
$administrator = new User(User::ROLE_ADMINISTRATOR); |
|
62
|
|
|
$administrator->setSite(Site::Dilps); |
|
63
|
|
|
$administrator->setLogin('Jane'); |
|
64
|
|
|
User::setCurrent($administrator); |
|
65
|
|
|
self::assertTrue($acl->isCurrentUserAllowed($card, 'update'), 'admin can do anything'); |
|
66
|
|
|
self::assertNull($acl->getLastDenialMessage()); |
|
67
|
|
|
|
|
68
|
|
|
$collection = new Collection(); |
|
69
|
|
|
$collection->setSite(Site::Dilps); |
|
70
|
|
|
self::assertFalse($acl->isCurrentUserAllowed($collection, 'read'), 'admin cannot read non-admin collection'); |
|
71
|
|
|
self::assertSame('User "Jane" with role administrator is not allowed on resource "Collection#null" with privilege "read" because it is not the owner, nor one of the responsible', $acl->getLastDenialMessage()); |
|
72
|
|
|
|
|
73
|
|
|
$collection->setVisibility(CollectionVisibility::Administrator); |
|
74
|
|
|
self::assertTrue($acl->isCurrentUserAllowed($collection, 'read'), 'admin can do anything'); |
|
75
|
|
|
self::assertNull($acl->getLastDenialMessage()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|