|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @namespace |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Application\Privileges; |
|
12
|
|
|
|
|
13
|
|
|
use Application\Roles; |
|
14
|
|
|
use Bluz\Proxy\Cache; |
|
15
|
|
|
use Bluz\Proxy\Db; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Table |
|
19
|
|
|
* |
|
20
|
|
|
* @package Application\Privileges |
|
21
|
|
|
* |
|
22
|
|
|
* @method static Row findRow($primaryKey) |
|
23
|
|
|
* @method static Row findRowWhere($whereList) |
|
24
|
|
|
*/ |
|
25
|
|
|
class Table extends \Bluz\Db\Table |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Table |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $name = 'acl_privileges'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Primary key(s) |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $primary = array('roleId', 'module', 'privilege'); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get all privileges |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function getPrivileges() |
|
47
|
|
|
{ |
|
48
|
1 |
|
return self::fetch( |
|
49
|
1 |
|
"SELECT DISTINCT p.roleId, p.module, p.privilege |
|
50
|
|
|
FROM acl_privileges AS p |
|
51
|
|
|
ORDER BY module, privilege" |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get user privileges |
|
57
|
|
|
* |
|
58
|
|
|
* @param integer $userId |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getUserPrivileges($userId) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$roles = Roles\Table::getInstance()->getUserRolesIdentity($userId); |
|
65
|
|
|
|
|
66
|
3 |
|
$stack = []; |
|
67
|
3 |
|
foreach ($roles as $roleId) { |
|
68
|
|
|
$stack = array_merge($stack, $this->getRolePrivileges($roleId)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// magic array_unique for multi array |
|
72
|
3 |
|
return array_unique($stack); |
|
73
|
|
|
|
|
74
|
|
|
// follow code is faster, but required record for every user in memcache |
|
75
|
|
|
// in other words, need more memory for decrease CPU load |
|
76
|
|
|
// for update |
|
77
|
|
|
/* |
|
|
|
|
|
|
78
|
|
|
$cacheKey = '|privileges|'.$userId; |
|
79
|
|
|
if (!$data = Cache::get($cacheKey)) { |
|
80
|
|
|
$data = Db::getDefaultAdapter()->fetchColumn( |
|
81
|
|
|
"SELECT DISTINCT r.id, CONCAT(p.module, ':', p.privilege) |
|
82
|
|
|
FROM acl_privileges AS p, acl_roles AS r, acl_users_roles AS u2r |
|
83
|
|
|
WHERE p.roleId = r.id AND r.id = u2r.roleId AND u2r.userId = ? |
|
84
|
|
|
ORDER BY module, privilege", |
|
85
|
|
|
array((int) $userId) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY); |
|
89
|
|
|
} |
|
90
|
|
|
return $data; |
|
91
|
|
|
*/ |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get user privileges |
|
96
|
|
|
* |
|
97
|
|
|
* @param integer $roleId |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getRolePrivileges($roleId) |
|
102
|
|
|
{ |
|
103
|
|
|
$cacheKey = 'roles.privileges.' . $roleId; |
|
104
|
|
|
|
|
105
|
|
|
if (!$data = Cache::get($cacheKey)) { |
|
106
|
|
|
$data = Db::fetchColumn( |
|
107
|
|
|
"SELECT DISTINCT CONCAT(p.module, ':', p.privilege) |
|
108
|
|
|
FROM acl_privileges AS p, acl_roles AS r |
|
109
|
|
|
WHERE p.roleId = r.id AND r.id = ? |
|
110
|
|
|
ORDER BY CONCAT(p.module, ':', p.privilege)", |
|
111
|
|
|
array((int)$roleId) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY, ['system', 'roles', 'privileges']); |
|
115
|
|
|
} |
|
116
|
|
|
return $data; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.