UserRoleModel.php
3.96 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
*
* @author Adhidarma <adhisimon@gmail.com>
*/
class UserRoleModel extends Model {
protected $table_name = 'user_roles';
protected $view_name = 'user_roles_combined';
protected $table_fields = array(
'user_id' => array(
'type' => \DB\SQL\Schema::DT_INT,
'nullable' => false,
'index' => false,
'unique' => false,
),
'role_id' => array(
'type' => \DB\SQL\Schema::DT_INT,
'nullable' => false,
'index' => false,
'unique' => false,
),
);
protected $table_indexes = array(
array(
'columns' => array('user_id', 'role_id'),
'unique' => true,
),
array(
'columns' => array('role_id', 'user_id'),
'unique' => true,
),
);
protected $initial_rows = array(
array(
'user_id' => 1,
'role_id' => 1
),
);
public function getMapperFromView() {
return new DB\SQL\Mapper($this->getDb(), $this->getViewName());
}
public function getRoleIdsByUserId($user_id) {
$items = $this->getByUserId($user_id);
$retval = array();
foreach ($items as $item) {
$retval[] = $item['role_id'];
}
return $retval;
}
public function getRoleTitlesByUserId($user_id) {
$items = $this->getByUserId($user_id);
$retval = array();
foreach ($items as $item) {
$retval[] = $item['role_title'];
}
return $retval;
}
/**
* Role-role yang dimiliki seorang user dengan user_id tertentu.
*/
function getByUserId($user_id) {
$mapper = $this->getMapperFromView();
$_roles = $mapper->find(array('user_id=?', $user_id));
$roles = array();
foreach ($_roles as $role) {
$roles[] = $role->cast();
}
return $roles;
}
/**
* Apakah sebuah role id dimiliki oleh sebuah user id.
*/
public function doesRoleIdBelongsToUserId($role_id, $user_id) {
$role_ids = $this->getRoleIdsByUserId($user_id);
return in_array($role_id, $role_ids);
}
/**
* Apakah sebuah user id memiliki sebuah role id.
*
* Alias dari doesRoleIdBelongsToUserId($role_id, $user_id).
*
* @see doesRoleIdBelongsToUserId
*/
public function doesUserIdHasRoleId($user_id, $role_id) {
return $this->doesRoleIdBelongsToUserId($role_id, $user_id);
}
public function delete($user_id, $role_id) {
$item = $this->getMapper();
$item->load(array('user_id=? AND role_id=?', $user_id, $role_id));
$item->erase();
}
final protected function getViewName() {
return $this->view_name;
}
protected function createView() {
$query = "
CREATE VIEW " . $this->getViewName() . " AS
SELECT
" . $this->getTableName() . ".user_id AS user_id,
users.email AS user_email,
" . $this->getTableName() . ".role_id AS role_id,
roles.title AS role_title
FROM " . $this->getTableName() . "
LEFT JOIN users ON
" . $this->getTableName() . ".user_id=users.id
LEFT JOIN roles ON
" . $this->getTableName() . ".role_id=roles.id
";
$db = $this->getDb();
$retval = $db->exec($query);
return true;
}
public function install() {
parent::install();
$test = new Test();
if ($this->isTableExists($this->getViewName())) {
$test->expect(
true,
'Table ' . $this->getViewName() . ' already exists, skipping it'
);
$this->dumpTestResult($test);
return;
}
$test->expect(
$this->createView(),
"Create view " . $this->getViewName()
);
$this->dumpTestResult($test);
}
}