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.

513 lines
14 KiB
PHTML

<?php
use Engelsystem\Database\DB;
use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User;
use Engelsystem\ShiftCalendarRenderer;
8 years ago
use Engelsystem\ShiftsFilter;
/**
* Route user actions.
*
* @return array
*/
8 years ago
function users_controller()
{
$user = auth()->user();
$request = request();
8 years ago
if (!$user) {
8 years ago
redirect(page_link_to(''));
}
8 years ago
$action = 'list';
if ($request->has('action')) {
$action = $request->input('action');
8 years ago
}
8 years ago
switch ($action) {
8 years ago
case 'view':
return user_controller();
case 'delete':
return user_delete_controller();
case 'edit_vouchers':
return user_edit_vouchers_controller();
case 'list':
default:
return users_list_controller();
8 years ago
}
}
/**
* Delete a user, requires to enter own password for reasons.
*
* @return array
*/
8 years ago
function user_delete_controller()
{
global $privileges;
$user = auth()->user();
$request = request();
8 years ago
if ($request->has('user_id')) {
$user_source = User::find($request->query->get('user_id'));
8 years ago
} else {
$user_source = $user;
}
8 years ago
if (!in_array('admin_user', $privileges)) {
8 years ago
redirect(page_link_to(''));
}
8 years ago
// You cannot delete yourself
if ($user->id == $user_source->id) {
error(__('You cannot delete yourself.'));
redirect(user_link($user->id));
8 years ago
}
if ($request->has('submit')) {
8 years ago
$valid = true;
8 years ago
if (
!(
$request->has('password')
&& verify_password($request->postData('password'), $user->password, $user->id)
)
8 years ago
) {
8 years ago
$valid = false;
error(__('Your password is incorrect. Please try it again.'));
8 years ago
}
8 years ago
8 years ago
if ($valid) {
$user_source->delete();
8 years ago
8 years ago
mail_user_delete($user_source);
success(__('User deleted.'));
engelsystem_log(sprintf('Deleted %s', User_Nick_render($user_source)));
8 years ago
8 years ago
redirect(users_link());
}
}
8 years ago
8 years ago
return [
sprintf(__('Delete %s'), $user_source->name),
8 years ago
User_delete_view($user_source)
];
}
/**
* @return string
*/
8 years ago
function users_link()
{
return page_link_to('users');
}
/**
* @param int $userId
* @return string
*/
function user_edit_link($userId)
8 years ago
{
return page_link_to('admin_user', ['user_id' => $userId]);
}
/**
* @param int $userId
* @return string
*/
function user_delete_link($userId)
8 years ago
{
return page_link_to('users', ['action' => 'delete', 'user_id' => $userId]);
}
/**
* @param int $userId
* @return string
*/
function user_link($userId)
8 years ago
{
return page_link_to('users', ['action' => 'view', 'user_id' => $userId]);
}
/**
* @return array
*/
8 years ago
function user_edit_vouchers_controller()
{
global $privileges;
$user = auth()->user();
$request = request();
8 years ago
if ($request->has('user_id')) {
$user_source = User::find($request->input('user_id'));
8 years ago
} else {
$user_source = $user;
}
8 years ago
if (!in_array('admin_user', $privileges)) {
8 years ago
redirect(page_link_to(''));
}
8 years ago
if ($request->has('submit')) {
8 years ago
$valid = true;
8 years ago
$vouchers = '';
if (
$request->has('vouchers')
&& test_request_int('vouchers')
&& trim($request->input('vouchers')) >= 0
) {
$vouchers = trim($request->input('vouchers'));
8 years ago
} else {
$valid = false;
error(__('Please enter a valid number of vouchers.'));
8 years ago
}
8 years ago
8 years ago
if ($valid) {
$user_source->state->got_voucher = $vouchers;
$user_source->state->save();
8 years ago
success(__('Saved the number of vouchers.'));
engelsystem_log(User_Nick_render($user_source) . ': ' . sprintf('Got %s vouchers',
$user_source->state->got_voucher));
8 years ago
redirect(user_link($user_source->id));
8 years ago
}
}
8 years ago
8 years ago
return [
sprintf(__('%s\'s vouchers'), $user_source->name),
8 years ago
User_edit_vouchers_view($user_source)
];
}
/**
* @return array
*/
8 years ago
function user_controller()
{
global $privileges;
$user = auth()->user();
$request = request();
8 years ago
8 years ago
$user_source = $user;
if ($request->has('user_id')) {
$user_source = User::find($request->input('user_id'));
if (!$user_source) {
error(__('User not found.'));
redirect(page_link_to('/'));
8 years ago
}
}
8 years ago
$shifts = Shifts_by_user($user_source->id, in_array('user_shifts_admin', $privileges));
8 years ago
foreach ($shifts as &$shift) {
// TODO: Move queries to model
$shift['needed_angeltypes'] = DB::select('
SELECT DISTINCT `AngelTypes`.*
FROM `ShiftEntry`
JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id`
WHERE `ShiftEntry`.`SID` = ?
ORDER BY `AngelTypes`.`name`
',
[$shift['SID']]
);
8 years ago
foreach ($shift['needed_angeltypes'] as &$needed_angeltype) {
$needed_angeltype['users'] = DB::select('
SELECT `ShiftEntry`.`freeloaded`, `users`.*
FROM `ShiftEntry`
JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
WHERE `ShiftEntry`.`SID` = ?
AND `ShiftEntry`.`TID` = ?
',
[$shift['SID'], $needed_angeltype['id']]
);
8 years ago
}
}
if (empty($user_source->api_key)) {
8 years ago
User_reset_api_key($user_source, false);
}
if ($user_source->state->force_active) {
$tshirt_score = __('Enough');
} else {
$tshirt_score = sprintf('%.2f', User_tshirt_score($user_source->id)) . '&nbsp;h';
}
8 years ago
8 years ago
return [
$user_source->name,
8 years ago
User_view(
$user_source,
in_array('admin_user', $privileges),
User_is_freeloader($user_source),
User_angeltypes($user_source->id),
User_groups($user_source->id),
8 years ago
$shifts,
$user->id == $user_source->id,
$tshirt_score,
in_array('admin_active', $privileges),
in_array('admin_user_worklog', $privileges),
UserWorkLogsForUser($user_source->id)
8 years ago
)
];
}
/**
* List all users.
*
* @return array
*/
8 years ago
function users_list_controller()
{
global $privileges;
$request = request();
8 years ago
if (!in_array('admin_user', $privileges)) {
8 years ago
redirect(page_link_to(''));
}
8 years ago
$order_by = 'name';
if ($request->has('OrderBy') && in_array($request->input('OrderBy'), [
'name',
'last_name',
'first_name',
'dect',
'email',
'shirt_size',
'arrived',
'active',
'force_active',
'got_shirt',
'last_login_at',
])) {
$order_by = $request->input('OrderBy');
8 years ago
}
8 years ago
/** @var User[] $users */
$users = User::query()
->leftJoin('users_contact', 'users.id', '=', 'users_contact.user_id')
->leftJoin('users_personal_data', 'users.id', '=', 'users_personal_data.user_id')
->leftJoin('users_state', 'users.id', '=', 'users_state.user_id')
->orderBy($order_by)
->orderBy('name')
->get();
foreach ($users as $user) {
$user->setAttribute('freeloads', count(ShiftEntries_freeloaded_by_user($user->id)));
8 years ago
}
8 years ago
8 years ago
return [
__('All users'),
8 years ago
Users_view(
$users,
$order_by,
State::whereArrived(true)->count(),
State::whereActive(true)->count(),
State::whereForceActive(true)->count(),
ShiftEntries_freeloaded_count(),
State::whereGotShirt(true)->count(),
State::query()->sum('got_voucher')
8 years ago
)
];
}
/**
* Second step of password recovery: set a new password using the token link from email
*
* @return string
*/
8 years ago
function user_password_recovery_set_new_controller()
{
$request = request();
$passwordReset = PasswordReset::whereToken($request->input('token'))->first();
if (!$passwordReset) {
error(__('Token is not correct.'));
8 years ago
redirect(page_link_to('login'));
}
8 years ago
if ($request->has('submit')) {
8 years ago
$valid = true;
if (
$request->has('password')
&& strlen($request->postData('password')) >= config('min_password_length')
) {
if ($request->postData('password') != $request->postData('password2')) {
8 years ago
$valid = false;
error(__('Your passwords don\'t match.'));
8 years ago
}
} else {
$valid = false;
error(__('Your password is to short (please use at least 6 characters).'));
8 years ago
}
8 years ago
8 years ago
if ($valid) {
set_password($passwordReset->user->id, $request->postData('password'));
success(__('Password saved.'));
$passwordReset->delete();
8 years ago
redirect(page_link_to('login'));
}
}
8 years ago
8 years ago
return User_password_set_view();
}
/**
* First step of password recovery: display a form that asks for your email and send email with recovery link
*
* @return string
*/
8 years ago
function user_password_recovery_start_controller()
{
$request = request();
if ($request->has('submit')) {
8 years ago
$valid = true;
8 years ago
$user_source = null;
if ($request->has('email') && strlen(strip_request_item('email')) > 0) {
8 years ago
$email = strip_request_item('email');
if (check_email($email)) {
/** @var User $user_source */
$user_source = User::whereEmail($email)->first();
if (!$user_source) {
8 years ago
$valid = false;
error(__('E-mail address is not correct.'));
8 years ago
}
} else {
$valid = false;
error(__('E-mail address is not correct.'));
8 years ago
}
} else {
$valid = false;
error(__('Please enter your e-mail.'));
}
8 years ago
8 years ago
if ($valid) {
$token = User_generate_password_recovery_token($user_source);
8 years ago
engelsystem_email_to_user(
$user_source,
__('Password recovery'),
8 years ago
sprintf(
__('Please visit %s to recover your password.'),
page_link_to('user_password_recovery', ['token' => $token])
8 years ago
)
);
success(__('We sent an email containing your password recovery link.'));
8 years ago
redirect(page_link_to('login'));
}
}
8 years ago
8 years ago
return User_password_recovery_view();
}
/**
* User password recovery in 2 steps.
* (By email)
*
* @return string
*/
8 years ago
function user_password_recovery_controller()
{
if (request()->has('token')) {
8 years ago
return user_password_recovery_set_new_controller();
}
return user_password_recovery_start_controller();
}
/**
* Menu title for password recovery.
*
* @return string
*/
8 years ago
function user_password_recovery_title()
{
return __('Password recovery');
}
/**
* Loads a user from param user_id.
*
* @return User
*/
8 years ago
function load_user()
{
$request = request();
if (!$request->has('user_id')) {
8 years ago
redirect(page_link_to());
}
8 years ago
$user = User::find($request->input('user_id'));
if (!$user) {
error(__('User doesn\'t exist.'));
8 years ago
redirect(page_link_to());
}
8 years ago
8 years ago
return $user;
}
/**
* @param ShiftsFilter $shiftsFilter
* @return ShiftCalendarRenderer
*/
8 years ago
function shiftCalendarRendererByShiftFilter(ShiftsFilter $shiftsFilter)
{
$shifts = Shifts_by_ShiftsFilter($shiftsFilter);
$needed_angeltypes_source = NeededAngeltypes_by_ShiftsFilter($shiftsFilter);
$shift_entries_source = ShiftEntries_by_ShiftsFilter($shiftsFilter);
8 years ago
8 years ago
$needed_angeltypes = [];
$shift_entries = [];
foreach ($shifts as $shift) {
$needed_angeltypes[$shift['SID']] = [];
$shift_entries[$shift['SID']] = [];
}
8 years ago
foreach ($shift_entries_source as $shift_entry) {
if (isset($shift_entries[$shift_entry['SID']])) {
$shift_entries[$shift_entry['SID']][] = $shift_entry;
}
}
8 years ago
foreach ($needed_angeltypes_source as $needed_angeltype) {
if (isset($needed_angeltypes[$needed_angeltype['SID']])) {
$needed_angeltypes[$needed_angeltype['SID']][] = $needed_angeltype;
}
}
8 years ago
unset($needed_angeltypes_source);
unset($shift_entries_source);
8 years ago
if (
in_array(ShiftsFilter::FILLED_FREE, $shiftsFilter->getFilled())
&& in_array(ShiftsFilter::FILLED_FILLED, $shiftsFilter->getFilled())
) {
8 years ago
return new ShiftCalendarRenderer($shifts, $needed_angeltypes, $shift_entries, $shiftsFilter);
}
8 years ago
8 years ago
$filtered_shifts = [];
foreach ($shifts as $shift) {
$needed_angels_count = 0;
$taken = 0;
8 years ago
foreach ($needed_angeltypes[$shift['SID']] as $needed_angeltype) {
$taken = 0;
foreach ($shift_entries[$shift['SID']] as $shift_entry) {
if ($needed_angeltype['angel_type_id'] == $shift_entry['TID'] && $shift_entry['freeloaded'] == 0) {
8 years ago
$taken++;
8 years ago
}
}
8 years ago
8 years ago
$needed_angels_count += max(0, $needed_angeltype['count'] - $taken);
}
if (in_array(ShiftsFilter::FILLED_FREE, $shiftsFilter->getFilled()) && $taken < $needed_angels_count) {
$filtered_shifts[] = $shift;
}
if (
in_array(ShiftsFilter::FILLED_FILLED, $shiftsFilter->getFilled())
&& $taken >= $needed_angels_count
) {
8 years ago
$filtered_shifts[] = $shift;
}
}
8 years ago
8 years ago
return new ShiftCalendarRenderer($filtered_shifts, $needed_angeltypes, $shift_entries, $shiftsFilter);
}