From 2dcb7cc2de448c664bbc9a7112f9cb13dc15c516 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 8 Oct 2018 21:15:56 +0200 Subject: [PATCH] Replaced some global `$user` variables --- .../controller/shift_entries_controller.php | 4 ++-- .../user_driver_licenses_controller.php | 9 +++---- includes/controller/users_controller.php | 4 ++-- includes/helper/email_helper.php | 4 ++-- includes/model/Message_model.php | 8 +++---- includes/model/Shifts_model.php | 4 ++-- includes/model/UserWorkLog_model.php | 4 ++-- includes/pages/admin_import.php | 5 ++-- includes/pages/admin_news.php | 5 ++-- includes/pages/admin_questions.php | 4 ++-- includes/pages/admin_user.php | 15 ++++++------ includes/pages/guest_login.php | 7 +++--- includes/pages/user_messages.php | 6 ++--- includes/pages/user_news.php | 10 ++++---- includes/pages/user_questions.php | 10 ++++---- includes/pages/user_shifts.php | 10 ++++---- includes/sys_menu.php | 4 ++-- includes/view/Rooms_view.php | 4 ++-- src/helpers.php | 17 +++++++++---- 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() . '

' . "\n"; $html .= ' Name' . '' . "\n"; $html .= ' Vorname' . '' . "\n"; - $html .= ' Alter' . '' . "\n"; + $html .= ' Alter' . '' . "\n"; $html .= ' Telefon' . '' . "\n"; $html .= ' Handy' . '' . "\n"; $html .= ' DECT' . '' . "\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:
' . "\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 ' ' . $new_messages . ''; 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 = '

' . user_news_comments_title() . '

'; @@ -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) . '

' . sprintf( __('Export your own shifts. iCal format or JSON format available (please keep secret, otherwise reset the api key).'), - 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]) ) . '

'; } 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; @@ -32,6 +33,27 @@ class HelpersTest extends TestCase $this->assertEquals($class, app('some.name')); } + /** + * @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() */