|
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\Roles; |
|
12
|
|
|
|
|
13
|
|
|
use Bluz\Proxy\Cache; |
|
14
|
|
|
use Bluz\Proxy\Db; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Table |
|
18
|
|
|
* |
|
19
|
|
|
* @package Application\Roles |
|
20
|
|
|
* |
|
21
|
|
|
* @method static Row findRow($primaryKey) |
|
22
|
|
|
* @method static Row findRowWhere($whereList) |
|
23
|
|
|
*/ |
|
24
|
|
|
class Table extends \Bluz\Db\Table |
|
25
|
|
|
{ |
|
26
|
|
|
const BASIC_ADMIN = 'admin'; |
|
27
|
|
|
const BASIC_GUEST = 'guest'; |
|
28
|
|
|
const BASIC_MEMBER = 'member'; |
|
29
|
|
|
const BASIC_SYSTEM = 'system'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Table |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $name = 'acl_roles'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Primary key(s) |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $primary = array('id'); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $basicRoles = [self::BASIC_ADMIN, self::BASIC_GUEST, self::BASIC_MEMBER, self::BASIC_SYSTEM]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Init table relations |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function init() |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->linkTo('id', 'UsersRoles', 'roleId'); |
|
58
|
1 |
|
$this->linkToMany('Users', 'UsersRoles'); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get all roles in system |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getRoles() |
|
67
|
|
|
{ |
|
68
|
2 |
|
return self::fetch("SELECT * FROM acl_roles ORDER BY id"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get all basic roles |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getBasicRoles() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->basicRoles; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get all user roles in system |
|
83
|
|
|
* |
|
84
|
|
|
* @param integer $userId |
|
85
|
|
|
* |
|
86
|
|
|
* @return array of rows |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getUserRoles($userId) |
|
89
|
|
|
{ |
|
90
|
|
|
$data = self::fetch( |
|
91
|
|
|
"SELECT r.* |
|
92
|
|
|
FROM acl_roles AS r, acl_users_roles AS u2r |
|
93
|
|
|
WHERE r.id = u2r.roleId AND u2r.userId = ?", |
|
94
|
|
|
array($userId) |
|
95
|
|
|
); |
|
96
|
|
|
return $data; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get all user roles in system |
|
101
|
|
|
* |
|
102
|
|
|
* @param integer $userId |
|
103
|
|
|
* |
|
104
|
|
|
* @return array of identity |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function getUserRolesIdentity($userId) |
|
107
|
|
|
{ |
|
108
|
4 |
|
$cacheKey = 'users.roles.' . $userId; |
|
109
|
4 |
|
if (!$data = Cache::get($cacheKey)) { |
|
110
|
4 |
|
$data = Db::fetchColumn( |
|
111
|
4 |
|
"SELECT r.id |
|
112
|
|
|
FROM acl_roles AS r, acl_users_roles AS u2r |
|
113
|
|
|
WHERE r.id = u2r.roleId AND u2r.userId = ? |
|
114
|
|
|
ORDER BY r.id ASC", |
|
115
|
4 |
|
array($userId) |
|
116
|
|
|
); |
|
117
|
4 |
|
Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY, ['system', 'users', 'roles']); |
|
118
|
|
|
} |
|
119
|
4 |
|
return $data; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|