This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoleMapper.php
115 lines (87 loc) · 3.13 KB
/
RoleMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace ZfcUserAcl\Model;
use ZfcBase\Mapper\DbMapperAbstract,
Zend\Db\Sql\Select,
Zend\Db\Sql\Where;
class RoleMapper extends DbMapperAbstract implements RoleMapperInterface
{
protected $tableName = 'user_role';
public function getRoleById($roleId)
{
$where = new Where;
$where->equalTo('role_id', $roleId);
$sql = new Select;
$sql->from($this->tableName)
->where($where);
$rowset = $this->getTableGateway()->selectWith($sql);
$row = $row->current()->toArray();
return Role::fromArray((array) $row);
}
/**
* @TODO caching
*/
public function getAllStaticRoles($parentId = null)
{
$sql = new Select;
$sql->from($this->tableName);
if (is_null($parentId)) {
$where = new Where;
$where->isNull('parent')->OR->equalTo('parent', '');
$sql->where($where);
$rowset = $this->getTableGateway()->selectWith($sql);
if (count($rowset) < 1) {
throw new \Exception('No top-level ACL roles defined.');
}
$roles = Role::fromArraySet((array) $rowset->toArray());
foreach ($roles as $r) {
$children = $this->getAllStaticRoles($r->getRoleId());
$r->setChildren($children);
}
return $roles;
} else {
$where = new Where;
$where->equalTo('parent', $parentId);
$rowset = $this->getTableGateway()->selectWith($sql->where($where));
if (count($rowset) > 0) {
$roles = Role::fromArraySet((array) $rowset->toArray());
foreach ($roles as $r) {
$children = $this->getAllStaticRoles($r->getRoleId());
$r->setChildren($children);
}
return $roles;
} else {
return array();
}
}
}
public function getDefaultRole()
{
$platform = $this->getTableGateway()->getAdapter()->getPlatform();
$identifier = $platform->quoteIdentifier('default');
$sql = new Select;
$sql->from($this->tableName)
->where("$identifier = 1");
$rowset = $this->getTableGateway()->selectWith($sql);
if (count($rowset) < 1) {
throw new \Exception('Please define a default role.');
} else if (count($rowset) > 1) {
throw new \Exception('Please define only one default role.');
}
$row = $rowset->current();
return Role::fromArray((array) $row);
}
public function getUserRoles($userId)
{
$where = new Where();
$where->equalTo('user_role_linker.user_id', $userId);
$sql = new Select;
$sql->from('user_role')
->join('user_role_linker', 'user_role.role_id = user_role_linker.role_id', array());
$sql->where($where);
$rowset = $this->getTableGateway()->selectWith($sql);
if (count($rowset) < 1) {
return $this->getDefaultRole();
}
return Role::fromArraySet((array) $rowset->toArray());
}
}