diff --git a/config/routes.php b/config/routes.php index 3b388de6..fc0a51ee 100644 --- a/config/routes.php +++ b/config/routes.php @@ -15,53 +15,83 @@ $route->post('/login', 'AuthController@postLogin'); $route->get('/logout', 'AuthController@logout'); // OAuth -$route->get('/oauth/{provider}', 'OAuthController@index'); -$route->post('/oauth/{provider}/connect', 'OAuthController@connect'); -$route->post('/oauth/{provider}/disconnect', 'OAuthController@disconnect'); +$route->addGroup( + '/oauth/{provider}', + function (RouteCollector $route) { + $route->get('', 'OAuthController@index'); + $route->post('/connect', 'OAuthController@connect'); + $route->post('/disconnect', 'OAuthController@disconnect'); + } +); // User settings -$route->get('/settings/profile', 'SettingsController@profile'); -$route->post('/settings/profile', 'SettingsController@saveProfile'); -$route->get('/settings/password', 'SettingsController@password'); -$route->post('/settings/password', 'SettingsController@savePassword'); -$route->get('/settings/theme', 'SettingsController@theme'); -$route->post('/settings/theme', 'SettingsController@saveTheme'); -$route->get('/settings/language', 'SettingsController@language'); -$route->post('/settings/language', 'SettingsController@saveLanguage'); -$route->get('/settings/oauth', 'SettingsController@oauth'); +$route->addGroup( + '/settings', + function (RouteCollector $route) { + $route->get('/profile', 'SettingsController@profile'); + $route->post('/profile', 'SettingsController@saveProfile'); + $route->get('/password', 'SettingsController@password'); + $route->post('/password', 'SettingsController@savePassword'); + $route->get('/theme', 'SettingsController@theme'); + $route->post('/theme', 'SettingsController@saveTheme'); + $route->get('/language', 'SettingsController@language'); + $route->post('/language', 'SettingsController@saveLanguage'); + $route->get('/oauth', 'SettingsController@oauth'); + } +); // Password recovery -$route->get('/password/reset', 'PasswordResetController@reset'); -$route->post('/password/reset', 'PasswordResetController@postReset'); -$route->get('/password/reset/{token:.+}', 'PasswordResetController@resetPassword'); -$route->post('/password/reset/{token:.+}', 'PasswordResetController@postResetPassword'); +$route->addGroup( + '/password/reset', + function (RouteCollector $route) { + $route->get('', 'PasswordResetController@reset'); + $route->post('', 'PasswordResetController@postReset'); + $route->get('/{token:.+}', 'PasswordResetController@resetPassword'); + $route->post('/{token:.+}', 'PasswordResetController@postResetPassword'); + } +); // Stats $route->get('/metrics', 'Metrics\\Controller@metrics'); $route->get('/stats', 'Metrics\\Controller@stats'); // News -$route->get('/news', 'NewsController@index'); $route->get('/meetings', 'NewsController@meetings'); -$route->get('/news/{id:\d+}', 'NewsController@show'); -$route->post('/news/{id:\d+}', 'NewsController@comment'); -$route->post('/news/comment/{id:\d+}', 'NewsController@deleteComment'); +$route->addGroup( + '/news', + function (RouteCollector $route) { + $route->get('', 'NewsController@index'); + $route->get('/{news_id:\d+}', 'NewsController@show'); + $route->post('/{news_id:\d+}', 'NewsController@comment'); + $route->post('/comment/{comment_id:\d+}', 'NewsController@deleteComment'); + } +); // FAQ $route->get('/faq', 'FaqController@index'); // Questions -$route->get('/questions', 'QuestionsController@index'); -$route->post('/questions', 'QuestionsController@delete'); -$route->get('/questions/new', 'QuestionsController@add'); -$route->post('/questions/new', 'QuestionsController@save'); +$route->addGroup( + '/questions', + function (RouteCollector $route) { + $route->get('', 'QuestionsController@index'); + $route->post('', 'QuestionsController@delete'); + $route->get('/new', 'QuestionsController@add'); + $route->post('/new', 'QuestionsController@save'); + } +); // Messages -$route->get('/messages', 'MessagesController@index'); -$route->post('/messages', 'MessagesController@redirectToConversation'); -$route->get('/messages/{user_id:\d+}', 'MessagesController@messagesOfConversation'); -$route->post('/messages/{user_id:\d+}', 'MessagesController@send'); -$route->post('/messages/{user_id:\d+}/{msg_id:\d+}', 'MessagesController@delete'); +$route->addGroup( + '/messages', + function (RouteCollector $route) { + $route->get('', 'MessagesController@index'); + $route->post('', 'MessagesController@redirectToConversation'); + $route->get('/{user_id:\d+}', 'MessagesController@messagesOfConversation'); + $route->post('/{user_id:\d+}', 'MessagesController@send'); + $route->post('/{user_id:\d+}/{msg_id:\d+}', 'MessagesController@delete'); + } +); // API $route->get('/api[/{resource:.+}]', 'ApiController@index'); @@ -77,24 +107,29 @@ $route->addGroup( $route->addGroup( '/faq', function (RouteCollector $route) { - $route->get('[/{id:\d+}]', 'Admin\\FaqController@edit'); - $route->post('[/{id:\d+}]', 'Admin\\FaqController@save'); + $route->get('[/{faq_id:\d+}]', 'Admin\\FaqController@edit'); + $route->post('[/{faq_id:\d+}]', 'Admin\\FaqController@save'); } ); // Log - $route->get('/logs', 'Admin\\LogsController@index'); - $route->post('/logs', 'Admin\\LogsController@index'); + $route->addGroup( + '/logs', + function (RouteCollector $route) { + $route->get('', 'Admin\\LogsController@index'); + $route->post('', 'Admin\\LogsController@index'); + } + ); // Schedule $route->addGroup( '/schedule', function (RouteCollector $route) { $route->get('', 'Admin\\Schedule\\ImportSchedule@index'); - $route->get('/edit[/{id:\d+}]', 'Admin\\Schedule\\ImportSchedule@edit'); - $route->post('/edit[/{id:\d+}]', 'Admin\\Schedule\\ImportSchedule@save'); - $route->get('/load/{id:\d+}', 'Admin\\Schedule\\ImportSchedule@loadSchedule'); - $route->post('/import/{id:\d+}', 'Admin\\Schedule\\ImportSchedule@importSchedule'); + $route->get('/edit[/{schedule_id:\d+}]', 'Admin\\Schedule\\ImportSchedule@edit'); + $route->post('/edit[/{schedule_id:\d+}]', 'Admin\\Schedule\\ImportSchedule@save'); + $route->get('/load/{schedule_id:\d+}', 'Admin\\Schedule\\ImportSchedule@loadSchedule'); + $route->post('/import/{schedule_id:\d+}', 'Admin\\Schedule\\ImportSchedule@importSchedule'); } ); @@ -104,31 +139,39 @@ $route->addGroup( function (RouteCollector $route) { $route->get('', 'Admin\\QuestionsController@index'); $route->post('', 'Admin\\QuestionsController@delete'); - $route->get('/{id:\d+}', 'Admin\\QuestionsController@edit'); - $route->post('/{id:\d+}', 'Admin\\QuestionsController@save'); + $route->get('/{question_id:\d+}', 'Admin\\QuestionsController@edit'); + $route->post('/{question_id:\d+}', 'Admin\\QuestionsController@save'); } ); // User $route->addGroup( - '/user/{id:\d+}', + '/user/{user_id:\d+}', function (RouteCollector $route) { // Shirts - $route->get('/shirt', 'Admin\\UserShirtController@editShirt'); - $route->post('/shirt', 'Admin\\UserShirtController@saveShirt'); + $route->addGroup( + '/shirt', + function (RouteCollector $route) { + $route->get('', 'Admin\\UserShirtController@editShirt'); + $route->post('', 'Admin\\UserShirtController@saveShirt'); + } + ); // Worklogs - $route->get('/worklog', 'Admin\\UserWorkLogController@editWorklog'); - $route->post('/worklog', 'Admin\\UserWorkLogController@saveWorklog'); - $route->get('/worklog/{worklog_id:\d+}', 'Admin\\UserWorkLogController@editWorklog'); - $route->post('/worklog/{worklog_id:\d+}', 'Admin\\UserWorkLogController@saveWorklog'); - $route->get( - '/worklog/{worklog_id:\d+}/delete', - 'Admin\\UserWorkLogController@showDeleteWorklog' - ); - $route->post( - '/worklog/{worklog_id:\d+}/delete', - 'Admin\\UserWorkLogController@deleteWorklog' + $route->addGroup( + '/worklog', + function (RouteCollector $route) { + $route->get('[/{worklog_id:\d+}]', 'Admin\\UserWorkLogController@editWorklog'); + $route->post('[/{worklog_id:\d+}]', 'Admin\\UserWorkLogController@saveWorklog'); + $route->get( + '/{worklog_id:\d+}/delete', + 'Admin\\UserWorkLogController@showDeleteWorklog' + ); + $route->post( + '/{worklog_id:\d+}/delete', + 'Admin\\UserWorkLogController@deleteWorklog' + ); + } ); } ); @@ -137,8 +180,8 @@ $route->addGroup( $route->addGroup( '/news', function (RouteCollector $route) { - $route->get('[/{id:\d+}]', 'Admin\\NewsController@edit'); - $route->post('[/{id:\d+}]', 'Admin\\NewsController@save'); + $route->get('[/{news_id:\d+}]', 'Admin\\NewsController@edit'); + $route->post('[/{news_id:\d+}]', 'Admin\\NewsController@save'); } ); } diff --git a/includes/pages/schedule/ImportSchedule.php b/includes/pages/schedule/ImportSchedule.php index f43356c3..44feac37 100644 --- a/includes/pages/schedule/ImportSchedule.php +++ b/includes/pages/schedule/ImportSchedule.php @@ -104,7 +104,9 @@ class ImportSchedule extends BaseController */ public function edit(Request $request): Response { - $schedule = ScheduleUrl::find($request->getAttribute('id')); + $scheduleId = $request->getAttribute('schedule_id'); // optional + + $schedule = ScheduleUrl::find($scheduleId); return $this->response->withView( 'admin/schedule/edit.twig', @@ -122,9 +124,10 @@ class ImportSchedule extends BaseController */ public function save(Request $request): Response { - $id = $request->getAttribute('id'); + $scheduleId = $request->getAttribute('schedule_id'); // optional + /** @var ScheduleUrl $schedule */ - $schedule = ScheduleUrl::findOrNew($id); + $schedule = ScheduleUrl::findOrNew($scheduleId); $data = $this->validate($request, [ 'name' => 'required', @@ -437,9 +440,10 @@ class ImportSchedule extends BaseController */ protected function getScheduleData(Request $request) { - $id = $request->getAttribute('id'); + $scheduleId = (int)$request->getAttribute('schedule_id'); + /** @var ScheduleUrl $scheduleUrl */ - $scheduleUrl = ScheduleUrl::findOrFail($id); + $scheduleUrl = ScheduleUrl::findOrFail($scheduleId); $scheduleResponse = $this->guzzle->get($scheduleUrl->url); if ($scheduleResponse->getStatusCode() != 200) { diff --git a/src/Controllers/Admin/FaqController.php b/src/Controllers/Admin/FaqController.php index 56a5bad3..927ae24e 100644 --- a/src/Controllers/Admin/FaqController.php +++ b/src/Controllers/Admin/FaqController.php @@ -57,8 +57,9 @@ class FaqController extends BaseController */ public function edit(Request $request): Response { - $id = $request->getAttribute('id'); - $faq = $this->faq->find($id); + $faqId = $request->getAttribute('faq_id'); // optional + + $faq = $this->faq->find($faqId); return $this->showEdit($faq); } @@ -70,9 +71,10 @@ class FaqController extends BaseController */ public function save(Request $request): Response { - $id = $request->getAttribute('id'); + $faqId = $request->getAttribute('faq_id'); // optional + /** @var Faq $faq */ - $faq = $this->faq->findOrNew($id); + $faq = $this->faq->findOrNew($faqId); $data = $this->validate($request, [ 'question' => 'required', diff --git a/src/Controllers/Admin/NewsController.php b/src/Controllers/Admin/NewsController.php index 84139c4e..c7ed963e 100644 --- a/src/Controllers/Admin/NewsController.php +++ b/src/Controllers/Admin/NewsController.php @@ -63,8 +63,9 @@ class NewsController extends BaseController */ public function edit(Request $request): Response { - $id = $request->getAttribute('id'); - $news = $this->news->find($id); + $newsId = $request->getAttribute('news_id'); // optional + + $news = $this->news->find($newsId); $isMeeting = $request->get('meeting', false); return $this->showEdit($news, $isMeeting); @@ -95,9 +96,10 @@ class NewsController extends BaseController */ public function save(Request $request): Response { - $id = $request->getAttribute('id'); + $newsId = $request->getAttribute('news_id'); // optional + /** @var News $news */ - $news = $this->news->findOrNew($id); + $news = $this->news->findOrNew($newsId); $data = $this->validate($request, [ 'title' => 'required', diff --git a/src/Controllers/Admin/QuestionsController.php b/src/Controllers/Admin/QuestionsController.php index a767c79f..585a96b2 100644 --- a/src/Controllers/Admin/QuestionsController.php +++ b/src/Controllers/Admin/QuestionsController.php @@ -102,8 +102,9 @@ class QuestionsController extends BaseController */ public function edit(Request $request): Response { - $id = $request->getAttribute('id'); - $questions = $this->question->find($id); + $questionId = (int)$request->getAttribute('question_id'); + + $questions = $this->question->find($questionId); return $this->showEdit($questions); } @@ -115,9 +116,10 @@ class QuestionsController extends BaseController */ public function save(Request $request): Response { - $id = $request->getAttribute('id'); + $questionId = (int)$request->getAttribute('question_id'); + /** @var Question $question */ - $question = $this->question->findOrNew($id); + $question = $this->question->findOrNew($questionId); $data = $this->validate($request, [ 'text' => 'required', diff --git a/src/Controllers/Admin/UserShirtController.php b/src/Controllers/Admin/UserShirtController.php index 8e052fd4..302216cd 100644 --- a/src/Controllers/Admin/UserShirtController.php +++ b/src/Controllers/Admin/UserShirtController.php @@ -71,8 +71,9 @@ class UserShirtController extends BaseController */ public function editShirt(Request $request): Response { - $id = $request->getAttribute('id'); - $user = $this->user->findOrFail($id); + $userId = (int)$request->getAttribute('user_id'); + + $user = $this->user->findOrFail($userId); return $this->response->withView( 'admin/user/edit-shirt.twig', @@ -87,9 +88,10 @@ class UserShirtController extends BaseController */ public function saveShirt(Request $request): Response { - $id = $request->getAttribute('id'); + $userId = (int)$request->getAttribute('user_id'); + /** @var User $user */ - $user = $this->user->findOrFail($id); + $user = $this->user->findOrFail($userId); $data = $this->validate($request, [ 'shirt_size' => 'required', diff --git a/src/Controllers/Admin/UserWorkLogController.php b/src/Controllers/Admin/UserWorkLogController.php index 588534b3..cee49546 100644 --- a/src/Controllers/Admin/UserWorkLogController.php +++ b/src/Controllers/Admin/UserWorkLogController.php @@ -78,14 +78,15 @@ class UserWorkLogController extends BaseController */ public function editWorklog(Request $request): Response { - $user_id = $request->getAttribute('id'); - $user = $this->user->findOrFail($user_id); + $userId = (int)$request->getAttribute('user_id'); + $worklogId = $request->getAttribute('worklog_id'); // optional - $worklog_id = $request->getAttribute('worklog_id'); - if (isset($worklog_id)) { - $worklog = $this->worklog->findOrFail($worklog_id); + $user = $this->user->findOrFail($userId); - if ($worklog->user->id != $user_id) { + if (isset($worklogId)) { + $worklog = $this->worklog->findOrFail((int)$worklogId); + + if ($worklog->user->id != $userId) { throw new HttpNotFound(); } return $this->showEditWorklog($user, $worklog->worked_at, $worklog->hours, $worklog->comment, true); @@ -100,8 +101,10 @@ class UserWorkLogController extends BaseController */ public function saveWorklog(Request $request): Response { - $user_id = $request->getAttribute('id'); - $user = $this->user->findOrFail($user_id); + $userId = (int)$request->getAttribute('user_id'); + $worklogId = $request->getAttribute('worklog_id'); // optional + + $user = $this->user->findOrFail($userId); $data = $this->validate($request, [ 'work_date' => 'required|date:Y-m-d', @@ -109,11 +112,10 @@ class UserWorkLogController extends BaseController 'comment' => 'required|max:200', ]); - $worklog_id = $request->getAttribute('worklog_id'); - if (isset($worklog_id)) { - $worklog = $this->worklog->findOrFail($worklog_id); + if (isset($worklogId)) { + $worklog = $this->worklog->findOrFail((int)$worklogId); - if ($worklog->user->id != $user_id) { + if ($worklog->user->id != $userId) { throw new HttpNotFound(); } } else { @@ -126,9 +128,9 @@ class UserWorkLogController extends BaseController $worklog->comment = $data['comment']; $worklog->save(); - $this->addNotification(isset($worklog_id) ? 'worklog.edit.success' : 'worklog.add.success'); + $this->addNotification(isset($worklogId) ? 'worklog.edit.success' : 'worklog.add.success'); - return $this->redirect->to('/users?action=view&user_id=' . $user_id); + return $this->redirect->to('/users?action=view&user_id=' . $userId); // TODO Once User_view.php gets removed, change this to withView + getNotifications } @@ -138,12 +140,13 @@ class UserWorkLogController extends BaseController */ public function showDeleteWorklog(Request $request): Response { - $user_id = $request->getAttribute('id'); - $user = $this->user->findOrFail($user_id); - $worklog_id = $request->getAttribute('worklog_id'); - $worklog = $this->worklog->findOrFail($worklog_id); + $userId = (int)$request->getAttribute('user_id'); + $worklogId = (int)$request->getAttribute('worklog_id'); - if ($worklog->user->id != $user_id) { + $user = $this->user->findOrFail($userId); + $worklog = $this->worklog->findOrFail($worklogId); + + if ($worklog->user->id != $userId) { throw new HttpNotFound(); } @@ -159,18 +162,19 @@ class UserWorkLogController extends BaseController */ public function deleteWorklog(Request $request): Response { - $user_id = $request->getAttribute('id'); - $worklog_id = $request->getAttribute('worklog_id'); - $worklog = $this->worklog->findOrFail($worklog_id); + $userId = (int)$request->getAttribute('user_id'); + $worklogId = (int)$request->getAttribute('worklog_id'); - if ($worklog->user->id != $user_id) { + $worklog = $this->worklog->findOrFail($worklogId); + + if ($worklog->user->id != $userId) { throw new HttpNotFound(); } $worklog->delete(); $this->addNotification('worklog.delete.success'); - return $this->redirect->to('/users?action=view&user_id=' . $user_id); + return $this->redirect->to('/users?action=view&user_id=' . $userId); // TODO Once User_view.php gets removed, change this to withView + getNotifications } diff --git a/src/Controllers/MessagesController.php b/src/Controllers/MessagesController.php index 3c6db84c..463e4dd3 100644 --- a/src/Controllers/MessagesController.php +++ b/src/Controllers/MessagesController.php @@ -130,8 +130,10 @@ class MessagesController extends BaseController */ public function messagesOfConversation(Request $request): Response { + $userId = (int)$request->getAttribute('user_id'); + $currentUser = $this->auth->user(); - $otherUser = $this->user->findOrFail($request->getAttribute('user_id')); + $otherUser = $this->user->findOrFail($userId); $messages = $this->message ->where(function ($query) use ($currentUser, $otherUser) { @@ -166,11 +168,13 @@ class MessagesController extends BaseController */ public function send(Request $request): Response { + $userId = (int)$request->getAttribute('user_id'); + $currentUser = $this->auth->user(); $data = $this->validate($request, ['text' => 'required']); - $otherUser = $this->user->findOrFail($request->getAttribute('user_id')); + $otherUser = $this->user->findOrFail($userId); $newMessage = new Message(); $newMessage->sender()->associate($currentUser); @@ -188,9 +192,10 @@ class MessagesController extends BaseController */ public function delete(Request $request): Response { + $otherUserId = (int)$request->getAttribute('user_id'); + $msgId = (int)$request->getAttribute('msg_id'); + $currentUser = $this->auth->user(); - $otherUserId = $request->getAttribute('user_id'); - $msgId = $request->getAttribute('msg_id'); $msg = $this->message->findOrFail($msgId); if ($msg->user_id == $currentUser->id) { diff --git a/src/Controllers/NewsController.php b/src/Controllers/NewsController.php index c42a16e1..c8f82950 100644 --- a/src/Controllers/NewsController.php +++ b/src/Controllers/NewsController.php @@ -100,10 +100,12 @@ class NewsController extends BaseController */ public function show(Request $request): Response { + $newsId = (int)$request->getAttribute('news_id'); + $news = $this->news ->with('user') ->with('comments') - ->findOrFail($request->getAttribute('id')); + ->findOrFail($newsId); return $this->renderView('pages/news/news.twig', ['news' => $news]); } @@ -114,12 +116,13 @@ class NewsController extends BaseController */ public function comment(Request $request): Response { + $newsId = (int)$request->getAttribute('news_id'); + $data = $this->validate($request, [ 'comment' => 'required', ]); $user = $this->auth->user(); - $news = $this->news - ->findOrFail($request->getAttribute('id')); + $news = $this->news->findOrFail($newsId); /** @var NewsComment $comment */ $comment = $news->comments()->create([ @@ -147,7 +150,8 @@ class NewsController extends BaseController */ public function deleteComment(Request $request): Response { - $id = $request->getAttribute('id'); + $commentId = (int)$request->getAttribute('comment_id'); + $this->validate( $request, [ @@ -155,7 +159,7 @@ class NewsController extends BaseController ] ); - $comment = $this->comment->findOrFail($id); + $comment = $this->comment->findOrFail($commentId); if ( $comment->user->id != $this->auth->user()->id && !$this->auth->can('admin_news') diff --git a/src/Controllers/OAuthController.php b/src/Controllers/OAuthController.php index 86a9744c..295abd4c 100644 --- a/src/Controllers/OAuthController.php +++ b/src/Controllers/OAuthController.php @@ -87,6 +87,7 @@ class OAuthController extends BaseController public function index(Request $request): Response { $providerName = $request->getAttribute('provider'); + $provider = $this->getProvider($providerName); $config = $this->config->get('oauth')[$providerName]; @@ -218,10 +219,11 @@ class OAuthController extends BaseController */ public function connect(Request $request): Response { - $provider = $request->getAttribute('provider'); - $this->requireProvider($provider); + $providerName = $request->getAttribute('provider'); - $this->session->set('oauth2_connect_provider', $provider); + $this->requireProvider($providerName); + + $this->session->set('oauth2_connect_provider', $providerName); return $this->index($request); } @@ -233,14 +235,14 @@ class OAuthController extends BaseController */ public function disconnect(Request $request): Response { - $provider = $request->getAttribute('provider'); + $providerName = $request->getAttribute('provider'); $this->oauth ->whereUserId($this->auth->user()->id) - ->where('provider', $provider) + ->where('provider', $providerName) ->delete(); - $this->log->info('Disconnected OAuth from {provider}', ['provider' => $provider]); + $this->log->info('Disconnected OAuth from {provider}', ['provider' => $providerName]); $this->addNotification('oauth.disconnected'); return $this->redirector->back(); diff --git a/src/Controllers/PasswordResetController.php b/src/Controllers/PasswordResetController.php index 26868fcd..bfcfa54e 100644 --- a/src/Controllers/PasswordResetController.php +++ b/src/Controllers/PasswordResetController.php @@ -154,6 +154,7 @@ class PasswordResetController extends BaseController protected function requireToken(Request $request): PasswordReset { $token = $request->getAttribute('token'); + /** @var PasswordReset|null $reset */ $reset = PasswordReset::whereToken($token)->first(); diff --git a/src/Middleware/RequestHandler.php b/src/Middleware/RequestHandler.php index ce3fb718..46aa4240 100644 --- a/src/Middleware/RequestHandler.php +++ b/src/Middleware/RequestHandler.php @@ -38,6 +38,7 @@ class RequestHandler implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $requestHandler = $request->getAttribute('route-request-handler'); + $requestHandler = $this->resolveRequestHandler($requestHandler); if ($requestHandler instanceof CallableHandler) { diff --git a/src/Middleware/SessionHandler.php b/src/Middleware/SessionHandler.php index 8c53b0fd..9d766383 100644 --- a/src/Middleware/SessionHandler.php +++ b/src/Middleware/SessionHandler.php @@ -34,12 +34,14 @@ class SessionHandler implements MiddlewareInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { + $requestPath = $request->getAttribute('route-request-path'); + $return = $handler->handle($request); $cookies = $request->getCookieParams(); if ( $this->session instanceof NativeSessionStorage - && in_array($request->getAttribute('route-request-path'), $this->paths) + && in_array($requestPath, $this->paths) && !isset($cookies[$this->session->getName()]) ) { $this->destroyNative(); diff --git a/tests/Unit/Controllers/Admin/FaqControllerTest.php b/tests/Unit/Controllers/Admin/FaqControllerTest.php index ea99b85d..8351da9c 100644 --- a/tests/Unit/Controllers/Admin/FaqControllerTest.php +++ b/tests/Unit/Controllers/Admin/FaqControllerTest.php @@ -25,7 +25,7 @@ class FaqControllerTest extends ControllerTest */ public function testEdit() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('faq_id', 1); $this->response->expects($this->once()) ->method('withView') ->willReturnCallback(function ($view, $data) { @@ -63,7 +63,7 @@ class FaqControllerTest extends ControllerTest */ public function testSaveCreateEdit() { - $this->request->attributes->set('id', 2); + $this->request->attributes->set('faq_id', 2); $body = $this->data; $this->request = $this->request->withParsedBody($body); @@ -95,7 +95,7 @@ class FaqControllerTest extends ControllerTest */ public function testSavePreview() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('faq_id', 1); $this->request = $this->request->withParsedBody([ 'question' => 'New question', 'text' => 'New text', @@ -132,7 +132,7 @@ class FaqControllerTest extends ControllerTest */ public function testSaveDelete() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('faq_id', 1); $this->request = $this->request->withParsedBody([ 'question' => '.', 'text' => '.', diff --git a/tests/Unit/Controllers/Admin/NewsControllerTest.php b/tests/Unit/Controllers/Admin/NewsControllerTest.php index 48defba4..eb919c25 100644 --- a/tests/Unit/Controllers/Admin/NewsControllerTest.php +++ b/tests/Unit/Controllers/Admin/NewsControllerTest.php @@ -35,7 +35,7 @@ class NewsControllerTest extends ControllerTest */ public function testEdit() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('news_id', 1); $this->response->expects($this->once()) ->method('withView') ->willReturnCallback(function ($view, $data) { @@ -83,7 +83,7 @@ class NewsControllerTest extends ControllerTest $controller->edit($this->request); // Should stay no meeting - $this->request->attributes->set('id', 1); + $this->request->attributes->set('news_id', 1); $controller->edit($this->request); } @@ -126,7 +126,7 @@ class NewsControllerTest extends ControllerTest bool $isMeeting, int $id = null ) { - $this->request->attributes->set('id', $id); + $this->request->attributes->set('news_id', $id); $id = $id ?: 2; $body = [ 'title' => 'Some Title', @@ -166,7 +166,7 @@ class NewsControllerTest extends ControllerTest */ public function testSavePreview() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('news_id', 1); $this->request = $this->request->withParsedBody([ 'title' => 'New title', 'text' => 'New text', @@ -209,7 +209,7 @@ class NewsControllerTest extends ControllerTest */ public function testSaveDelete() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('news_id', 1); $this->request = $this->request->withParsedBody([ 'title' => '.', 'text' => '.', diff --git a/tests/Unit/Controllers/Admin/QuestionsControllerTest.php b/tests/Unit/Controllers/Admin/QuestionsControllerTest.php index c3c46aaf..4f0fff68 100644 --- a/tests/Unit/Controllers/Admin/QuestionsControllerTest.php +++ b/tests/Unit/Controllers/Admin/QuestionsControllerTest.php @@ -100,7 +100,7 @@ class QuestionsControllerTest extends ControllerTest */ public function testEdit() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('question_id', 1); $this->response->expects($this->once()) ->method('withView') ->willReturnCallback(function (string $view, array $data) { @@ -139,7 +139,7 @@ class QuestionsControllerTest extends ControllerTest */ public function testSaveCreateEdit() { - $this->request->attributes->set('id', 2); + $this->request->attributes->set('question_id', 2); $body = [ 'text' => 'Foo?', 'answer' => 'Bar!', @@ -170,7 +170,7 @@ class QuestionsControllerTest extends ControllerTest */ public function testSavePreview() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('question_id', 1); $this->request = $this->request->withParsedBody([ 'text' => 'Foo?', 'answer' => 'Bar!', @@ -207,7 +207,7 @@ class QuestionsControllerTest extends ControllerTest */ public function testSaveDelete() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('question_id', 1); $this->request = $this->request->withParsedBody([ 'text' => '.', 'answer' => '.', diff --git a/tests/Unit/Controllers/Admin/UserShirtControllerTest.php b/tests/Unit/Controllers/Admin/UserShirtControllerTest.php index 414996f1..4f23804a 100644 --- a/tests/Unit/Controllers/Admin/UserShirtControllerTest.php +++ b/tests/Unit/Controllers/Admin/UserShirtControllerTest.php @@ -24,7 +24,7 @@ class UserShirtControllerTest extends ControllerTest */ public function testIndex() { - $request = $this->request->withAttribute('id', 1); + $request = $this->request->withAttribute('user_id', 1); /** @var Authenticator|MockObject $auth */ $auth = $this->createMock(Authenticator::class); /** @var Redirector|MockObject $redirector */ @@ -62,7 +62,7 @@ class UserShirtControllerTest extends ControllerTest public function testSaveShirt() { $request = $this->request - ->withAttribute('id', 1) + ->withAttribute('user_id', 1) ->withParsedBody([ 'shirt_size' => 'S', ]); diff --git a/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php b/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php index 1678bd8b..e6cb2f67 100644 --- a/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php +++ b/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php @@ -35,7 +35,7 @@ class UserWorkLogControllerTest extends ControllerTest */ public function testShowAddWorklogWithUnknownUserIdThrows() { - $request = $this->request->withAttribute('id', 1234); + $request = $this->request->withAttribute('user_id', 1234); $this->expectException(ModelNotFoundException::class); $this->controller->editWorklog($request); } @@ -47,7 +47,7 @@ class UserWorkLogControllerTest extends ControllerTest */ public function testShowAddWorklog() { - $request = $this->request->withAttribute('id', $this->user->id); + $request = $this->request->withAttribute('user_id', $this->user->id); $this->response->expects($this->once()) ->method('withView') ->willReturnCallback(function (string $view, array $data) { @@ -70,7 +70,7 @@ class UserWorkLogControllerTest extends ControllerTest */ public function testShowAddWorklogWithSuggestedWorkDate($buildup_start, $event_start, $suggested_work_date) { - $request = $this->request->withAttribute('id', $this->user->id); + $request = $this->request->withAttribute('user_id', $this->user->id); config(['buildup_start' => $buildup_start]); config(['event_start' => $event_start]); $this->response->expects($this->once()) @@ -93,7 +93,7 @@ class UserWorkLogControllerTest extends ControllerTest $worklog = Worklog::factory(['user_id' => $user2->id])->create(); $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id); $this->expectException(HttpNotFound::class); $this->controller->editWorklog($request); @@ -113,7 +113,7 @@ class UserWorkLogControllerTest extends ControllerTest ])->create(); $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id); $this->response->expects($this->once()) ->method('withView') @@ -133,7 +133,7 @@ class UserWorkLogControllerTest extends ControllerTest */ public function testSaveWorklogWithUnkownUserIdThrows() { - $request = $this->request->withAttribute('id', 1234)->withParsedBody([]); + $request = $this->request->withAttribute('user_id', 1234)->withParsedBody([]); $this->expectException(ModelNotFoundException::class); $this->controller->saveWorklog($request); } @@ -145,7 +145,7 @@ class UserWorkLogControllerTest extends ControllerTest */ public function testSaveWorklogWithInvalidParamsThrows($body) { - $request = $this->request->withAttribute('id', $this->user->id)->withParsedBody($body); + $request = $this->request->withAttribute('user_id', $this->user->id)->withParsedBody($body); $this->expectException(ValidationException::class); $this->controller->saveWorklog($request); } @@ -159,7 +159,7 @@ class UserWorkLogControllerTest extends ControllerTest $work_hours = 3.14; $comment = str_repeat('X', 200); $body = ['work_date' => $work_date, 'work_hours' => $work_hours, 'comment' => $comment]; - $request = $this->request->withAttribute('id', $this->user->id)->withParsedBody($body); + $request = $this->request->withAttribute('user_id', $this->user->id)->withParsedBody($body); $this->setExpects($this->auth, 'user', null, $this->user, $this->any()); $this->redirect->expects($this->once()) ->method('to') @@ -184,7 +184,7 @@ class UserWorkLogControllerTest extends ControllerTest { $body = ['work_date' => Carbon::today(), 'work_hours' => 3.14, 'comment' => 'a comment']; $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', 1234) ->withParsedBody($body); $this->expectException(ModelNotFoundException::class); @@ -203,7 +203,7 @@ class UserWorkLogControllerTest extends ControllerTest $body = ['work_date' => Carbon::today(), 'work_hours' => 3.14, 'comment' => 'a comment']; $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id) ->withParsedBody($body); $this->expectException(HttpNotFound::class); @@ -223,7 +223,7 @@ class UserWorkLogControllerTest extends ControllerTest $body = ['work_date' => $work_date, 'work_hours' => $work_hours, 'comment' => $comment]; $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id) ->withParsedBody($body); $this->setExpects($this->auth, 'user', null, $this->user, $this->any()); @@ -252,7 +252,7 @@ class UserWorkLogControllerTest extends ControllerTest $worklog = Worklog::factory(['user_id' => $user2->id])->create(); $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id); $this->expectException(HttpNotFound::class); $this->controller->showDeleteWorklog($request); @@ -267,7 +267,7 @@ class UserWorkLogControllerTest extends ControllerTest $worklog = Worklog::factory(['user_id' => $this->user->id])->create(); $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id); $this->response->expects($this->once()) ->method('withView') @@ -284,7 +284,7 @@ class UserWorkLogControllerTest extends ControllerTest public function testDeleteWorklogWithUnknownWorkLogIdThrows() { $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', 1234); $this->expectException(ModelNotFoundException::class); $this->controller->deleteWorklog($request); @@ -301,7 +301,7 @@ class UserWorkLogControllerTest extends ControllerTest $worklog = Worklog::factory(['user_id' => $user2->id])->create(); $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id); $this->expectException(HttpNotFound::class); $this->controller->deleteWorklog($request); @@ -316,7 +316,7 @@ class UserWorkLogControllerTest extends ControllerTest $worklog = Worklog::factory(['user_id' => $this->user->id])->create(); $request = $this->request - ->withAttribute('id', $this->user->id) + ->withAttribute('user_id', $this->user->id) ->withAttribute('worklog_id', $worklog->id); $this->setExpects($this->auth, 'user', null, $this->user, $this->any()); $this->redirect->expects($this->once()) diff --git a/tests/Unit/Controllers/NewsControllerTest.php b/tests/Unit/Controllers/NewsControllerTest.php index 1e2a26dd..6d0663ca 100644 --- a/tests/Unit/Controllers/NewsControllerTest.php +++ b/tests/Unit/Controllers/NewsControllerTest.php @@ -129,7 +129,7 @@ class NewsControllerTest extends ControllerTest */ public function testShow() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('news_id', 1); $this->response->expects($this->once()) ->method('withView') ->with('pages/news/news.twig') @@ -146,7 +146,7 @@ class NewsControllerTest extends ControllerTest */ public function testShowNotFound() { - $this->request->attributes->set('id', 42); + $this->request->attributes->set('news_id', 42); /** @var NewsController $controller */ $controller = $this->app->make(NewsController::class); @@ -173,7 +173,7 @@ class NewsControllerTest extends ControllerTest */ public function testCommentNewsNotFound() { - $this->request->attributes->set('id', 42); + $this->request->attributes->set('news_id', 42); $this->request = $this->request->withParsedBody(['comment' => 'Foo bar!']); $this->addUser(); @@ -190,7 +190,7 @@ class NewsControllerTest extends ControllerTest */ public function testComment() { - $this->request->attributes->set('id', 1); + $this->request->attributes->set('news_id', 1); $this->request = $this->request->withParsedBody(['comment' => 'Foo bar!']); $this->addUser(); @@ -228,7 +228,7 @@ class NewsControllerTest extends ControllerTest */ public function testDeleteCommentNotFound() { - $this->request = $this->request->withAttribute('id', 42)->withParsedBody(['delete' => '1']); + $this->request = $this->request->withAttribute('news_id', 42)->withParsedBody(['delete' => '1']); /** @var NewsController $controller */ $controller = $this->app->get(NewsController::class); @@ -243,7 +243,7 @@ class NewsControllerTest extends ControllerTest */ public function testDeleteCommentNotAllowed() { - $this->request = $this->request->withAttribute('id', 2)->withParsedBody(['delete' => '1']); + $this->request = $this->request->withAttribute('comment_id', 2)->withParsedBody(['delete' => '1']); $this->addUser(1); $this->addUser(2); @@ -261,7 +261,7 @@ class NewsControllerTest extends ControllerTest */ public function testDeleteComment() { - $this->request = $this->request->withAttribute('id', 1)->withParsedBody(['delete' => '1']); + $this->request = $this->request->withAttribute('comment_id', 1)->withParsedBody(['delete' => '1']); $this->setExpects($this->response, 'redirectTo', ['http://localhost/news/1'], $this->response); $this->addUser(1);