Skip to content

Commit

Permalink
pkp#10506 optimize relationships with eager loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Hafsa-Naeem committed Nov 18, 2024
1 parent 1ce4b3f commit 2ef27fa
Show file tree
Hide file tree
Showing 27 changed files with 773 additions and 536 deletions.
61 changes: 42 additions & 19 deletions classes/core/PKPPageRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use PKP\plugins\Hook;
use PKP\security\Role;
use PKP\security\Validation;
use PKP\userGroup\UserGroup;
use PKP\userGroup\relationships\UserUserGroup;


class PKPPageRouter extends PKPRouter
{
Expand Down Expand Up @@ -402,50 +405,70 @@ public function getHomeUrl(PKPRequest $request): string
$userId = $user->getId();

if ($context = $this->getContext($request)) {
// If the user has no roles, or only one role and this is reader, go to "Index" page.
// Else go to "submissions" page
$userGroups = Repo::userGroup()->userUserGroups($userId, $context->getId());

// 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()->getRoleId() == Role::ROLE_ID_READER)
|| ($userGroups->count() == 1 && $userGroups->first()->role_id == Role::ROLE_ID_READER)
) {
return $request->url(null, 'index');
}

if(Config::getVar('features', 'enable_new_submission_listing')) {

$roleIds = $userGroups->map(function ($group) {
return $group->getRoleId();
});

$roleIdsArray = $roleIds->all();
$roleIdsArray = $userGroups->pluck('role_id')->all();

if (count(array_intersect([Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_ASSISTANT], $roleIdsArray))) {
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(count(array_intersect([ Role::ROLE_ID_REVIEWER], $roleIdsArray))) {
if (in_array(Role::ROLE_ID_REVIEWER, $roleIdsArray)) {
return $request->url(null, 'dashboard', 'reviewAssignments');

}
if(count(array_intersect([ Role::ROLE_ID_AUTHOR], $roleIdsArray))) {
if (in_array(Role::ROLE_ID_AUTHOR, $roleIdsArray)) {
return $request->url(null, 'dashboard', 'mySubmissions');
}
}

return $request->url(null, 'submissions');
} else {
// The user is at the site context, check to see if they are
// only registered in one place w/ one role
$userGroups = Repo::userGroup()->userUserGroups($userId, \PKP\core\PKPApplication::SITE_CONTEXT_ID);
// 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->getContextId());
$context = $contextDao->getById($firstUserGroup->context_id);
if (!isset($context)) {
$request->redirect(Application::SITE_CONTEXT_PATH, 'index');
}
if ($firstUserGroup->getRoleId() == Role::ROLE_ID_READER) {
if ($firstUserGroup->role_id == Role::ROLE_ID_READER) {
$request->redirect(null, 'index');
}
}
Expand Down
10 changes: 5 additions & 5 deletions classes/mail/mailables/UserRoleAssignmentInvitationNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,23 @@ private function getUserUserGroupSection(UserGroupHelper $userUserGroup, UserGro
$sectionMastheadAppear = __('emails.userRoleAssignmentInvitationNotify.userGroupSectionWillNotAppear',
[
'contextName' => $context->getName($locale),
'sectionName' => $userGroup->getName($locale)
'sectionName' => $userGroup->getLocalizedData('name', $locale)
]
);

if (isset($userUserGroup->masthead) && $userUserGroup->masthead) {
$sectionMastheadAppear = __('emails.userRoleAssignmentInvitationNotify.userGroupSectionWillAppear',
[
'contextName' => $context->getName($locale),
'sectionName' => $userGroup->getName($locale)
'sectionName' => $userGroup->getLocalizedData('name', $locale)
]
);
}

$userGroupSection = __('emails.userRoleAssignmentInvitationNotify.userGroupSection',
[
'sectionNumber' => $count,
'sectionName' => $userGroup->getName($locale),
'sectionName' => $userGroup->getLocalizedData('name', $locale),
'dateStart' => $userUserGroup->dateStart,
'sectionEndingDate' => $sectionEndingDate,
'sectionMastheadAppear' => $sectionMastheadAppear
Expand Down Expand Up @@ -192,7 +192,7 @@ public function setData(?string $locale = null): void

// Fetch the user-user group relationships
$userUserGroups = UserUserGroup::withUserId($user->getId())
->withUserGroupId($userGroup->getId())
->withUserGroupId($userGroup->id)
->withActive()
->get();

Expand All @@ -215,7 +215,7 @@ public function setData(?string $locale = null): void

foreach ($userGroups as $userGroup) {
$userUserGroups = UserUserGroup::withUserId($user->getId())
->withUserGroupId($userGroup->getId())
->withUserGroupId($userGroup->id)
->withActive()
->get();

Expand Down
3 changes: 2 additions & 1 deletion classes/orcid/PKPOrcidWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ private function buildOrcidContributors(array $authors, Context $context, Public
];

$userGroup = $author->getUserGroup();
$role = self::USER_GROUP_TO_ORCID_ROLE[$userGroup->getName('en')];
$roleName = $userGroup->getLocalizedData('name', 'en');
$role = self::USER_GROUP_TO_ORCID_ROLE[$roleName];

if ($role) {
$contributor['contributor-attributes']['contributor-role'] = $role;
Expand Down
25 changes: 19 additions & 6 deletions classes/security/RoleDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PKP\core\Core;
use PKP\db\DAO;
use PKP\db\DAORegistry;
use PKP\userGroup\UserGroup;

class RoleDAO extends DAO
{
Expand Down Expand Up @@ -89,14 +90,26 @@ public function getByUserId(int $userId, ?int $contextId = Application::SITE_CON
*/
public function getByUserIdGroupedByContext(int $userId)
{
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
$userGroups = Repo::userGroup()->userUserGroups($userId);

$userGroups = UserGroup::query()
->with('userUserGroups')
->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();

$roles = [];
foreach ($userGroups as $userGroup) {
$role = $roleDao->newDataObject();
$role->setRoleId($userGroup->id);
$roles[(int) $userGroup->contextId][$userGroup->roleId] = $role;
$role = $this->newDataObject();
$role->setRoleId($userGroup->role_id);
$roles[(int) $userGroup->context_id][$userGroup->role_id] = $role;
}

return $roles;
Expand Down
Loading

0 comments on commit 2ef27fa

Please sign in to comment.