diff options
-rw-r--r-- | includes/controller/shift_entries_controller.php | 4 | ||||
-rw-r--r-- | includes/controller/user_driver_licenses_controller.php | 9 | ||||
-rw-r--r-- | includes/controller/users_controller.php | 4 | ||||
-rw-r--r-- | includes/helper/email_helper.php | 4 | ||||
-rw-r--r-- | includes/model/Message_model.php | 8 | ||||
-rw-r--r-- | includes/model/Shifts_model.php | 4 | ||||
-rw-r--r-- | includes/model/UserWorkLog_model.php | 4 | ||||
-rw-r--r-- | includes/pages/admin_import.php | 5 | ||||
-rw-r--r-- | includes/pages/admin_news.php | 5 | ||||
-rw-r--r-- | includes/pages/admin_questions.php | 4 | ||||
-rw-r--r-- | includes/pages/admin_user.php | 15 | ||||
-rw-r--r-- | includes/pages/guest_login.php | 7 | ||||
-rw-r--r-- | includes/pages/user_messages.php | 6 | ||||
-rw-r--r-- | includes/pages/user_news.php | 10 | ||||
-rw-r--r-- | includes/pages/user_questions.php | 10 | ||||
-rw-r--r-- | includes/pages/user_shifts.php | 10 | ||||
-rw-r--r-- | includes/sys_menu.php | 4 | ||||
-rw-r--r-- | includes/view/Rooms_view.php | 4 | ||||
-rw-r--r-- | src/helpers.php | 17 | ||||
-rw-r--r-- | tests/Unit/HelpersTest.php | 24 |
20 files changed, 97 insertions, 61 deletions
diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 05a3a5e5..2c57f550 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -336,7 +336,7 @@ function shift_entry_load() */ function shift_entry_delete_controller() { - global $user; + $user = Auth()->user(); $request = request(); $shiftEntry = shift_entry_load(); @@ -354,7 +354,7 @@ function shift_entry_delete_controller() redirect(shift_link($shift)); } - if ($user['UID'] == $signout_user['UID']) { + if ($user->id == $signout_user['UID']) { return [ ShiftEntry_delete_title(), ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signout_user) diff --git a/includes/controller/user_driver_licenses_controller.php b/includes/controller/user_driver_licenses_controller.php index 6621866d..422ad382 100644 --- a/includes/controller/user_driver_licenses_controller.php +++ b/includes/controller/user_driver_licenses_controller.php @@ -37,9 +37,9 @@ function user_driver_license_required_hint() */ function user_driver_licenses_controller() { - global $user; + $user = Auth()->user(); - if (!isset($user)) { + if (!$user) { redirect(page_link_to('')); } @@ -94,12 +94,13 @@ function user_driver_license_load_user() */ function user_driver_license_edit_controller() { - global $privileges, $user; + global $privileges; + $user = Auth()->user(); $request = request(); $user_source = user_driver_license_load_user(); // only privilege admin_user can edit other users driver license information - if ($user['UID'] != $user_source['UID'] && !in_array('admin_user', $privileges)) { + if ($user->id != $user_source['UID'] && !in_array('admin_user', $privileges)) { redirect(user_driver_license_edit_link()); } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index d7f97c7e..13264fd1 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -11,10 +11,10 @@ use Engelsystem\ShiftsFilter; */ function users_controller() { - global $user; + $user = Auth()->user(); $request = request(); - if (!isset($user)) { + if (!$user) { redirect(page_link_to('')); } diff --git a/includes/helper/email_helper.php b/includes/helper/email_helper.php index 7cd60933..8668cab0 100644 --- a/includes/helper/email_helper.php +++ b/includes/helper/email_helper.php @@ -11,9 +11,9 @@ use Engelsystem\Mail\EngelsystemMailer; */ function engelsystem_email_to_user($recipient_user, $title, $message, $not_if_its_me = false) { - global $user; + $user = Auth()->user(); - if ($not_if_its_me && $user['UID'] == $recipient_user['UID']) { + if ($not_if_its_me && $user->id == $recipient_user['UID']) { return true; } diff --git a/includes/model/Message_model.php b/includes/model/Message_model.php index 3f66f74a..4f8a3972 100644 --- a/includes/model/Message_model.php +++ b/includes/model/Message_model.php @@ -27,7 +27,7 @@ function Message($message_id) /** * TODO: use validation functions, return new message id - * TODO: global $user con not be used in model! + * TODO: global $user can't be used in model! * send message * * @param int $receiver_user_id User ID of Receiver @@ -36,7 +36,7 @@ function Message($message_id) */ function Message_send($receiver_user_id, $text) { - global $user; + $user = Auth()->user(); $text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($text)); $receiver_user_id = preg_replace('/([^\d]{1,})/ui', '', strip_tags($receiver_user_id)); @@ -49,7 +49,7 @@ function Message_send($receiver_user_id, $text) WHERE `UID` = ? AND NOT `UID` = ? LIMIT 1 - ', [$receiver_user_id, $user['UID']])) > 0 + ', [$receiver_user_id, $user->id])) > 0 ) { return DB::insert(' INSERT INTO `Messages` (`Datum`, `SUID`, `RUID`, `Text`) @@ -57,7 +57,7 @@ function Message_send($receiver_user_id, $text) ', [ time(), - $user['UID'], + $user->id, $receiver_user_id, $text ] diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 74facc0a..30ce5a97 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -518,7 +518,7 @@ function Shift_delete($shift_id) */ function Shift_update($shift) { - global $user; + $user = Auth()->user(); $shift['name'] = ShiftType($shift['shifttype_id'])['name']; mail_shift_change(Shift($shift['SID']), $shift); @@ -543,7 +543,7 @@ function Shift_update($shift) $shift['title'], $shift['URL'], $shift['PSID'], - $user['UID'], + $user->id, time(), $shift['SID'] ] diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php index dd4b2574..46ab891e 100644 --- a/includes/model/UserWorkLog_model.php +++ b/includes/model/UserWorkLog_model.php @@ -93,7 +93,7 @@ function UserWorkLog_update($userWorkLog) */ function UserWorkLog_create($userWorkLog) { - global $user; + $user = Auth()->user(); $user_source = User($userWorkLog['user_id']); @@ -110,7 +110,7 @@ function UserWorkLog_create($userWorkLog) $userWorkLog['work_timestamp'], $userWorkLog['work_hours'], $userWorkLog['comment'], - $user['UID'], + $user->id, time() ]); diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index 29aa719b..f6889320 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -13,7 +13,8 @@ function admin_import_title() */ function admin_import() { - global $rooms_import, $user; + global $rooms_import; + $user = Auth()->user(); $html = ''; $import_dir = __DIR__ . '/../../import'; $request = request(); @@ -38,7 +39,7 @@ function admin_import() error(__('Webserver has no write-permission on import directory.')); } - $import_file = $import_dir . '/import_' . $user['UID'] . '.xml'; + $import_file = $import_dir . '/import_' . $user->id . '.xml'; $shifttype_id = null; $add_minutes_start = 15; $add_minutes_end = 15; diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php index 8a3fe9c2..d387e06b 100644 --- a/includes/pages/admin_news.php +++ b/includes/pages/admin_news.php @@ -7,7 +7,8 @@ use Engelsystem\Database\DB; */ function admin_news() { - global $user, $privileges; + global $privileges; + $user = Auth()->user(); $request = request(); if (!$request->has('action')) { @@ -77,7 +78,7 @@ function admin_news() time(), strip_tags($request->postData('eBetreff')), $text, - $user['UID'], + $user->id, $request->has('eTreffen') ? 1 : 0, $news_id ] diff --git a/includes/pages/admin_questions.php b/includes/pages/admin_questions.php index b4a956d5..fc84ff9e 100644 --- a/includes/pages/admin_questions.php +++ b/includes/pages/admin_questions.php @@ -39,7 +39,7 @@ function admin_new_questions() */ function admin_questions() { - global $user; + $user = Auth()->user(); $request = request(); if (!$request->has('action')) { @@ -126,7 +126,7 @@ function admin_questions() LIMIT 1 ', [ - $user['UID'], + $user->id, $answer, $question_id, ] diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index a0125604..2ef48eb7 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -15,7 +15,8 @@ function admin_user_title() */ function admin_user() { - global $user, $privileges; + global $privileges; + $user = Auth()->user(); $tshirt_sizes = config('tshirt_sizes'); $request = request(); $html = ''; @@ -52,7 +53,7 @@ function admin_user() . '</p></td></tr>' . "\n"; $html .= ' <tr><td>Name</td><td>' . '<input size="40" name="eName" value="' . $user_source['Name'] . '" class="form-control"></td></tr>' . "\n"; $html .= ' <tr><td>Vorname</td><td>' . '<input size="40" name="eVorname" value="' . $user_source['Vorname'] . '" class="form-control"></td></tr>' . "\n"; - $html .= ' <tr><td>Alter</td><td>' . '<input type="value" size="5" name="eAlter" value="' . $user_source['Alter'] . '" class="form-control"></td></tr>' . "\n"; + $html .= ' <tr><td>Alter</td><td>' . '<input size="5" name="eAlter" value="' . $user_source['Alter'] . '" class="form-control"></td></tr>' . "\n"; $html .= ' <tr><td>Telefon</td><td>' . '<input type="tel" size="40" name="eTelefon" value="' . $user_source['Telefon'] . '" class="form-control"></td></tr>' . "\n"; $html .= ' <tr><td>Handy</td><td>' . '<input type= "tel" size="40" name="eHandy" value="' . $user_source['Handy'] . '" class="form-control"></td></tr>' . "\n"; $html .= ' <tr><td>DECT</td><td>' . '<input size="4" name="eDECT" value="' . $user_source['DECT'] . '" class="form-control"></td></tr>' . "\n"; @@ -114,7 +115,7 @@ function admin_user() $my_highest_group = DB::selectOne( 'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', - [$user['UID']] + [$user->id] ); if (!empty($my_highest_group)) { $my_highest_group = $my_highest_group['group_id']; @@ -128,7 +129,7 @@ function admin_user() $his_highest_group = $his_highest_group['group_id']; } - if ($user_id != $user['UID'] && $my_highest_group <= $his_highest_group) { + if ($user_id != $user->id && $my_highest_group <= $his_highest_group) { $html .= 'Hier kannst Du die Benutzergruppen des Engels festlegen:<form action="' . page_link_to('admin_user', ['action' => 'save_groups', 'id' => $user_id]) . '" method="post">' . "\n"; @@ -171,10 +172,10 @@ function admin_user() } else { switch ($request->input('action')) { case 'save_groups': - if ($user_id != $user['UID']) { + if ($user_id != $user->id) { $my_highest_group = DB::selectOne( 'SELECT * FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id`', - [$user['UID']] + [$user->id] ); $his_highest_group = DB::selectOne( 'SELECT * FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id`', @@ -241,7 +242,7 @@ function admin_user() break; case 'save': - $force_active = $user['force_active']; + $force_active = $user->state->force_active; $user_source = User($user_id); if (in_array('admin_active', $privileges)) { $force_active = $request->input('force_active'); diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index 95e63bbc..1bb39d01 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -34,7 +34,8 @@ function logout_title() */ function guest_register() { - global $user, $privileges; + global $privileges; + $user = Auth()->user(); $tshirt_sizes = config('tshirt_sizes'); $enable_tshirt_size = config('enable_tshirt_size'); $min_password_length = config('min_password_length'); @@ -70,7 +71,7 @@ function guest_register() } } - if (!in_array('register', $privileges) || (!isset($user) && !config('registration_enabled'))) { + if (!in_array('register', $privileges) || (!$user && !config('registration_enabled'))) { error(__('Registration is disabled.')); return page_with_title(register_title(), [ @@ -269,7 +270,7 @@ function guest_register() success(__('Angel registration successful!')); // User is already logged in - that means a supporter has registered an angel. Return to register page. - if (isset($user)) { + if ($user) { redirect(page_link_to('register')); } diff --git a/includes/pages/user_messages.php b/includes/pages/user_messages.php index 0738f433..d2034dc0 100644 --- a/includes/pages/user_messages.php +++ b/includes/pages/user_messages.php @@ -15,12 +15,12 @@ function messages_title() */ function user_unread_messages() { - global $user; + $user = Auth()->user(); - if (isset($user)) { + if ($user) { $new_messages = count(DB::select( 'SELECT `id` FROM `Messages` WHERE isRead=\'N\' AND `RUID`=?', - [$user['UID']] + [$user->id] )); if ($new_messages > 0) { return ' <span class="badge danger">' . $new_messages . '</span>'; diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php index f95b6ae8..ac1f8b80 100644 --- a/includes/pages/user_news.php +++ b/includes/pages/user_news.php @@ -132,8 +132,7 @@ function display_news($news) */ function user_news_comments() { - global $user; - + $user = Auth()->user(); $request = request(); $html = '<div class="col-md-12"><h1>' . user_news_comments_title() . '</h1>'; @@ -158,7 +157,7 @@ function user_news_comments() $nid, date('Y-m-d H:i:s'), $text, - $user["UID"], + $user->id, ] ); engelsystem_log('Created news_comment: ' . $text); @@ -200,7 +199,8 @@ function user_news_comments() */ function user_news() { - global $privileges, $user; + global $privileges; + $user = Auth()->user(); $display_news = config('display_news'); $request = request(); @@ -225,7 +225,7 @@ function user_news() time(), strip_tags($request->postData('betreff')), $text, - $user['UID'], + $user->id, $isMeeting, ] ); diff --git a/includes/pages/user_questions.php b/includes/pages/user_questions.php index ea255a3d..94922141 100644 --- a/includes/pages/user_questions.php +++ b/includes/pages/user_questions.php @@ -15,18 +15,18 @@ function questions_title() */ function user_questions() { - global $user; + $user = Auth()->user(); $request = request(); if (!$request->has('action')) { $open_questions = DB::select( 'SELECT * FROM `Questions` WHERE `AID` IS NULL AND `UID`=?', - [$user['UID']] + [$user->id] ); $answered_questions = DB::select( 'SELECT * FROM `Questions` WHERE NOT `AID` IS NULL AND `UID`=?', - [$user['UID']] + [$user->id] ); foreach ($answered_questions as &$question) { $answer_user_source = User($question['AID']); @@ -47,7 +47,7 @@ function user_questions() INSERT INTO `Questions` (`UID`, `Question`) VALUES (?, ?) ', - [$user['UID'], $question] + [$user->id, $question] ); success(__('You question was saved.')); @@ -69,7 +69,7 @@ function user_questions() 'SELECT `UID` FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id] ); - if (!empty($question) && $question['UID'] == $user['UID']) { + if (!empty($question) && $question['UID'] == $user->id) { DB::delete( 'DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id] diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 6754292e..dc33d078 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -129,7 +129,7 @@ function load_days() */ function load_types() { - global $user; + $user = Auth()->user(); if (!count(DB::select('SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0'))) { error(__('The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype.')); @@ -155,7 +155,7 @@ function load_types() ORDER BY `AngelTypes`.`name` ', [ - $user['UID'], + $user->id, ] ); if (empty($types)) { @@ -284,13 +284,13 @@ function view_user_shifts() */ function ical_hint() { - global $user; + $user = Auth()->user(); return heading(__('iCal export'), 2) . '<p>' . sprintf( __('Export your own shifts. <a href="%s">iCal format</a> or <a href="%s">JSON format</a> available (please keep secret, otherwise <a href="%s">reset the api key</a>).'), - page_link_to('ical', ['key' => $user['api_key']]), - page_link_to('shifts_json_export', ['key' => $user['api_key']]), + page_link_to('ical', ['key' => $user->api_key]), + page_link_to('shifts_json_export', ['key' => $user->api_key]), page_link_to('user_myshifts', ['reset' => 1]) ) . '</p>'; } diff --git a/includes/sys_menu.php b/includes/sys_menu.php index ca2ab411..dfc5c424 100644 --- a/includes/sys_menu.php +++ b/includes/sys_menu.php @@ -20,11 +20,11 @@ function page_link_to($page = '', $parameters = []) */ function header_render_hints() { - global $user; + $user = Auth()->user(); $hints_renderer = new UserHintsRenderer(); - if (isset($user)) { + if ($user) { $hints_renderer->addHint(admin_new_questions()); $hints_renderer->addHint(user_angeltypes_unconfirmed_hint()); $hints_renderer->addHint(render_user_departure_date_hint()); diff --git a/includes/view/Rooms_view.php b/includes/view/Rooms_view.php index d883e4b2..0a2286fb 100644 --- a/includes/view/Rooms_view.php +++ b/includes/view/Rooms_view.php @@ -12,10 +12,10 @@ use Engelsystem\ShiftsFilterRenderer; */ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalendarRenderer $shiftCalendarRenderer) { - global $user; + $user = Auth()->user(); $assignNotice = ''; - if (config('signup_requires_arrival') && !$user['Gekommen']) { + if (config('signup_requires_arrival') && !$user->state->arrived) { $assignNotice = info(render_user_arrived_hint(), true); } diff --git a/src/helpers.php b/src/helpers.php index 64ca9ec8..111141e4 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -3,10 +3,11 @@ use Engelsystem\Application; use Engelsystem\Config\Config; +use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Translator; use Engelsystem\Http\Request; use Engelsystem\Http\Response; -use Engelsystem\Http\UrlGenerator; +use Engelsystem\Http\UrlGeneratorInterface; use Engelsystem\Renderer\Renderer; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -16,13 +17,21 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface; * @param string $id * @return mixed|Application */ -function app($instance_id = null) +function app($id = null) { - if (is_null($instance_id)) { + if (is_null($id)) { return Application::getInstance(); } - return Application::getInstance()->get($instance_id); + return Application::getInstance()->get($id); +} + +/** + * @return Authenticator + */ +function auth() +{ + return app('authenticator'); } /** diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index b36abc64..e0184b86 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -5,11 +5,12 @@ namespace Engelsystem\Test\Unit; use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Container\Container; +use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Translator; use Engelsystem\Http\Request; use Engelsystem\Http\Response; -use Engelsystem\Renderer\Renderer; use Engelsystem\Http\UrlGeneratorInterface; +use Engelsystem\Renderer\Renderer; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; @@ -33,6 +34,27 @@ class HelpersTest extends TestCase } /** + * @covers \auth + */ + public function testAuth() + { + /** @var Application|MockObject $app */ + $app = $this->createMock(Container::class); + Application::setInstance($app); + /** @var Authenticator|MockObject $auth */ + $auth = $this->getMockBuilder(Authenticator::class) + ->disableOriginalConstructor() + ->getMock(); + + $app->expects($this->once()) + ->method('get') + ->with('authenticator') + ->willReturn($auth); + + $this->assertEquals($auth, auth()); + } + + /** * @covers \base_path() */ public function testBasePath() |