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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
<?php
/**
* plugin.php - role administration controller
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Dennis Reil
* @author Michael Riehemann <michael.riehemann@uni-oldenburg.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package admin
*/
class Admin_RoleController extends AuthenticatedController
{
/**
* Returns the role with the given id.
* @param string $role_id Id of the role
* @return Role
*/
public static function getRole($role_id)
{
static $roles = null;
if ($roles === null) {
$roles = RolePersistence::getAllRoles();
}
return $roles[$role_id];
}
/**
* Common tasks for all actions.
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
// user must have root permission
$GLOBALS['perm']->check('root');
// set page title and navigation
PageLayout::setTitle(_('Verwaltung von Rollen'));
Navigation::activateItem('/admin/config/roles');
$this->setSidebar($action);
}
/**
* Display a list of all existing roles and some statistics.
*/
public function index_action()
{
$this->roles = RolePersistence::getAllRoles();
$this->stats = RolePersistence::getStatistics();
}
/**
* Create a new role.
*/
public function add_action()
{
PageLayout::setTitle(_('Neue Rolle anlegen'));
if (Request::isPost()) {
$this->check_ticket();
$name = Request::get('name');
$name = trim($name);
if ($name !== '') {
$role = new Role();
$role->setRolename($name);
RolePersistence::saveRole($role);
$message = sprintf(_('Die Rolle "%s" wurde angelegt.'), htmlReady($name));
} else {
$message = _('Sie haben keinen Namen eingegeben.');
}
PageLayout::postSuccess($message);
$this->redirect('admin/role');
}
}
/**
* Ask for confirmation from the user before deleting a role.
*
* @param integer id of role to delete
*/
public function ask_remove_role_action($role_id)
{
PageLayout::postQuestion(
sprintf(
_('Wollen Sie wirklich die Rolle "%s" löschen?'),
htmlReady(self::getRole($role_id)->getRolename())
),
$this->url_for("admin/role/remove_role/{$role_id}")
)->includeTicket();
$this->redirect('admin/role');
}
/**
* Completely delete a role (including all its assignments).
*
* @param integer id of role to delete
*/
public function remove_role_action($roleid)
{
$this->check_ticket();
$role = self::getRole($roleid);
RolePersistence::deleteRole($role);
$message = _('Die Rolle und alle dazugehörigen Zuweisungen wurden gelöscht.');
PageLayout::postSuccess($message);
$this->redirect('admin/role');
}
/**
* Search for users containing the given string in either
* first name, last name oder user name.
*
* @param string text to match agaist
*
* @return array list of User objects
*/
private function search_user($searchtxt)
{
$searchtxt = "%{$searchtxt}%";
$condition = "username LIKE ?
OR CONCAT(Nachname, ', ', Vorname, ' ', Nachname) LIKE ?
ORDER BY Vorname, Nachname, username";
return User::findBySQL($condition, [$searchtxt, $searchtxt]);
}
/**
* Display all roles assigned to a particular user.
*
* @param string user id (optional)
*/
public function assign_role_action($userid = NULL)
{
$usersel = Request::option('usersel', $userid);
if (isset($usersel) && Request::isPost()) {
$this->redirect("admin/role/assign_role/{$usersel}");
return;
}
$this->username = Request::get('username');
$this->currentuser = null;
$this->assignedroles = [];
$this->all_userroles = [];
// user search was started
if (Request::submitted('search')) {
if ($this->username == '') {
$this->error = _('Es wurde kein Suchwort eingegeben.');
} else {
$this->users = $this->search_user($this->username);
if (count($this->users) === 0) {
$this->error = _('Es wurde keine Person gefunden.');
}
}
}
// a user was selected
if (isset($usersel)) {
$this->users[$usersel] = User::find($usersel);
$this->currentuser = $this->users[$usersel];
$this->assignedroles = $this->currentuser->getRoles();
$this->all_userroles = $this->currentuser->getRoles(true);
$this->roles = RolePersistence::getAllRoles(true);
foreach ($this->assignedroles as $role) {
$institutes = SimpleCollection::createFromArray(Institute::findMany(RolePersistence::getAssignedRoleInstitutes($usersel, $role->getRoleid())));
$this->assignedroles_institutes[$role->getRoleid()] = $institutes->orderBy('name')->pluck('name');
}
}
}
/**
* Change the roles assigned to a particular user.
*
* @param string user id
*/
public function save_role_action($userid)
{
$selecteduser = User::find($userid);
$this->check_ticket();
if (Request::submitted('assign_role')) {
// assign roles
foreach (Request::intArray('rolesel') as $selroleid) {
$role = self::getRole($selroleid);
RolePersistence::assignRole($selecteduser, $role);
}
} else if (Request::submitted('remove_role')) {
// delete role assignment
foreach (Request::intArray('assignedroles') as $roleid) {
$role = self::getRole($roleid);
RolePersistence::deleteRoleAssignment($selecteduser, $role);
}
}
$message = _('Die Rollenzuweisungen wurden gespeichert.');
PageLayout::postSuccess($message);
$this->redirect('admin/role/assign_role/'.$userid);
}
/**
* Display all roles assigned to a particular plugin.
*
* @param integer plugin id (optional)
*/
public function assign_plugin_role_action($pluginid = NULL)
{
$pluginid = Request::int('pluginid', $pluginid);
$this->plugins = PluginManager::getInstance()->getPluginInfos();
$this->assigned = RolePersistence::getAssignedPluginRoles($pluginid);
$this->roles = RolePersistence::getAllRoles(true);
$this->pluginid = $pluginid;
}
/**
* Change the roles assigned to a particular plugin.
*
* @param integer plugin id
*/
public function save_plugin_role_action($pluginid)
{
$this->check_ticket();
if (Request::submitted('assign_role')) {
// assign roles
$selroles = Request::intArray('rolesel');
RolePersistence::assignPluginRoles($pluginid, $selroles);
} else if (Request::submitted('remove_role')) {
// delete role assignment
$delassignedrols = Request::intArray('assignedroles');
RolePersistence::deleteAssignedPluginRoles($pluginid, $delassignedrols);
}
$message = _('Die Rechteeinstellungen wurden gespeichert.');
PageLayout::postSuccess($message);
$this->redirect("admin/role/assign_plugin_role/{$pluginid}");
}
/**
* Check role access permission for the given plugin.
*
* @param array plugin meta data
* @param integer role id of role
*/
private function check_role_access($plugin, $role_id)
{
$plugin_roles = RolePersistence::getAssignedPluginRoles($plugin['id']);
foreach ($plugin_roles as $plugin_role) {
if ($plugin_role->getRoleid() == $role_id) {
return true;
}
}
return false;
}
/**
* Display a list of user and role assignments for a given role.
*
* @param integer role id (optional)
*/
public function show_role_action($roleid = null)
{
$roleid = Request::int('role', $roleid);
$this->roles = RolePersistence::getAllRoles();
$this->roleid = '';
if ($roleid) {
$this->users = RolePersistence::getUsersWithRoleById($roleid);
$this->user_institutes = [];
foreach ($this->users as $user) {
$this->user_institutes[$user->id] = Institute::findAndMapMany(
function (Institute $institute) {
return $institute->name;
},
RolePersistence::getAssignedRoleInstitutes($user['user_id'], $roleid),
'ORDER BY name'
);
}
$plugins = PluginManager::getInstance()->getPluginInfos();
foreach ($plugins as $id => $plugin) {
if (!$this->check_role_access($plugin, $roleid)) {
unset($plugins[$id]);
}
}
$this->implicit_count = RolePersistence::countImplicitUsers($roleid);
$this->plugins = $plugins;
$this->role = self::getRole($roleid);
$this->roleid = $roleid;
$this->mps = $this->getMultiPersonSearch($roleid);
}
}
/**
*
*/
public function add_user_action($role_id, $user_id)
{
$role = self::getRole($role_id);
$ids = $this->getUsers($role_id, $user_id);
$users = User::findMany($ids);
foreach ($users as $user) {
RolePersistence::assignRole($user, $role);
}
$template = ngettext(
'Der Rolle wurde eine weitere Person hinzugefügt.',
'Der Rolle wurden %u weitere Personen hinzugefügt.',
count($ids)
);
$message = sprintf($template, count($ids));
PageLayout::postSuccess($message);
$this->redirect("admin/role/show_role/{$role_id}");
}
/**
*
*/
public function remove_user_action($role_id, $user_id)
{
CSRFProtection::verifyUnsafeRequest();
$role = self::getRole($role_id);
$ids = $this->getUsers($role_id, $user_id);
$users = User::findMany($ids);
foreach ($users as $user) {
RolePersistence::deleteRoleAssignment($user, $role);
}
$template = ngettext(
'Einer Person wurde die Rolle entzogen.',
'%u Personen wurde die Rolle entzogen.',
count($ids)
);
$message = sprintf($template, count($ids));
PageLayout::postSuccess($message);
$this->redirect("admin/role/show_role/{$role_id}");
}
/**
*
*/
public function add_plugin_action($role_id)
{
PageLayout::setTitle(_('Plugins zur Rolle hinzufügen'));
if (Request::isPost()) {
CSRFProtection::verifyUnsafeRequest();
$plugin_ids = Request::intArray('plugin_ids');
if (count($plugin_ids) > 0) {
foreach ($plugin_ids as $id) {
RolePersistence::assignPluginRoles($id, [$role_id]);
}
$template = ngettext(
'Der Rolle wurde ein weiteres Plugin hinzugefügt.',
'Der Rolle wurden %u weitere Plugins hinzugefügt.',
count($plugin_ids)
);
$message = sprintf($template, count($plugin_ids));
PageLayout::postSuccess($message);
}
$this->redirect("admin/role/show_role/{$role_id}");
}
$this->role_id = $role_id;
$plugins = PluginManager::getInstance()->getPluginInfos();
$controller = $this;
$this->plugins = array_filter($plugins, function ($plugin) use ($controller, $role_id) {
return !$controller->check_role_access($plugin, $role_id);
});
}
/**
*
*/
public function remove_plugin_action($role_id, $plugin_id)
{
CSRFProtection::verifyUnsafeRequest();
$role = self::getRole($role_id);
$ids = $this->getPlugins($role_id, $plugin_id);
foreach ($ids as $id) {
RolePersistence::deleteAssignedPluginRoles($id, [$role_id]);
}
$template = ngettext(
'Einem Plugin wurde die Rolle entzogen.',
'%u Plugins wurde die Rolle entzogen.',
count($ids)
);
$message = sprintf($template, count($ids));
PageLayout::postSuccess($message);
$this->redirect("admin/role/show_role/{$role_id}");
}
/**
*
*/
private function getUsers($role_id, $user_id)
{
// From form
if (Request::getInstance()->offsetExists('ids')) {
return Request::optionArray('ids');
}
// From multi person search
if ($user_id === 'bulk') {
return $this->getMultiPersonSearch($role_id)->getAddedUsers();
}
// From url
return [$user_id];
}
/**
*
*/
private function getPlugins($role_id, $plugin_id)
{
// From form
if (Request::getInstance()->offsetExists('ids')) {
return Request::optionArray('ids');
}
// From url
return [$plugin_id];
}
/**
*
*/
protected function getMultiPersonSearch($role_id)
{
// Multiperson search
$query = "SELECT aum.user_id,
{$GLOBALS['_fullname_sql']['full_rev']} AS fullname,
username, perms
FROM auth_user_md5 AS aum
LEFT JOIN user_info AS ui ON (aum.user_id = ui.user_id)
LEFT JOIN roles_user AS ru ON (aum.user_id = ru.userid AND ru.roleid = {$role_id})
WHERE ru.userid IS NULL
AND (
username LIKE :input
OR CONCAT(Vorname, ' ', Nachname) LIKE :input
OR CONCAT(Nachname, ' ', Vorname) LIKE :input
OR {$GLOBALS['_fullname_sql']['full_rev']} LIKE :input
)
ORDER BY fullname ASC";
$url = URLHelper::getURL('dispatch.php/admin/role/add_user/' . $role_id . '/bulk');
return MultiPersonSearch::get('add_role_users')
->setLinkText(_('Personen hinzufügen'))
->setTitle(_('Personen zur Rolle hinzufügen'))
->setExecuteURL($url)
->setSearchObject(new SQLSearch($query, _('Person suchen'), 'user_id'));
}
public function assign_role_institutes_action($role_id, $user_id)
{
if (Request::submitted('add_institute') && $institut_id = Request::option('institute_id')) {
$roles = RolePersistence::getAllRoles();
$role = $roles[$role_id];
$user = User::find($user_id);
RolePersistence::assignRole($user, $role, Request::option('institute_id'));
PageLayout::postSuccess(_('Die Einrichtung wurde zugewiesen.'));
}
if ($remove_institut_id = Request::option('remove_institute')) {
$roles = RolePersistence::getAllRoles();
$role = $roles[$role_id];
$user = User::find($user_id);
RolePersistence::deleteRoleAssignment($user, $role, $remove_institut_id);
PageLayout::postSuccess(_('Die Einrichtung wurde entfernt.'));
}
$roles = RolePersistence::getAllRoles();
$this->role = $roles[$role_id];
$this->user = new User($user_id);
$this->institutes = SimpleCollection::createFromArray(Institute::findMany(RolePersistence::getAssignedRoleInstitutes($user_id, $role_id)));
$this->institutes->orderBy('name');
$this->qsearch = QuickSearch::get('institute_id', new StandardSearch('Institut_id'));
if (Request::isXhr()) {
$this->qsearch->withoutButton();
}
}
private function setSidebar($action)
{
$sidebar = Sidebar::Get();
$views = $sidebar->addWidget(new ViewsWidget());
$views->addLink(
_('Rollen verwalten'),
$this->url_for('admin/role')
)->setActive($action === 'index');
$views->addLink(
_('Personenzuweisungen bearbeiten'),
$this->url_for('admin/role/assign_role')
)->setActive($action === 'assign_role');
$views->addLink(
_('Pluginzuweisungen bearbeiten'),
$this->url_for('admin/role/assign_plugin_role')
)->setActive($action === 'assign_plugin_role');
$views->addLink(
_('Rollenzuweisungen anzeigen'),
$this->url_for('admin/role/show_role')
)->setActive($action === 'show_role');
$actions = $sidebar->addWidget(new ActionsWidget());
$actions->addLink(
_('Neue Rolle anlegen'),
$this->url_for('admin/role/add'),
Icon::create('add')
)->asDialog('size=auto');
}
/**
* Validate ticket (passed via request environment).
* This method always checks Request::quoted('ticket').
*
* @throws InvalidArgumentException if ticket is not valid
*/
private function check_ticket()
{
if (!check_ticket(Request::option('studip_ticket'))) {
throw new InvalidArgumentException(_('Das Ticket für diese Aktion ist ungültig.'));
}
}
}
|