Replaced some global `$user` variables

This commit is contained in:
Igor Scheller 2018-10-08 21:15:56 +02:00 committed by msquare
parent d15946df2d
commit 2dcb7cc2de
20 changed files with 97 additions and 61 deletions

View File

@ -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)

View File

@ -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());
}

View File

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

View File

@ -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;
}

View File

@ -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
]

View File

@ -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']
]

View File

@ -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()
]);

View File

@ -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;

View File

@ -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
]

View File

@ -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,
]

View File

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

View File

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

View File

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

View File

@ -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,
]
);

View File

@ -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]

View File

@ -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>';
}

View File

@ -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());

View File

@ -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);
}

View File

@ -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');
}
/**

View File

@ -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()
*/