From 1bf0dd646168d66601236b84e1174e9ba50e3296 Mon Sep 17 00:00:00 2001 From: Jonas Raoni Soares da Silva Date: Fri, 17 Jan 2025 18:50:18 +0300 Subject: [PATCH 1/2] pkp/pkp-lib#10816 Fixed redirect loop --- classes/core/PKPPageRouter.php | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/classes/core/PKPPageRouter.php b/classes/core/PKPPageRouter.php index c4d1883a202..9f23f1538c1 100644 --- a/classes/core/PKPPageRouter.php +++ b/classes/core/PKPPageRouter.php @@ -443,33 +443,6 @@ public function getHomeUrl(PKPRequest $request): string return $request->url(null, 'submissions'); } else { - // The user is at the site context - $userGroups = UserGroup::query() - ->where('context_id', \PKP\core\PKPApplication::SITE_CONTEXT_ID) - ->whereHas('userUserGroups', function ($query) use ($userId) { - $query->where('user_id', $userId) - ->where(function ($q) { - $q->whereNull('date_end') - ->orWhere('date_end', '>', now()); - }) - ->where(function ($q) { - $q->whereNull('date_start') - ->orWhere('date_start', '<=', now()); - }); - }) - ->get(); - - if ($userGroups->count() == 1) { - $firstUserGroup = $userGroups->first(); - $contextDao = Application::getContextDAO(); - $context = $contextDao->getById($firstUserGroup->contextId); - if (!isset($context)) { - $request->redirect(Application::SITE_CONTEXT_PATH, 'index'); - } - if ($firstUserGroup->roleId == Role::ROLE_ID_READER) { - $request->redirect(null, 'index'); - } - } return $request->url(Application::SITE_CONTEXT_PATH, 'index'); } } From 0ae901e55b81a7789a1db3594ce4e0966df2ba67 Mon Sep 17 00:00:00 2001 From: Jonas Raoni Soares da Silva Date: Fri, 17 Jan 2025 20:15:44 +0300 Subject: [PATCH 2/2] pkp/pkp-lib#10816 Early return/reduced code indentation --- classes/core/PKPPageRouter.php | 70 +++++++++++++++------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/classes/core/PKPPageRouter.php b/classes/core/PKPPageRouter.php index 9f23f1538c1..91a05b6c0a5 100644 --- a/classes/core/PKPPageRouter.php +++ b/classes/core/PKPPageRouter.php @@ -17,6 +17,7 @@ namespace PKP\core; use APP\core\Application; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; use PKP\config\Config; use PKP\context\Context; @@ -399,54 +400,45 @@ public function redirectHome(PKPRequest $request): void */ public function getHomeUrl(PKPRequest $request): string { + $context = $this->getContext($request); + if (!$context) { + return $request->url(Application::SITE_CONTEXT_PATH, 'index'); + } + $user = Auth::user(); /** @var \PKP\user\User $user */ $userId = $user->getId(); + // fetch user groups for the user in the current context + $userGroups = UserGroup::query() + ->where('context_id', $context->getId()) + ->whereHas( + 'userUserGroups', + fn (Builder $query) => $query->where('user_id', $userId) + ->where(fn (Builder $q) => $q->whereNull('date_end')->orWhere('date_end', '>', now())) + ->where(fn (Builder $q) => $q->whereNull('date_start')->orWhere('date_start', '<=', now())) + ) + ->get(); + if ($userGroups->isEmpty() || ($userGroups->count() == 1 && $userGroups->first()->role_id == Role::ROLE_ID_READER)) { + return $request->url(null, 'index'); + } - if ($context = $this->getContext($request)) { - // fetch user groups for the user in the current context - $userGroups = UserGroup::query() - ->where('context_id', $context->getId()) - ->whereHas('userUserGroups', function ($query) use ($userId) { - $query->where('user_id', $userId) - ->where(function ($q) { - $q->whereNull('date_end') - ->orWhere('date_end', '>', now()); - }) - ->where(function ($q) { - $q->whereNull('date_start') - ->orWhere('date_start', '<=', now()); - }); - }) - ->get(); - - if ($userGroups->isEmpty() - || ($userGroups->count() == 1 && $userGroups->first()->role_id == Role::ROLE_ID_READER) - ) { - return $request->url(null, 'index'); - } - - if (Config::getVar('features', 'enable_new_submission_listing')) { - - $roleIdsArray = $userGroups->pluck('role_id')->all(); + if (Config::getVar('features', 'enable_new_submission_listing')) { + $roleIdsArray = $userGroups->pluck('role_id')->all(); - if (array_intersect([Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_ASSISTANT], $roleIdsArray)) { - return $request->url(null, 'dashboard', 'editorial'); + if (array_intersect([Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_ASSISTANT], $roleIdsArray)) { + return $request->url(null, 'dashboard', 'editorial'); + } - } - if (in_array(Role::ROLE_ID_REVIEWER, $roleIdsArray)) { - return $request->url(null, 'dashboard', 'reviewAssignments'); - } - if (in_array(Role::ROLE_ID_AUTHOR, $roleIdsArray)) { - return $request->url(null, 'dashboard', 'mySubmissions'); - } + if (in_array(Role::ROLE_ID_REVIEWER, $roleIdsArray)) { + return $request->url(null, 'dashboard', 'reviewAssignments'); } - return $request->url(null, 'submissions'); - } else { - return $request->url(Application::SITE_CONTEXT_PATH, 'index'); + if (in_array(Role::ROLE_ID_AUTHOR, $roleIdsArray)) { + return $request->url(null, 'dashboard', 'mySubmissions'); + } } - } + return $request->url(null, 'submissions'); + } // // Private helper methods.