|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LibraryCatalog\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\Permissions\Acl\Resource\GenericResource; |
|
8
|
|
|
use Laminas\Permissions\Acl\Resource\ResourceInterface; |
|
9
|
|
|
use Laminas\Permissions\Acl\Role\GenericRole; |
|
10
|
|
|
use Laminas\Permissions\Acl\Role\RoleInterface; |
|
11
|
|
|
use LibraryCatalog\ValueObject\SessionUser; |
|
12
|
|
|
|
|
13
|
|
|
class Acl extends \Laminas\Permissions\Acl\Acl implements AclInterface |
|
14
|
|
|
{ |
|
15
|
|
|
// Roles. |
|
16
|
|
|
public const GUEST = 'guest'; |
|
17
|
|
|
public const USER = 'user'; |
|
18
|
|
|
public const ADMIN = 'admin'; |
|
19
|
|
|
|
|
20
|
|
|
// Resources. |
|
21
|
|
|
public const BOOK = 'book'; |
|
22
|
|
|
public const AUTHOR = 'author'; |
|
23
|
|
|
|
|
24
|
|
|
// Privileges. |
|
25
|
|
|
public const READ = 'read'; |
|
26
|
|
|
public const ADD = 'add'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Acl constructor. |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
1 |
|
$guest = new GenericRole(static::GUEST); |
|
34
|
1 |
|
$user = new GenericRole(static::USER); |
|
35
|
1 |
|
$admin = new GenericRole(static::ADMIN); |
|
36
|
|
|
|
|
37
|
1 |
|
$book = new GenericResource(static::BOOK); |
|
38
|
1 |
|
$author = new GenericResource(static::AUTHOR); |
|
39
|
|
|
|
|
40
|
1 |
|
$this->addRole($guest) |
|
41
|
1 |
|
->addRole($user, $guest) |
|
42
|
1 |
|
->addRole($admin, $user) |
|
43
|
1 |
|
->addResource($book) |
|
44
|
1 |
|
->addResource($author); |
|
45
|
|
|
|
|
46
|
1 |
|
$this->allow($guest, [$book, $author], static::READ) |
|
47
|
1 |
|
->allow($user, $book, static::ADD) |
|
48
|
1 |
|
->allow($admin, $author, static::ADD); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Generate SessionUser from payload; |
|
53
|
|
|
* Payload example: "user:182". |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $payload |
|
56
|
|
|
* @return SessionUser |
|
57
|
|
|
*/ |
|
58
|
13 |
|
public function parseUserPayload(string $payload): SessionUser |
|
59
|
|
|
{ |
|
60
|
13 |
|
return SessionUser::createFromPayload($payload); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* SessionUser can be used instead of Role. |
|
65
|
|
|
* |
|
66
|
|
|
* @param RoleInterface|SessionUser|string $role |
|
67
|
|
|
* @param ResourceInterface|string $resource |
|
68
|
|
|
* @param string $privilege |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
13 |
|
public function isAllowed($role = null, $resource = null, $privilege = null) |
|
72
|
|
|
{ |
|
73
|
|
|
// Autotransform SessionUser to RoleId. |
|
74
|
13 |
|
if ($role instanceof SessionUser) { |
|
75
|
13 |
|
$role = $role->roleId; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
13 |
|
$res = parent::isAllowed($role, $resource, $privilege); |
|
|
|
|
|
|
80
|
4 |
|
} catch (\InvalidArgumentException $e) { |
|
81
|
4 |
|
$res = false; |
|
82
|
|
|
} |
|
83
|
13 |
|
return $res; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|