You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.9 KiB
PHTML

14 years ago
<?php
use Engelsystem\Database\DB;
/**
* @return string
*/
8 years ago
function admin_groups_title()
{
return _('Grouprights');
}
/**
* @return string
*/
8 years ago
function admin_groups()
{
$html = '';
$request = request();
$groups = DB::select('SELECT * FROM `Groups` ORDER BY `Name`');
if (!$request->has('action')) {
8 years ago
$groups_table = [];
foreach ($groups as $group) {
$privileges = DB::select('
SELECT `name`
8 years ago
FROM `GroupPrivileges`
JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
WHERE `group_id`=?
', [$group['UID']]);
8 years ago
$privileges_html = [];
8 years ago
foreach ($privileges as $privilege) {
$privileges_html[] = $privilege['name'];
8 years ago
}
8 years ago
8 years ago
$groups_table[] = [
8 years ago
'name' => $group['Name'],
'privileges' => join(', ', $privileges_html),
'actions' => button(
page_link_to('admin_groups',
['action' => 'edit', 'id' => $group['UID']]),
_('edit'),
8 years ago
'btn-xs'
)
];
8 years ago
}
8 years ago
8 years ago
return page_with_title(admin_groups_title(), [
8 years ago
table([
'name' => _('Name'),
'privileges' => _('Privileges'),
8 years ago
'actions' => ''
], $groups_table)
]);
8 years ago
} else {
switch ($request->input('action')) {
8 years ago
case 'edit':
if ($request->has('id') && preg_match('/^-\d{1,11}$/', $request->input('id'))) {
$group_id = $request->input('id');
8 years ago
} else {
return error('Incomplete call, missing Groups ID.', true);
8 years ago
}
$group = DB::select('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]);
if (!empty($group)) {
$privileges = DB::select('
8 years ago
SELECT `Privileges`.*, `GroupPrivileges`.`group_id`
FROM `Privileges`
LEFT OUTER JOIN `GroupPrivileges`
ON (
`Privileges`.`id` = `GroupPrivileges`.`privilege_id`
AND `GroupPrivileges`.`group_id`=?
8 years ago
)
ORDER BY `Privileges`.`name`
', [$group_id]);
$privileges_html = '';
8 years ago
$privileges_form = [];
foreach ($privileges as $privilege) {
8 years ago
$privileges_form[] = form_checkbox(
'privileges[]',
$privilege['desc'] . ' (' . $privilege['name'] . ')',
$privilege['group_id'] != '',
$privilege['id']
8 years ago
);
$privileges_html .= sprintf(
'<tr><td><input type="checkbox" name="privileges[]" value="%s" %s /></td> <td>%s</td> <td>%s</td></tr>',
$privilege['id'],
($privilege['group_id'] != '' ? 'checked="checked"' : ''),
$privilege['name'],
$privilege['desc']
8 years ago
);
8 years ago
}
8 years ago
$privileges_form[] = form_submit('submit', _('Save'));
$html .= page_with_title(_('Edit group'), [
form(
$privileges_form,
page_link_to('admin_groups', ['action' => 'save', 'id' => $group_id])
)
8 years ago
]);
} else {
return error('No Group found.', true);
8 years ago
}
8 years ago
break;
case 'save':
if ($request->has('id') && preg_match('/^-\d{1,11}$/', $request->input('id'))) {
$group_id = $request->input('id');
8 years ago
} else {
return error('Incomplete call, missing Groups ID.', true);
8 years ago
}
$group = DB::select('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]);
$privileges = $request->get('privileges');
if (!is_array($privileges)) {
$privileges = [];
8 years ago
}
if (!empty($group)) {
$group = array_shift($group);
DB::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]);
8 years ago
$privilege_names = [];
foreach ($privileges as $privilege) {
if (preg_match('/^\d{1,}$/', $privilege)) {
$group_privileges_source = DB::select(
'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1',
[$privilege]
);
if (!empty($group_privileges_source)) {
$group_privileges_source = array_shift($group_privileges_source);
DB::insert(
'INSERT INTO `GroupPrivileges` (`group_id`, `privilege_id`) VALUES (?, ?)',
[$group_id, $privilege]
);
$privilege_names[] = $group_privileges_source['name'];
8 years ago
}
}
}
engelsystem_log(
'Group privileges of group ' . $group['Name']
. ' edited: ' . join(', ', $privilege_names)
8 years ago
);
redirect(page_link_to('admin_groups'));
8 years ago
} else {
return error('No Group found.', true);
8 years ago
}
break;
}
8 years ago
}
return $html;
14 years ago
}