engelsystem/includes/controller/users_controller.php

440 lines
11 KiB
PHP
Raw Normal View History

2013-12-26 13:34:48 +01:00
<?php
use Engelsystem\Database\Db;
2023-01-18 13:02:11 +01:00
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\User\State;
2018-10-09 21:47:31 +02:00
use Engelsystem\Models\User\User;
2016-12-28 14:53:35 +01:00
use Engelsystem\ShiftCalendarRenderer;
2017-01-02 15:43:36 +01:00
use Engelsystem\ShiftsFilter;
2020-04-24 20:01:26 +02:00
use Illuminate\Database\Eloquent\Collection;
2020-11-17 21:34:34 +01:00
use Illuminate\Support\Str;
2013-12-26 13:34:48 +01:00
/**
2014-08-22 22:34:13 +02:00
* Route user actions.
2017-01-03 03:22:48 +01:00
*
* @return array
2014-08-22 22:34:13 +02:00
*/
2017-01-02 03:57:23 +01:00
function users_controller()
{
2018-10-31 12:48:22 +01:00
$user = auth()->user();
$request = request();
2017-01-02 15:43:36 +01:00
2018-10-08 21:15:56 +02:00
if (!$user) {
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$action = 'list';
if ($request->has('action')) {
$action = $request->input('action');
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2022-12-22 00:08:54 +01:00
return match ($action) {
'view' => user_controller(),
'delete' => user_delete_controller(),
'edit_vouchers' => user_edit_vouchers_controller(),
'list' => users_list_controller(),
default => users_list_controller(),
};
2014-08-22 22:34:13 +02:00
}
/**
* Delete a user, requires to enter own password for reasons.
2017-01-03 03:22:48 +01:00
*
* @return array
*/
2017-01-02 03:57:23 +01:00
function user_delete_controller()
{
2018-10-09 21:47:31 +02:00
$user = auth()->user();
$auth = auth();
$request = request();
2017-01-02 15:43:36 +01:00
if ($request->has('user_id')) {
2018-10-09 21:47:31 +02:00
$user_source = User::find($request->query->get('user_id'));
2017-01-02 03:57:23 +01:00
} else {
$user_source = $user;
}
2017-01-02 15:43:36 +01:00
if (!auth()->can('admin_user')) {
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
// You cannot delete yourself
2018-10-09 21:47:31 +02:00
if ($user->id == $user_source->id) {
error(__('You cannot delete yourself.'));
2019-09-08 02:25:49 +02:00
throw_redirect(user_link($user->id));
2017-01-02 15:43:36 +01:00
}
if ($request->hasPostData('submit')) {
2017-01-02 03:57:23 +01:00
$valid = true;
2017-01-02 15:43:36 +01:00
2022-10-18 19:15:22 +02:00
if (
!(
$request->has('password')
&& $auth->verifyPassword($user, $request->postData('password'))
2022-10-18 19:15:22 +02:00
)
) {
2017-01-02 03:57:23 +01:00
$valid = false;
2020-11-24 17:27:21 +01:00
error(__('auth.password.error'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($valid) {
// Load data before user deletion to prevent errors when displaying
$user_source->load(['contact', 'personalData', 'settings', 'state']);
$user_source->delete();
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
mail_user_delete($user_source);
success(__('User deleted.'));
2019-05-31 04:03:19 +02:00
engelsystem_log(sprintf('Deleted %s', User_Nick_render($user_source, true)));
2017-01-02 15:43:36 +01:00
2019-09-08 02:25:49 +02:00
throw_redirect(users_link());
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 [
2018-10-09 21:47:31 +02:00
sprintf(__('Delete %s'), $user_source->name),
User_delete_view($user_source),
2017-01-02 15:43:36 +01:00
];
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function users_link()
{
return page_link_to('users');
2014-12-27 21:55:24 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @param int $userId
2017-01-03 03:22:48 +01:00
* @return string
*/
function user_edit_link($userId)
2017-01-02 03:57:23 +01:00
{
return page_link_to('admin_user', ['user_id' => $userId]);
}
2017-01-03 03:22:48 +01:00
/**
* @param int $userId
2017-01-03 03:22:48 +01:00
* @return string
*/
function user_delete_link($userId)
2017-01-02 03:57:23 +01:00
{
return page_link_to('users', ['action' => 'delete', 'user_id' => $userId]);
}
2017-01-03 03:22:48 +01:00
/**
* @param int $userId
2017-01-03 03:22:48 +01:00
* @return string
*/
function user_link($userId)
2017-01-02 03:57:23 +01:00
{
return page_link_to('users', ['action' => 'view', 'user_id' => $userId]);
2014-12-26 01:49:59 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @return array
*/
2017-01-02 03:57:23 +01:00
function user_edit_vouchers_controller()
{
2018-10-10 03:10:28 +02:00
$user = auth()->user();
$request = request();
2017-01-02 15:43:36 +01:00
if ($request->has('user_id')) {
2018-10-10 03:10:28 +02:00
$user_source = User::find($request->input('user_id'));
2017-01-02 03:57:23 +01:00
} else {
$user_source = $user;
}
2017-01-02 15:43:36 +01:00
if (
(!auth()->can('admin_user') && !auth()->can('voucher.edit'))
|| !config('enable_voucher')
) {
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
if ($request->hasPostData('submit')) {
2017-01-02 03:57:23 +01:00
$valid = true;
2017-01-02 15:43:36 +01:00
2017-01-03 03:22:48 +01:00
$vouchers = '';
if (
$request->has('vouchers')
&& test_request_int('vouchers')
&& trim($request->input('vouchers')) >= 0
) {
$vouchers = trim($request->input('vouchers'));
2017-01-02 03:57:23 +01:00
} else {
$valid = false;
error(__('Please enter a valid number of vouchers.'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($valid) {
2018-10-10 03:10:28 +02:00
$user_source->state->got_voucher = $vouchers;
2018-10-17 01:30:10 +02:00
$user_source->state->save();
2017-01-02 15:43:36 +01:00
success(__('Saved the number of vouchers.'));
2022-10-18 19:15:22 +02:00
engelsystem_log(User_Nick_render($user_source, true) . ': ' . sprintf(
'Got %s vouchers',
$user_source->state->got_voucher
));
2017-01-02 15:43:36 +01:00
2019-09-08 02:25:49 +02:00
throw_redirect(user_link($user_source->id));
2017-01-02 03:57:23 +01:00
}
2015-08-12 23:44:39 +02:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return [
2018-10-10 03:10:28 +02:00
sprintf(__('%s\'s vouchers'), $user_source->name),
User_edit_vouchers_view($user_source),
2017-01-02 15:43:36 +01:00
];
2014-12-26 01:49:59 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @return array
*/
2017-01-02 03:57:23 +01:00
function user_controller()
{
2018-10-10 03:10:28 +02:00
$user = auth()->user();
$request = request();
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$user_source = $user;
if ($request->has('user_id')) {
2018-10-10 03:10:28 +02:00
$user_source = User::find($request->input('user_id'));
if (!$user_source) {
error(__('User not found.'));
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('/'));
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
$shifts = Shifts_by_user($user_source->id, auth()->can('user_shifts_admin'));
2023-01-03 22:19:03 +01:00
foreach ($shifts as $shift) {
2017-01-02 03:57:23 +01:00
// TODO: Move queries to model
2023-01-03 22:19:03 +01:00
$shift->needed_angeltypes = Db::select(
2022-10-18 19:15:22 +02:00
'
2022-11-09 00:02:30 +01:00
SELECT DISTINCT `angel_types`.*
2023-01-18 13:02:11 +01:00
FROM `shift_entries`
JOIN `angel_types` ON `shift_entries`.`angel_type_id`=`angel_types`.`id`
WHERE `shift_entries`.`shift_id` = ?
2022-11-09 00:02:30 +01:00
ORDER BY `angel_types`.`name`
',
2023-01-03 22:19:03 +01:00
[$shift->id]
);
2023-01-03 22:19:03 +01:00
$neededAngeltypes = $shift->needed_angeltypes;
foreach ($neededAngeltypes as &$needed_angeltype) {
2022-10-18 19:15:22 +02:00
$needed_angeltype['users'] = Db::select(
'
2023-01-18 13:02:11 +01:00
SELECT `shift_entries`.`freeloaded`, `users`.*
FROM `shift_entries`
JOIN `users` ON `shift_entries`.`user_id`=`users`.`id`
WHERE `shift_entries`.`shift_id` = ?
AND `shift_entries`.`angel_type_id` = ?
',
2023-01-03 22:19:03 +01:00
[$shift->id, $needed_angeltype['id']]
);
2017-01-02 03:57:23 +01:00
}
2023-01-03 22:19:03 +01:00
$shift->needed_angeltypes = $neededAngeltypes;
2014-08-23 01:55:18 +02:00
}
2018-01-14 17:47:26 +01:00
2018-10-10 03:10:28 +02:00
if (empty($user_source->api_key)) {
2017-01-02 03:57:23 +01:00
User_reset_api_key($user_source, false);
}
2018-01-14 17:47:26 +01:00
2018-10-10 03:10:28 +02:00
if ($user_source->state->force_active) {
$tshirt_score = __('Enough');
2017-12-27 20:30:05 +01:00
} else {
2018-10-10 03:10:28 +02:00
$tshirt_score = sprintf('%.2f', User_tshirt_score($user_source->id)) . '&nbsp;h';
2017-12-27 20:30:05 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return [
2018-10-10 03:10:28 +02:00
$user_source->name,
2017-01-02 15:43:36 +01:00
User_view(
$user_source,
auth()->can('admin_user'),
2023-01-18 13:02:11 +01:00
$user_source->isFreeloader(),
2022-12-03 00:57:04 +01:00
$user_source->userAngelTypes,
$user_source->groups,
2017-01-02 15:43:36 +01:00
$shifts,
2018-10-10 03:10:28 +02:00
$user->id == $user_source->id,
2017-12-27 20:30:05 +01:00
$tshirt_score,
auth()->can('admin_active'),
auth()->can('admin_user_worklog'),
2018-10-10 03:10:28 +02:00
UserWorkLogsForUser($user_source->id)
),
2017-01-02 15:43:36 +01:00
];
2014-08-22 22:34:13 +02:00
}
2014-09-28 15:01:02 +02:00
/**
* List all users.
2017-01-03 03:22:48 +01:00
*
* @return array
2014-09-28 15:01:02 +02:00
*/
2017-01-02 03:57:23 +01:00
function users_list_controller()
{
$request = request();
2017-01-02 15:43:36 +01:00
if (!auth()->can('admin_user')) {
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2018-10-17 01:30:10 +02:00
$order_by = 'name';
2022-10-18 19:15:22 +02:00
if (
$request->has('OrderBy') && in_array($request->input('OrderBy'), [
2018-10-17 01:30:10 +02:00
'name',
'first_name',
2020-04-24 20:01:26 +02:00
'last_name',
2018-10-17 01:30:10 +02:00
'dect',
'arrived',
2020-04-24 20:01:26 +02:00
'got_voucher',
'freeloads',
2018-10-17 01:30:10 +02:00
'active',
'force_active',
2018-10-17 01:30:10 +02:00
'got_shirt',
2020-04-24 20:01:26 +02:00
'shirt_size',
'planned_arrival_date',
'planned_departure_date',
2018-10-17 01:30:10 +02:00
'last_login_at',
2022-10-18 19:15:22 +02:00
])
) {
$order_by = $request->input('OrderBy');
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2020-04-24 20:01:26 +02:00
/** @var User[]|Collection $users */
2019-12-26 19:07:51 +01:00
$users = User::with(['contact', 'personalData', 'state'])
2018-10-17 01:30:10 +02:00
->orderBy('name')
->get();
foreach ($users as $user) {
2023-01-18 13:02:11 +01:00
$user->setAttribute(
'freeloads',
$user->shiftEntries()
->where('freeloaded', true)
->count()
);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2020-04-24 20:01:26 +02:00
$users = $users->sortBy(function (User $user) use ($order_by) {
$userData = $user->toArray();
$data = [];
array_walk_recursive($userData, function ($value, $key) use (&$data) {
$data[$key] = $value;
});
2020-11-17 21:34:34 +01:00
return isset($data[$order_by]) ? Str::lower($data[$order_by]) : null;
2020-04-24 20:01:26 +02:00
});
2017-01-02 03:57:23 +01:00
return [
__('All users'),
2017-01-02 15:43:36 +01:00
Users_view(
$users,
$order_by,
State::whereArrived(true)->count(),
State::whereActive(true)->count(),
State::whereForceActive(true)->count(),
2023-01-18 13:02:11 +01:00
ShiftEntry::whereFreeloaded(true)->count(),
State::whereGotShirt(true)->count(),
State::query()->sum('got_voucher')
),
2017-01-02 15:43:36 +01:00
];
2014-08-22 22:34:13 +02:00
}
2016-09-30 16:55:47 +02:00
/**
* Loads a user from param user_id.
2017-01-03 03:22:48 +01:00
*
2018-10-10 03:10:28 +02:00
* @return User
2016-09-30 16:55:47 +02:00
*/
2017-01-02 03:57:23 +01:00
function load_user()
{
$request = request();
if (!$request->has('user_id')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2018-10-10 03:10:28 +02:00
$user = User::find($request->input('user_id'));
if (!$user) {
error(__('User doesn\'t exist.'));
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to());
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 $user;
2016-09-30 16:55:47 +02:00
}
2017-01-03 03:22:48 +01:00
/**
* @param ShiftsFilter $shiftsFilter
* @return ShiftCalendarRenderer
*/
2017-01-02 03:57:23 +01:00
function shiftCalendarRendererByShiftFilter(ShiftsFilter $shiftsFilter)
{
$shifts = Shifts_by_ShiftsFilter($shiftsFilter);
$needed_angeltypes_source = NeededAngeltypes_by_ShiftsFilter($shiftsFilter);
$shift_entries_source = ShiftEntries_by_ShiftsFilter($shiftsFilter);
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angeltypes = [];
2023-01-18 13:02:11 +01:00
/** @var ShiftEntry[][] $shift_entries */
2017-01-02 03:57:23 +01:00
$shift_entries = [];
foreach ($shifts as $shift) {
2023-01-03 22:19:03 +01:00
$needed_angeltypes[$shift->id] = [];
$shift_entries[$shift->id] = [];
2016-12-29 14:51:43 +01:00
}
2018-12-30 04:32:03 +01:00
2017-01-02 03:57:23 +01:00
foreach ($shift_entries_source as $shift_entry) {
2023-01-18 13:02:11 +01:00
if (isset($shift_entries[$shift_entry->shift_id])) {
$shift_entries[$shift_entry->shift_id][] = $shift_entry;
2017-01-02 03:57:23 +01:00
}
2016-12-29 14:51:43 +01:00
}
2018-12-30 04:32:03 +01:00
2017-01-02 03:57:23 +01:00
foreach ($needed_angeltypes_source as $needed_angeltype) {
2023-01-03 22:19:03 +01:00
if (isset($needed_angeltypes[$needed_angeltype['shift_id']])) {
$needed_angeltypes[$needed_angeltype['shift_id']][] = $needed_angeltype;
2016-12-29 15:33:21 +01:00
}
2016-12-29 14:51:43 +01:00
}
2018-12-30 04:32:03 +01:00
2017-01-02 03:57:23 +01:00
unset($needed_angeltypes_source);
unset($shift_entries_source);
2017-01-02 15:43:36 +01:00
if (
in_array(ShiftsFilter::FILLED_FREE, $shiftsFilter->getFilled())
&& in_array(ShiftsFilter::FILLED_FILLED, $shiftsFilter->getFilled())
) {
2017-01-02 03:57:23 +01:00
return new ShiftCalendarRenderer($shifts, $needed_angeltypes, $shift_entries, $shiftsFilter);
2016-12-29 14:51:43 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$filtered_shifts = [];
foreach ($shifts as $shift) {
$needed_angels_count = 0;
2023-01-03 22:19:03 +01:00
foreach ($needed_angeltypes[$shift->id] as $needed_angeltype) {
2017-01-02 03:57:23 +01:00
$taken = 0;
2018-12-30 04:32:03 +01:00
if (
!in_array(ShiftsFilter::FILLED_FILLED, $shiftsFilter->getFilled())
&& !in_array($needed_angeltype['angel_type_id'], $shiftsFilter->getTypes())
) {
continue;
}
2023-01-03 22:19:03 +01:00
foreach ($shift_entries[$shift->id] as $shift_entry) {
2018-12-30 04:32:03 +01:00
if (
2023-01-18 13:02:11 +01:00
$needed_angeltype['angel_type_id'] == $shift_entry->angel_type_id
&& !$shift_entry->freeloaded
2018-12-30 04:32:03 +01:00
) {
2017-01-02 15:43:36 +01:00
$taken++;
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angels_count += max(0, $needed_angeltype['count'] - $taken);
}
2018-12-30 04:32:03 +01:00
if (
in_array(ShiftsFilter::FILLED_FREE, $shiftsFilter->getFilled())
&& $needed_angels_count > 0
2018-12-30 04:32:03 +01:00
) {
2017-01-02 03:57:23 +01:00
$filtered_shifts[] = $shift;
}
2018-12-30 04:32:03 +01:00
2017-12-25 23:12:52 +01:00
if (
in_array(ShiftsFilter::FILLED_FILLED, $shiftsFilter->getFilled())
&& $needed_angels_count == 0
2017-12-25 23:12:52 +01:00
) {
2017-01-02 03:57:23 +01:00
$filtered_shifts[] = $shift;
}
2016-12-29 14:51:43 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return new ShiftCalendarRenderer($filtered_shifts, $needed_angeltypes, $shift_entries, $shiftsFilter);
2016-12-28 14:53:35 +01:00
}