2011-06-02 22:40:08 +02:00
|
|
|
<?php
|
2016-09-29 10:53:17 +02:00
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
use Engelsystem\Models\Group;
|
|
|
|
use Engelsystem\Models\Privilege;
|
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-01-21 13:58:53 +01:00
|
|
|
|
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();
|
2022-11-06 12:41:52 +01:00
|
|
|
/** @var Group[]|Collection $groups */
|
2023-12-13 14:42:25 +01:00
|
|
|
$groups = Group::with('privileges')->orderBy('name')->get();
|
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) {
|
2022-11-06 12:41:52 +01:00
|
|
|
/** @var Privilege[]|Collection $privileges */
|
2023-12-13 14:42:25 +01:00
|
|
|
$privileges = $group->privileges->sortBy('name');
|
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) {
|
2023-12-04 23:33:07 +01:00
|
|
|
$privileges_html[] = htmlspecialchars($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[] = [
|
2023-12-04 23:33:07 +01:00
|
|
|
'name' => htmlspecialchars($group->name),
|
2017-01-02 15:43:36 +01:00
|
|
|
'privileges' => join(', ', $privileges_html),
|
|
|
|
'actions' => button(
|
2023-11-13 16:56:52 +01:00
|
|
|
url(
|
|
|
|
'/admin-groups',
|
2022-11-06 12:41:52 +01:00
|
|
|
['action' => 'edit', 'id' => $group->id]
|
2022-10-18 19:15:22 +02:00
|
|
|
),
|
2023-12-21 13:08:29 +01:00
|
|
|
icon('pencil'),
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
__('form.edit'),
|
2021-07-23 01:52:19 +02:00
|
|
|
'btn-sm'
|
2023-02-05 18:03:00 +01:00
|
|
|
),
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
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([
|
2023-10-02 16:15:25 +02:00
|
|
|
'name' => __('general.name'),
|
2018-08-29 21:55:32 +02:00
|
|
|
'privileges' => __('Privileges'),
|
2023-02-05 18:03:00 +01:00
|
|
|
'actions' => '',
|
|
|
|
], $groups_table),
|
2017-01-02 15:43:36 +01:00
|
|
|
]);
|
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':
|
2019-07-28 15:33:01 +02:00
|
|
|
if ($request->has('id')) {
|
2022-12-25 11:59:45 +01:00
|
|
|
$group_id = (int) $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
|
|
|
}
|
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
/** @var Group|null $group */
|
|
|
|
$group = Group::find($group_id);
|
2017-01-21 13:58:53 +01:00
|
|
|
if (!empty($group)) {
|
2022-11-06 12:41:52 +01:00
|
|
|
$privileges = groupPrivilegesWithSelected($group);
|
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[]',
|
2023-12-04 23:33:07 +01:00
|
|
|
htmlspecialchars($privilege->description . ' (' . $privilege->name . ')'),
|
2022-11-06 12:41:52 +01:00
|
|
|
$privilege->selected != '',
|
|
|
|
$privilege->id,
|
2023-12-04 23:33:07 +01:00
|
|
|
'privilege-' . htmlspecialchars($privilege->name)
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2023-12-21 13:08:29 +01:00
|
|
|
$privileges_form[] = form_submit('submit', icon('save') . __('form.save'));
|
2023-12-04 23:33:07 +01:00
|
|
|
$html .= page_with_title(__('Edit group') . ' ' . htmlspecialchars($group->name), [
|
2017-08-28 16:21:10 +02:00
|
|
|
form(
|
|
|
|
$privileges_form,
|
2023-11-13 16:56:52 +01:00
|
|
|
url('/admin-groups', ['action' => 'save', 'id' => $group->id])
|
2023-02-05 18:03:00 +01:00
|
|
|
),
|
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')
|
|
|
|
&& $request->hasPostData('submit')
|
|
|
|
) {
|
2022-12-25 11:59:45 +01:00
|
|
|
$group_id = (int) $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
|
|
|
}
|
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
/** @var Group|null $group */
|
|
|
|
$group = Group::find($group_id);
|
2020-09-06 23:50:36 +02:00
|
|
|
$privileges = $request->request->all('privileges');
|
2017-01-21 13:58:53 +01:00
|
|
|
if (!empty($group)) {
|
2022-11-06 12:41:52 +01:00
|
|
|
$group->privileges()->detach();
|
2017-01-02 15:43:36 +01:00
|
|
|
$privilege_names = [];
|
2017-07-18 21:38:53 +02:00
|
|
|
foreach ($privileges as $privilege) {
|
2022-11-06 12:41:52 +01:00
|
|
|
$privilege = Privilege::find($privilege);
|
2019-07-28 15:33:01 +02:00
|
|
|
if ($privilege) {
|
2022-11-06 12:41:52 +01:00
|
|
|
$group->privileges()->attach($privilege);
|
|
|
|
$privilege_names[] = $privilege->name;
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
engelsystem_log(
|
2022-11-06 12:41:52 +01:00
|
|
|
'Group privileges of group ' . $group->name
|
2017-01-03 14:12:17 +01:00
|
|
|
. ' edited: ' . join(', ', $privilege_names)
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2023-11-13 16:56:52 +01:00
|
|
|
throw_redirect(url('/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
|
|
|
}
|
2022-11-06 12:41:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Group $group
|
|
|
|
* @return Collection|Privilege[]
|
|
|
|
*/
|
|
|
|
function groupPrivilegesWithSelected(Group $group): Collection
|
|
|
|
{
|
|
|
|
return Privilege::query()
|
|
|
|
->join('group_privileges', function ($query) use ($group) {
|
|
|
|
/** @var JoinClause $query */
|
|
|
|
$query
|
|
|
|
->where('privileges.id', '=', $query->raw('group_privileges.privilege_id'))
|
|
|
|
->where('group_privileges.group_id', $group->id)
|
|
|
|
;
|
|
|
|
}, null, null, 'left outer')
|
|
|
|
->orderBy('name')
|
|
|
|
->get([
|
|
|
|
'privileges.*',
|
2023-02-05 18:03:00 +01:00
|
|
|
'group_privileges.group_id as selected',
|
2022-11-06 12:41:52 +01:00
|
|
|
]);
|
|
|
|
}
|