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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
namespace Icinga\Authentication\UserGroup;
use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Inspectable;
use Icinga\Data\Inspection;
use Icinga\Exception\NotFoundError;
use Icinga\Repository\DbRepository;
use Icinga\Repository\RepositoryQuery;
use Icinga\User;
class DbUserGroupBackend extends DbRepository implements Inspectable, UserGroupBackendInterface
{
/**
* The query columns being provided
*
* @var array
*/
protected $queryColumns = array(
'group' => array(
'group_id' => 'g.id',
'group' => 'g.name COLLATE utf8mb4_general_ci',
'group_name' => 'g.name',
'parent' => 'g.parent',
'created_at' => 'UNIX_TIMESTAMP(g.ctime)',
'last_modified' => 'UNIX_TIMESTAMP(g.mtime)'
),
'group_membership' => array(
'group_id' => 'gm.group_id',
'user' => 'gm.username COLLATE utf8mb4_general_ci',
'user_name' => 'gm.username',
'created_at' => 'UNIX_TIMESTAMP(gm.ctime)',
'last_modified' => 'UNIX_TIMESTAMP(gm.mtime)'
)
);
/**
* The table aliases being applied
*
* @var array
*/
protected $tableAliases = array(
'group' => 'g',
'group_membership' => 'gm'
);
/**
* The statement columns being provided
*
* @var array
*/
protected $statementColumns = array(
'group' => array(
'group_id' => 'id',
'group_name' => 'name',
'parent' => 'parent',
'created_at' => 'ctime',
'last_modified' => 'mtime'
),
'group_membership' => array(
'group_id' => 'group_id',
'group_name' => 'group_id',
'user_name' => 'username',
'created_at' => 'ctime',
'last_modified' => 'mtime'
)
);
/**
* The columns which are not permitted to be queried
*
* @var array
*/
protected $blacklistedQueryColumns = array('group', 'user');
/**
* The search columns being provided
*
* @var array
*/
protected $searchColumns = array('group', 'user');
/**
* The value conversion rules to apply on a query or statement
*
* @var array
*/
protected $conversionRules = array(
'group' => array(
'parent' => 'group_id'
),
'group_membership' => array(
'group_name' => 'group_id'
)
);
/**
* Initialize this database user group backend
*/
protected function init()
{
if (! $this->ds->getTablePrefix()) {
$this->ds->setTablePrefix('icingaweb_');
}
}
/**
* Initialize this repository's filter columns
*
* @return array
*/
protected function initializeFilterColumns()
{
$userLabel = t('Username') . ' ' . t('(Case insensitive)');
$groupLabel = t('User Group') . ' ' . t('(Case insensitive)');
return array(
$userLabel => 'user',
t('Username') => 'user_name',
$groupLabel => 'group',
t('User Group') => 'group_name',
t('Parent') => 'parent',
t('Created At') => 'created_at',
t('Last modified') => 'last_modified'
);
}
/**
* Insert a table row with the given data
*
* @param string $table
* @param array $bind
*/
public function insert($table, array $bind, array $types = array())
{
$bind['created_at'] = date('Y-m-d H:i:s');
parent::insert($table, $bind);
}
/**
* Update table rows with the given data, optionally limited by using a filter
*
* @param string $table
* @param array $bind
* @param Filter $filter
*/
public function update($table, array $bind, Filter $filter = null, array $types = array())
{
$bind['last_modified'] = date('Y-m-d H:i:s');
parent::update($table, $bind, $filter);
}
/**
* Delete table rows, optionally limited by using a filter
*
* @param string $table
* @param Filter $filter
*/
public function delete($table, Filter $filter = null)
{
if ($table === 'group') {
parent::delete('group_membership', $filter);
$idQuery = $this->select(array('group_id'));
if ($filter !== null) {
$idQuery->applyFilter($filter);
}
$this->update('group', array('parent' => null), Filter::where('parent', $idQuery->fetchColumn()));
}
parent::delete($table, $filter);
}
/**
* Return the groups the given user is a member of
*
* @param User $user
*
* @return array
*/
public function getMemberships(User $user)
{
$groupQuery = $this->ds
->select()
->from(
array('g' => $this->prependTablePrefix('group')),
array(
'group_name' => 'g.name',
'parent_name' => 'gg.name'
)
)->joinLeft(
array('gg' => $this->prependTablePrefix('group')),
'g.parent = gg.id',
array()
);
$groups = array();
foreach ($groupQuery as $group) {
$groups[$group->group_name] = $group->parent_name;
}
$membershipQuery = $this
->select()
->from('group_membership', array('group_name'))
->where('user_name', $user->getUsername());
$memberships = array();
foreach ($membershipQuery as $membership) {
$memberships[] = $membership->group_name;
$parent = $groups[$membership->group_name];
while ($parent !== null) {
$memberships[] = $parent;
// Usually a parent is an existing group, but since we do not have a constraint on our table..
$parent = isset($groups[$parent]) ? $groups[$parent] : null;
}
}
return $memberships;
}
/**
* Return the name of the backend that is providing the given user
*
* @param string $username Currently unused
*
* @return null|string The name of the backend or null in case this information is not available
*/
public function getUserBackendName($username)
{
return null; // TODO(10373): Store this to the database when inserting and fetch it here
}
/**
* Join group into group_membership
*
* @param RepositoryQuery $query
*/
protected function joinGroup(RepositoryQuery $query)
{
$query->getQuery()->join(
$this->requireTable('group'),
'gm.group_id = g.id',
array()
);
}
/**
* Join group_membership into group
*
* @param RepositoryQuery $query
*/
protected function joinGroupMembership(RepositoryQuery $query)
{
$query->getQuery()->joinLeft(
$this->requireTable('group_membership'),
'g.id = gm.group_id',
array()
)->group('g.id');
}
/**
* Fetch and return the corresponding id for the given group's name
*
* @param string|array $groupName
*
* @return int
*
* @throws NotFoundError
*/
protected function persistGroupId($groupName)
{
if (! $groupName || empty($groupName) || is_numeric($groupName)) {
return $groupName;
}
if (is_array($groupName)) {
if (is_numeric($groupName[0])) {
return $groupName; // In case the array contains mixed types...
}
$groupIds = $this->ds
->select()
->from($this->prependTablePrefix('group'), array('id'))
->where('name', $groupName)
->fetchColumn();
if (empty($groupIds)) {
throw new NotFoundError('No groups found matching one of: %s', implode(', ', $groupName));
}
return $groupIds;
}
$groupId = $this->ds
->select()
->from($this->prependTablePrefix('group'), array('id'))
->where('name', $groupName)
->fetchOne();
if ($groupId === false) {
throw new NotFoundError('Group "%s" does not exist', $groupName);
}
return $groupId;
}
/**
* Inspect this object to gain extended information about its health
*
* @return Inspection The inspection result
*/
public function inspect()
{
$insp = new Inspection('Db User Group Backend');
$insp->write($this->ds->inspect());
try {
$insp->write(sprintf('%s group(s)', $this->select()->count()));
} catch (Exception $e) {
$insp->error(sprintf('Query failed: %s', $e->getMessage()));
}
return $insp;
}
}
|