2011-06-02 22:40:08 +02:00
|
|
|
<?php
|
2016-09-29 10:53:17 +02:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
use Engelsystem\Database\DB;
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function admin_groups_title()
|
|
|
|
{
|
2018-08-29 21:55:32 +02:00
|
|
|
return __('Grouprights');
|
2013-11-25 21:04:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function admin_groups()
|
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
$html = '';
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-21 13:58:53 +01:00
|
|
|
$groups = DB::select('SELECT * FROM `Groups` ORDER BY `Name`');
|
2017-07-18 21:38:53 +02:00
|
|
|
|
|
|
|
if (!$request->has('action')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$groups_table = [];
|
|
|
|
foreach ($groups as $group) {
|
2017-01-21 13:58:53 +01:00
|
|
|
$privileges = DB::select('
|
|
|
|
SELECT `name`
|
2017-01-02 15:43:36 +01:00
|
|
|
FROM `GroupPrivileges`
|
|
|
|
JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `group_id`=?
|
2018-12-25 10:25:32 +01:00
|
|
|
ORDER BY `name`
|
2017-01-21 13:58:53 +01:00
|
|
|
', [$group['UID']]);
|
2017-01-02 03:57:23 +01:00
|
|
|
$privileges_html = [];
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
foreach ($privileges as $privilege) {
|
|
|
|
$privileges_html[] = $privilege['name'];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$groups_table[] = [
|
2017-01-02 15:43:36 +01:00
|
|
|
'name' => $group['Name'],
|
|
|
|
'privileges' => join(', ', $privileges_html),
|
|
|
|
'actions' => button(
|
2017-08-28 16:21:10 +02:00
|
|
|
page_link_to('admin_groups',
|
|
|
|
['action' => 'edit', 'id' => $group['UID']]),
|
2018-08-29 21:55:32 +02:00
|
|
|
__('edit'),
|
2017-01-02 15:43:36 +01:00
|
|
|
'btn-xs'
|
|
|
|
)
|
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return page_with_title(admin_groups_title(), [
|
2017-01-02 15:43:36 +01:00
|
|
|
table([
|
2018-08-29 21:55:32 +02:00
|
|
|
'name' => __('Name'),
|
|
|
|
'privileges' => __('Privileges'),
|
2017-01-02 15:43:36 +01:00
|
|
|
'actions' => ''
|
|
|
|
], $groups_table)
|
|
|
|
]);
|
2017-01-02 03:57:23 +01:00
|
|
|
} else {
|
2017-07-18 21:38:53 +02:00
|
|
|
switch ($request->input('action')) {
|
2017-01-02 15:43:36 +01:00
|
|
|
case 'edit':
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('id') && preg_match('/^-\d{1,11}$/', $request->input('id'))) {
|
|
|
|
$group_id = $request->input('id');
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
2017-01-03 14:12:17 +01:00
|
|
|
return error('Incomplete call, missing Groups ID.', true);
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
$group = DB::select('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]);
|
|
|
|
if (!empty($group)) {
|
|
|
|
$privileges = DB::select('
|
2017-01-02 15:43:36 +01:00
|
|
|
SELECT `Privileges`.*, `GroupPrivileges`.`group_id`
|
|
|
|
FROM `Privileges`
|
|
|
|
LEFT OUTER JOIN `GroupPrivileges`
|
|
|
|
ON (
|
|
|
|
`Privileges`.`id` = `GroupPrivileges`.`privilege_id`
|
2017-01-21 13:58:53 +01:00
|
|
|
AND `GroupPrivileges`.`group_id`=?
|
2017-01-02 15:43:36 +01:00
|
|
|
)
|
|
|
|
ORDER BY `Privileges`.`name`
|
2017-01-21 13:58:53 +01:00
|
|
|
', [$group_id]);
|
2017-01-03 14:12:17 +01:00
|
|
|
$privileges_html = '';
|
2017-01-02 15:43:36 +01:00
|
|
|
$privileges_form = [];
|
2017-01-21 13:58:53 +01:00
|
|
|
foreach ($privileges as $privilege) {
|
2017-01-02 15:43:36 +01:00
|
|
|
$privileges_form[] = form_checkbox(
|
|
|
|
'privileges[]',
|
2017-01-21 13:58:53 +01:00
|
|
|
$privilege['desc'] . ' (' . $privilege['name'] . ')',
|
|
|
|
$privilege['group_id'] != '',
|
2017-08-29 16:21:25 +02:00
|
|
|
$privilege['id'],
|
|
|
|
'privilege-' . $privilege['name']
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
|
|
|
$privileges_html .= sprintf(
|
2017-12-25 23:12:52 +01:00
|
|
|
'<tr>'
|
|
|
|
. '<td><input type="checkbox" name="privileges[]" value="%s" %s /></td>'
|
|
|
|
. '<td>%s</td>'
|
|
|
|
. '<td>%s</td>'
|
|
|
|
. '</tr>',
|
2017-01-21 13:58:53 +01:00
|
|
|
$privilege['id'],
|
|
|
|
($privilege['group_id'] != '' ? 'checked="checked"' : ''),
|
|
|
|
$privilege['name'],
|
|
|
|
$privilege['desc']
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
$privileges_form[] = form_submit('submit', __('Save'));
|
|
|
|
$html .= page_with_title(__('Edit group'), [
|
2017-08-28 16:21:10 +02:00
|
|
|
form(
|
|
|
|
$privileges_form,
|
|
|
|
page_link_to('admin_groups', ['action' => 'save', 'id' => $group_id])
|
|
|
|
)
|
2017-01-02 15:43:36 +01:00
|
|
|
]);
|
|
|
|
} else {
|
2017-01-03 14:12:17 +01:00
|
|
|
return error('No Group found.', true);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'save':
|
2018-11-20 16:02:03 +01:00
|
|
|
if (
|
|
|
|
$request->has('id')
|
|
|
|
&& preg_match('/^-\d{1,11}$/', $request->input('id'))
|
|
|
|
&& $request->hasPostData('submit')
|
|
|
|
) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$group_id = $request->input('id');
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
2017-01-03 14:12:17 +01:00
|
|
|
return error('Incomplete call, missing Groups ID.', true);
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
$group = DB::selectOne('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]);
|
2017-08-29 16:21:25 +02:00
|
|
|
$privileges = $request->postData('privileges');
|
2017-07-18 21:38:53 +02:00
|
|
|
if (!is_array($privileges)) {
|
|
|
|
$privileges = [];
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
2017-01-21 13:58:53 +01:00
|
|
|
if (!empty($group)) {
|
|
|
|
DB::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]);
|
2017-01-02 15:43:36 +01:00
|
|
|
$privilege_names = [];
|
2017-07-18 21:38:53 +02:00
|
|
|
foreach ($privileges as $privilege) {
|
2017-01-21 19:47:44 +01:00
|
|
|
if (preg_match('/^\d{1,}$/', $privilege)) {
|
2017-07-28 20:11:09 +02:00
|
|
|
$group_privileges_source = DB::selectOne(
|
2017-01-21 13:58:53 +01:00
|
|
|
'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1',
|
|
|
|
[$privilege]
|
|
|
|
);
|
|
|
|
if (!empty($group_privileges_source)) {
|
|
|
|
DB::insert(
|
|
|
|
'INSERT INTO `GroupPrivileges` (`group_id`, `privilege_id`) VALUES (?, ?)',
|
|
|
|
[$group_id, $privilege]
|
|
|
|
);
|
|
|
|
$privilege_names[] = $group_privileges_source['name'];
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
engelsystem_log(
|
2017-01-03 14:12:17 +01:00
|
|
|
'Group privileges of group ' . $group['Name']
|
|
|
|
. ' edited: ' . join(', ', $privilege_names)
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(page_link_to('admin_groups'));
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
2017-01-03 14:12:17 +01:00
|
|
|
return error('No Group found.', true);
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
break;
|
2016-09-29 10:53:17 +02:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return $html;
|
2011-06-02 22:40:08 +02:00
|
|
|
}
|