|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Authorization; |
|
13
|
|
|
|
|
14
|
|
|
use Linna\Authentication\Authentication; |
|
15
|
|
|
use Linna\Authorization\Permission; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Provide methods for check permissions for authenticated user. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Authorization |
|
21
|
|
|
{ |
|
22
|
|
|
use PermissionTrait { |
|
23
|
|
|
PermissionTrait::can as canByObject; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int User id |
|
28
|
|
|
*/ |
|
29
|
|
|
protected int $userId = 0; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class Constructor. |
|
33
|
|
|
* <pre><code class="php">use Linna\Auth\Authentication; |
|
34
|
|
|
* use Linna\Auth\Authorization; |
|
35
|
|
|
* use Linna\Auth\Password; |
|
36
|
|
|
* use Linna\Session\Session; |
|
37
|
|
|
* |
|
38
|
|
|
* //your concrete permission mapper |
|
39
|
|
|
* use YourApp\Mapper\PermissionMapper; |
|
40
|
|
|
* |
|
41
|
|
|
* $password = new Password(); |
|
42
|
|
|
* $session = new Session(); |
|
43
|
|
|
* |
|
44
|
|
|
* $authentication = new Authentication($session, $password); |
|
45
|
|
|
* $permissionMapper = new PermissionMapper(); |
|
46
|
|
|
* |
|
47
|
|
|
* $authorization = new Authorization($authentication, $permissionMapper); |
|
48
|
|
|
* </code></pre> |
|
49
|
|
|
* |
|
50
|
|
|
* @param Authentication $authentication |
|
51
|
|
|
* @param PermissionMapperInterface $permissionMapper |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function __construct(Authentication $authentication, PermissionMapperInterface $permissionMapper) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->userId = $authentication->getLoginData()['user_id'] ?? 0; |
|
56
|
1 |
|
$this->permission = $permissionMapper->fetchByUserId($this->userId); |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check if authenticated user has a permission. |
|
61
|
|
|
* <pre><code class="php">$authorization = new Authorization($authentication, $permissionMapper); |
|
62
|
|
|
* |
|
63
|
|
|
* //with this example, the class checks if the authenticated |
|
64
|
|
|
* //user has the permission with the permission object. |
|
65
|
|
|
* |
|
66
|
|
|
* $permission = $permissionMapper->fetchById(1); |
|
67
|
|
|
* $authorization->can($permission); |
|
68
|
|
|
* |
|
69
|
|
|
* //with this example, the class checks if the authenticated |
|
70
|
|
|
* //user has the permission with the permission id 1. |
|
71
|
|
|
* $authorization->can(1); |
|
72
|
|
|
* |
|
73
|
|
|
* //with this example, the class checks if the authenticated |
|
74
|
|
|
* //user has the permission 'see users'. |
|
75
|
|
|
* $authorization->can('see users'); |
|
76
|
|
|
* </code></pre> |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $permission |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function can($permission): bool |
|
83
|
|
|
{ |
|
84
|
3 |
|
if ($permission instanceof Permission) { |
|
85
|
2 |
|
return $this->canByObject($permission); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
if (\is_int($permission)) { |
|
89
|
2 |
|
return $this->canById($permission); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
if (\is_string($permission)) { |
|
93
|
2 |
|
return $this->canByName($permission); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|