2013-12-26 13:34:48 +01:00
|
|
|
<?php
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
use Engelsystem\Database\DB;
|
2018-10-09 21:47:31 +02:00
|
|
|
use Engelsystem\Models\User\PasswordReset;
|
2018-10-11 01:26:34 +02:00
|
|
|
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;
|
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();
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-08 21:15:56 +02:00
|
|
|
if (!$user) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to(''));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02: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
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
switch ($action) {
|
2017-01-02 15:43:36 +01:00
|
|
|
case 'view':
|
|
|
|
return user_controller();
|
|
|
|
case 'delete':
|
|
|
|
return user_delete_controller();
|
|
|
|
case 'edit_vouchers':
|
|
|
|
return user_edit_vouchers_controller();
|
2017-01-03 03:22:48 +01:00
|
|
|
case 'list':
|
|
|
|
default:
|
|
|
|
return users_list_controller();
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
2014-08-22 22:34:13 +02:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:48:41 +01:00
|
|
|
/**
|
|
|
|
* Delete a user, requires to enter own password for reasons.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2015-12-30 15:48:41 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_delete_controller()
|
|
|
|
{
|
2018-10-09 21:47:31 +02:00
|
|
|
global $privileges;
|
|
|
|
$user = auth()->user();
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02: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 (!in_array('admin_user', $privileges)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to(''));
|
|
|
|
}
|
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) {
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('You cannot delete yourself.'));
|
2018-10-09 21:47:31 +02:00
|
|
|
redirect(user_link($user->id));
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('submit')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = true;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if (
|
|
|
|
!(
|
|
|
|
$request->has('password')
|
2018-10-09 21:47:31 +02:00
|
|
|
&& verify_password($request->postData('password'), $user->password, $user->id)
|
2017-07-18 21:38:53 +02:00
|
|
|
)
|
2017-01-02 15:43:36 +01:00
|
|
|
) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Your password is incorrect. Please try it again.'));
|
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-11 01:26:34 +02:00
|
|
|
$user_source->delete();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
mail_user_delete($user_source);
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('User deleted.'));
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_log(sprintf('Deleted %s', User_Nick_render($user_source)));
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(users_link());
|
|
|
|
}
|
2015-12-30 15:48:41 +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),
|
2017-01-02 15:43:36 +01:00
|
|
|
User_delete_view($user_source)
|
|
|
|
];
|
2015-12-30 15:48:41 +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
|
|
|
/**
|
2018-10-09 20:38:13 +02:00
|
|
|
* @param int $userId
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-10-09 20:38:13 +02:00
|
|
|
function user_edit_link($userId)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-10-09 20:38:13 +02:00
|
|
|
return page_link_to('admin_user', ['user_id' => $userId]);
|
2015-12-30 15:48:41 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2018-10-09 20:38:13 +02:00
|
|
|
* @param int $userId
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-10-09 20:38:13 +02:00
|
|
|
function user_delete_link($userId)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-10-09 20:38:13 +02:00
|
|
|
return page_link_to('users', ['action' => 'delete', 'user_id' => $userId]);
|
2015-12-30 15:48:41 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2018-10-09 20:38:13 +02:00
|
|
|
* @param int $userId
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-10-09 20:38:13 +02:00
|
|
|
function user_link($userId)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-10-09 20:38:13 +02: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
|
|
|
global $privileges;
|
|
|
|
$user = auth()->user();
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02: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 (!in_array('admin_user', $privileges)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to(''));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('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 = '';
|
2017-07-18 21:38:53 +02:00
|
|
|
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;
|
2018-08-29 21:55:32 +02:00
|
|
|
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
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Saved the number of vouchers.'));
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_log(User_Nick_render($user_source) . ': ' . sprintf('Got %s vouchers',
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_source->state->got_voucher));
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
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),
|
2017-01-02 15:43:36 +01:00
|
|
|
User_edit_vouchers_view($user_source)
|
|
|
|
];
|
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
|
|
|
global $privileges;
|
|
|
|
$user = auth()->user();
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$user_source = $user;
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('user_id')) {
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_source = User::find($request->input('user_id'));
|
|
|
|
if (!$user_source) {
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('User not found.'));
|
2017-11-19 10:45:08 +01:00
|
|
|
redirect(page_link_to('/'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2015-12-05 11:55:35 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
$shifts = Shifts_by_user($user_source->id, in_array('user_shifts_admin', $privileges));
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($shifts as &$shift) {
|
|
|
|
// TODO: Move queries to model
|
2017-01-21 13:58:53 +01:00
|
|
|
$shift['needed_angeltypes'] = DB::select('
|
2017-01-03 03:22:48 +01:00
|
|
|
SELECT DISTINCT `AngelTypes`.*
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id`
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `ShiftEntry`.`SID` = ?
|
2017-01-03 03:22:48 +01:00
|
|
|
ORDER BY `AngelTypes`.`name`
|
2017-01-21 13:58:53 +01:00
|
|
|
',
|
|
|
|
[$shift['SID']]
|
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($shift['needed_angeltypes'] as &$needed_angeltype) {
|
2017-01-21 13:58:53 +01:00
|
|
|
$needed_angeltype['users'] = DB::select('
|
2018-10-14 18:24:42 +02:00
|
|
|
SELECT `ShiftEntry`.`freeloaded`, `users`.*
|
2017-01-21 13:58:53 +01:00
|
|
|
FROM `ShiftEntry`
|
2018-10-14 18:24:42 +02:00
|
|
|
JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `ShiftEntry`.`SID` = ?
|
|
|
|
AND `ShiftEntry`.`TID` = ?
|
|
|
|
',
|
|
|
|
[$shift['SID'], $needed_angeltype['id']]
|
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
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) {
|
2018-08-29 21:55:32 +02:00
|
|
|
$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)) . ' 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,
|
|
|
|
in_array('admin_user', $privileges),
|
|
|
|
User_is_freeloader($user_source),
|
2018-10-10 03:10:28 +02:00
|
|
|
User_angeltypes($user_source->id),
|
|
|
|
User_groups($user_source->id),
|
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,
|
2017-12-29 17:19:27 +01:00
|
|
|
in_array('admin_active', $privileges),
|
|
|
|
in_array('admin_user_worklog', $privileges),
|
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()
|
|
|
|
{
|
|
|
|
global $privileges;
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (!in_array('admin_user', $privileges)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to(''));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-17 01:30:10 +02:00
|
|
|
$order_by = 'name';
|
2018-10-11 01:26:34 +02:00
|
|
|
if ($request->has('OrderBy') && in_array($request->input('OrderBy'), [
|
2018-10-17 01:30:10 +02:00
|
|
|
'name',
|
|
|
|
'last_name',
|
|
|
|
'first_name',
|
|
|
|
'dect',
|
2018-10-11 01:26:34 +02:00
|
|
|
'email',
|
2018-10-17 01:30:10 +02:00
|
|
|
'shirt_size',
|
|
|
|
'arrived',
|
|
|
|
'active',
|
2018-10-11 01:26:34 +02:00
|
|
|
'force_active',
|
2018-10-17 01:30:10 +02:00
|
|
|
'got_shirt',
|
|
|
|
'last_login_at',
|
2018-10-11 01:26:34 +02:00
|
|
|
])) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$order_by = $request->input('OrderBy');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-11 01:26:34 +02:00
|
|
|
/** @var User[] $users */
|
|
|
|
$users = User::query()
|
2018-10-17 01:30:10 +02:00
|
|
|
->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')
|
2018-10-11 01:26:34 +02:00
|
|
|
->orderBy($order_by)
|
2018-10-17 01:30:10 +02:00
|
|
|
->orderBy('name')
|
2018-10-11 01:26:34 +02:00
|
|
|
->get();
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$user->setAttribute('freeloads', count(ShiftEntries_freeloaded_by_user($user->id)));
|
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-08-29 21:55:32 +02:00
|
|
|
__('All users'),
|
2017-01-02 15:43:36 +01:00
|
|
|
Users_view(
|
|
|
|
$users,
|
|
|
|
$order_by,
|
2018-10-11 01:26:34 +02:00
|
|
|
State::whereArrived(true)->count(),
|
|
|
|
State::whereActive(true)->count(),
|
|
|
|
State::whereForceActive(true)->count(),
|
2017-12-25 23:12:52 +01:00
|
|
|
ShiftEntries_freeloaded_count(),
|
2018-10-11 01:26:34 +02:00
|
|
|
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-11-15 22:08:41 +01:00
|
|
|
* Second step of password recovery: set a new password using the token link from email
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2013-12-26 13:34:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_password_recovery_set_new_controller()
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2018-10-11 01:26:34 +02:00
|
|
|
$passwordReset = PasswordReset::whereToken($request->input('token'))->first();
|
2018-10-09 21:47:31 +02:00
|
|
|
if (!$passwordReset) {
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Token is not correct.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to('login'));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('submit')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = true;
|
2017-01-01 18:49:43 +01:00
|
|
|
|
2017-01-21 23:07:20 +01:00
|
|
|
if (
|
2017-07-18 21:38:53 +02:00
|
|
|
$request->has('password')
|
2017-08-29 16:21:25 +02:00
|
|
|
&& strlen($request->postData('password')) >= config('min_password_length')
|
2017-01-21 23:07:20 +01:00
|
|
|
) {
|
2017-08-29 16:21:25 +02:00
|
|
|
if ($request->postData('password') != $request->postData('password2')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Your passwords don\'t match.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Your password is to short (please use at least 6 characters).'));
|
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-09 21:47:31 +02:00
|
|
|
set_password($passwordReset->user->id, $request->postData('password'));
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Password saved.'));
|
2018-10-09 21:47:31 +02:00
|
|
|
$passwordReset->delete();
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to('login'));
|
|
|
|
}
|
2016-11-15 22:08:41 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return User_password_set_view();
|
2016-11-15 22:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* First step of password recovery: display a form that asks for your email and send email with recovery link
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2016-11-15 22:08:41 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_password_recovery_start_controller()
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
|
|
|
if ($request->has('submit')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = true;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-11 01:26:34 +02:00
|
|
|
$user_source = null;
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('email') && strlen(strip_request_item('email')) > 0) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$email = strip_request_item('email');
|
|
|
|
if (check_email($email)) {
|
2018-10-11 01:26:34 +02:00
|
|
|
/** @var User $user_source */
|
|
|
|
$user_source = User::whereEmail($email)->first();
|
|
|
|
if (!$user_source) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('E-mail address is not correct.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('E-mail address is not correct.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Please enter your e-mail.'));
|
2013-12-26 13:34:48 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($valid) {
|
|
|
|
$token = User_generate_password_recovery_token($user_source);
|
2017-01-02 15:43:36 +01:00
|
|
|
engelsystem_email_to_user(
|
|
|
|
$user_source,
|
2018-08-29 21:55:32 +02:00
|
|
|
__('Password recovery'),
|
2017-01-02 15:43:36 +01:00
|
|
|
sprintf(
|
2018-08-29 21:55:32 +02:00
|
|
|
__('Please visit %s to recover your password.'),
|
2017-08-29 16:21:25 +02:00
|
|
|
page_link_to('user_password_recovery', ['token' => $token])
|
2017-01-02 15:43:36 +01:00
|
|
|
)
|
|
|
|
);
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('We sent an email containing your password recovery link.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to('login'));
|
|
|
|
}
|
2016-11-15 22:08:41 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return User_password_recovery_view();
|
2016-11-15 22:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User password recovery in 2 steps.
|
|
|
|
* (By email)
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2016-11-15 22:08:41 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_password_recovery_controller()
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
if (request()->has('token')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return user_password_recovery_set_new_controller();
|
|
|
|
}
|
2017-01-03 03:22:48 +01:00
|
|
|
|
|
|
|
return user_password_recovery_start_controller();
|
2013-12-26 13:34:48 +01:00
|
|
|
}
|
|
|
|
|
2014-01-07 16:19:35 +01:00
|
|
|
/**
|
|
|
|
* Menu title for password recovery.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2014-01-07 16:19:35 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_password_recovery_title()
|
|
|
|
{
|
2018-08-29 21:55:32 +02:00
|
|
|
return __('Password recovery');
|
2013-12-26 13:34:48 +01: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()
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
|
|
|
if (!$request->has('user_id')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to());
|
|
|
|
}
|
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) {
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('User doesn\'t exist.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to());
|
|
|
|
}
|
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 = [];
|
|
|
|
$shift_entries = [];
|
|
|
|
foreach ($shifts as $shift) {
|
|
|
|
$needed_angeltypes[$shift['SID']] = [];
|
|
|
|
$shift_entries[$shift['SID']] = [];
|
2016-12-29 14:51:43 +01:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($shift_entries_source as $shift_entry) {
|
|
|
|
if (isset($shift_entries[$shift_entry['SID']])) {
|
|
|
|
$shift_entries[$shift_entry['SID']][] = $shift_entry;
|
|
|
|
}
|
2016-12-29 14:51:43 +01:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($needed_angeltypes_source as $needed_angeltype) {
|
|
|
|
if (isset($needed_angeltypes[$needed_angeltype['SID']])) {
|
|
|
|
$needed_angeltypes[$needed_angeltype['SID']][] = $needed_angeltype;
|
2016-12-29 15:33:21 +01:00
|
|
|
}
|
2016-12-29 14:51:43 +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;
|
2017-01-03 03:22:48 +01:00
|
|
|
$taken = 0;
|
2017-01-02 03:57:23 +01:00
|
|
|
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) {
|
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);
|
|
|
|
}
|
|
|
|
if (in_array(ShiftsFilter::FILLED_FREE, $shiftsFilter->getFilled()) && $taken < $needed_angels_count) {
|
|
|
|
$filtered_shifts[] = $shift;
|
|
|
|
}
|
2017-12-25 23:12:52 +01:00
|
|
|
if (
|
|
|
|
in_array(ShiftsFilter::FILLED_FILLED, $shiftsFilter->getFilled())
|
|
|
|
&& $taken >= $needed_angels_count
|
|
|
|
) {
|
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
|
|
|
}
|