|
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\DataMapper\NullDomainObject; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Provide methods for check permissions for authenticated user. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Authorization |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var PermissionMapperInterface Permission Mapper |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $permissionMapper; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Authentication Current authentication status |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $authentication; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int User id |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $userId = 0; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array User/Permission hash table |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $hashTable; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Class Constructor. |
|
44
|
|
|
* <pre><code class="php">use Linna\Auth\Authentication; |
|
45
|
|
|
* use Linna\Auth\Authorization; |
|
46
|
|
|
* use Linna\Auth\Password; |
|
47
|
|
|
* use Linna\Session\Session; |
|
48
|
|
|
* |
|
49
|
|
|
* //your concrete permission mapper |
|
50
|
|
|
* use YourApp\Mapper\PermissionMapper; |
|
51
|
|
|
* |
|
52
|
|
|
* $password = new Password(); |
|
53
|
|
|
* $session = new Session(); |
|
54
|
|
|
* |
|
55
|
|
|
* $authentication = new Authentication($session, $password); |
|
56
|
|
|
* $permissionMapper = new PermissionMapper(); |
|
57
|
|
|
* |
|
58
|
|
|
* $authorization = new Authorization($authentication, $permissionMapper); |
|
59
|
|
|
* </code></pre> |
|
60
|
|
|
* |
|
61
|
|
|
* @param Authentication $authentication |
|
62
|
|
|
* @param PermissionMapperInterface $permissionMapper |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct(Authentication $authentication, PermissionMapperInterface $permissionMapper) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->authentication = $authentication; |
|
67
|
|
|
$this->permissionMapper = $permissionMapper; |
|
68
|
|
|
|
|
69
|
|
|
$this->userId = $authentication->getLoginData()['user_id'] ?? 0; |
|
70
|
|
|
|
|
71
|
|
|
$this->hashTable = $permissionMapper->fetchUserPermissionHashTable($this->userId); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Check if authenticated user has a permission. |
|
76
|
|
|
* <pre><code class="php">$authorization = new Authorization($authentication, $permissionMapper); |
|
77
|
|
|
* |
|
78
|
|
|
* //with this example, the class checks if the authenticated |
|
79
|
|
|
* //user has the permission 'update user'. |
|
80
|
|
|
* $authorization->can('update user'); |
|
81
|
|
|
* </code></pre> |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $permissionName |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function can(string $permissionName): bool |
|
88
|
|
|
{ |
|
89
|
|
|
//get permission |
|
90
|
|
|
$permission = $this->permissionMapper->fetchByName($permissionName); |
|
91
|
|
|
|
|
92
|
|
|
//permission not exist |
|
93
|
|
|
if ($permission instanceof NullDomainObject) { |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
//check if there is user logged |
|
98
|
|
|
if (!$this->userId) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
//make hash for hash table check |
|
103
|
|
|
$hash = hash('sha256', $this->userId.'.'.$permission->getId()); |
|
104
|
|
|
|
|
105
|
|
|
return isset($this->hashTable[$hash]); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|