Skip to content

Commit

Permalink
pkp#10506 Update usage for porting UserGroup to Eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
Hafsa-Naeem committed Oct 28, 2024
1 parent 35b9299 commit f20097d
Show file tree
Hide file tree
Showing 73 changed files with 1,002 additions and 801 deletions.
8 changes: 5 additions & 3 deletions api/v1/_dois/PKPBackendDoiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use PKP\security\authorization\UserRolesRequiredPolicy;
use PKP\security\Role;
use PKP\submission\GenreDAO;
use PKP\userGroup\UserGroup;


class PKPBackendDoiController extends PKPBaseController
{
Expand Down Expand Up @@ -128,9 +130,9 @@ public function editPublication(Request $illuminateRequest): JsonResponse
$publication = Repo::publication()->get($publication->getId());

$submission = Repo::submission()->get($publication->getData('submissionId'));
$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();

$contextId = $submission->getData('contextId');
$userGroups = UserGroup::where('contextId', $contextId)->get();

$genreDao = DAORegistry::getDAO('GenreDAO'); /** @var GenreDAO $genreDao */
$genres = $genreDao->getByContextId($submission->getData('contextId'))->toArray();
Expand Down
18 changes: 9 additions & 9 deletions api/v1/_submissions/PKPBackendSubmissionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use PKP\security\Role;
use PKP\submission\DashboardView;
use PKP\submission\PKPSubmission;
use PKP\userGroup\UserGroup;


abstract class PKPBackendSubmissionsController extends PKPBaseController
{
Expand Down Expand Up @@ -218,9 +220,9 @@ public function getMany(Request $illuminateRequest): JsonResponse

$submissions = $collector->getMany();

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
$contextId = $context->getId();

$userGroups = UserGroup::where('contextId', $contextId)->get();

/** @var \PKP\submission\GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -253,9 +255,8 @@ public function assigned(Request $illuminateRequest): JsonResponse
->assignedTo([$user->getId()])
->getMany();

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
$contextId = $context->getId();
$userGroups = UserGroup::where('contextId', $contextId)->get();

/** @var \PKP\submission\GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -325,9 +326,8 @@ public function reviews(Request $illuminateRequest): JsonResponse

$submissions = $collector->getMany();

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
$contextId = $context->getId();
$userGroups = UserGroup::where('contextId', $contextId)->get();

/** @var \PKP\submission\GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down
7 changes: 3 additions & 4 deletions api/v1/submissions/AnonymizeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ public function anonymizeReviews(LazyCollection|Submission $submissions, ?LazyCo
->pluck('user_group_id')
->toArray();

$currentUserGroups = Repo::userGroup()->getCollector()
->filterByUserGroupIds($currentUserUserGroupIds)
->getMany();
$currentUserGroups = UserGroup::whereIn('userGroupId', $currentUserUserGroupIds)->get();


$isAuthor = $currentUserGroups->contains(
fn (UserGroup $userGroup) =>
$userGroup->getRoleId() == Role::ROLE_ID_AUTHOR
$userGroup->roleId == Role::ROLE_ID_AUTHOR
);

if ($currentUserReviewAssignment->isNotEmpty() || $isAuthor) {
Expand Down
105 changes: 39 additions & 66 deletions api/v1/submissions/PKPSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,7 @@ public function getMany(Request $illuminateRequest): JsonResponse

$anonymizeReviews = $this->anonymizeReviews($submissions);

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
$userGroups = UserGroup::where('contextId', $context->getId())->get();

/** @var \PKP\submission\GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -533,9 +531,8 @@ public function get(Request $illuminateRequest): JsonResponse
{
$submission = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION);

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();


// Anonymize sensitive review assignment data if user is a reviewer or author assigned to the article and review isn't open
$reviewAssignments = Repo::reviewAssignment()->getCollector()->filterBySubmissionIds([$submission->getId()])->getMany()->remember();
Expand Down Expand Up @@ -599,20 +596,20 @@ public function add(Request $illuminateRequest): JsonResponse
}
}
}

$submitterUserGroups = Repo::userGroup()
->getCollector()
->filterByContextIds([$context->getId()])
->filterByUserIds([$user->getId()])
->filterByRoleIds([Role::ROLE_ID_MANAGER, Role::ROLE_ID_AUTHOR])
->getMany();
$submitterUserGroups = UserGroup::where('contextId', $context->getId())
->where('roleId', Role::ROLE_ID_MANAGER)
->orWhere('roleId', Role::ROLE_ID_AUTHOR)
->whereHas('userUserGroups', function ($query) use ($user) {
$query->where('userId', $user->getId());
})
->get();

$userGroupIdPropName = 'userGroupId';

if (isset($params[$userGroupIdPropName])) {
$submitAsUserGroup = $submitterUserGroups
->first(function (UserGroup $userGroup) use ($params, $userGroupIdPropName) {
return $userGroup->getId() === $params[$userGroupIdPropName];
return $userGroup->usergroupid === $params[$userGroupIdPropName];
});
if (!$submitAsUserGroup) {
$errors[$userGroupIdPropName] = [__('api.submissions.400.invalidSubmitAs')];
Expand All @@ -624,14 +621,14 @@ public function add(Request $illuminateRequest): JsonResponse
})
->first();
} else {
$submitAsUserGroup = Repo::userGroup()->getFirstSubmitAsAuthorUserGroup($context->getId());
$submitAsUserGroup = UserGroup::where('contextId', $context->getId())->where('roleId', Role::ROLE_ID_AUTHOR)->first();
if (!$submitAsUserGroup) {
$errors[$userGroupIdPropName] = [__('submission.wizard.notAllowed.description')];
} else {
Repo::userGroup()->assignUserToGroup(
$user->getId(),
$submitAsUserGroup->getId()
);
$submitAsUserGroup->usergroupid
);
}
}

Expand All @@ -655,27 +652,25 @@ public function add(Request $illuminateRequest): JsonResponse
Repo::stageAssignment()
->build(
$submission->getId(),
$submitAsUserGroup->getId(),
$submitAsUserGroup->usergroupid,
$request->getUser()->getId(),
$submitAsUserGroup->getRecommendOnly(),
$submitAsUserGroup->recommendOnly,
// Authors can always edit metadata before submitting
$submission->getData('submissionProgress')
? true
: $submitAsUserGroup->getPermitMetadataEdit()
: $submitAsUserGroup->permitMetadataEdit
);

// Create an author record from the submitter's user account
if ($submitAsUserGroup->getRoleId() === Role::ROLE_ID_AUTHOR) {
if ($submitAsUserGroup->roleId === Role::ROLE_ID_AUTHOR) {
$author = Repo::author()->newAuthorFromUser($request->getUser());
$author->setData('publicationId', $publication->getId());
$author->setUserGroupId($submitAsUserGroup->getId());
$author->setUserGroupId($submitAsUserGroup->usergroupid);
$authorId = Repo::author()->add($author);
Repo::publication()->edit($publication, ['primaryContactId' => $authorId]);
}

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -712,9 +707,7 @@ public function edit(Request $illuminateRequest): JsonResponse

$submission = Repo::submission()->get($submission->getId());

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -767,10 +760,8 @@ public function saveForLater(Request $illuminateRequest): JsonResponse

$submission = Repo::submission()->get($submission->getId());

$userGroups = Repo::userGroup()
->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$contextId = $submission->getData('contextId');
$userGroups = UserGroup::where('context_id', $contextId)->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -847,10 +838,8 @@ public function submit(Request $illuminateRequest): JsonResponse
Repo::eventLog()->add($eventLog);
}

$userGroups = Repo::userGroup()
->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
$contextId = $context->getId();
$userGroups = UserGroup::where('context_id', $contextId)->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand All @@ -872,9 +861,8 @@ public function delete(Request $illuminateRequest): JsonResponse
], Response::HTTP_NOT_FOUND);
}

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();


/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1033,9 +1021,7 @@ public function getPublications(Request $illuminateRequest): JsonResponse

$publications = $collector->getMany();

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

$currentUserReviewAssignment = Repo::reviewAssignment()->getCollector()
->filterBySubmissionIds([$submission->getId()])
Expand Down Expand Up @@ -1077,9 +1063,7 @@ public function getPublication(Request $illuminateRequest): JsonResponse
], Response::HTTP_FORBIDDEN);
}

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1117,9 +1101,7 @@ public function addPublication(Request $illuminateRequest): JsonResponse
$newId = Repo::publication()->add($publication);
$publication = Repo::publication()->get($newId);

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1199,9 +1181,7 @@ public function versionPublication(Request $illuminateRequest): JsonResponse
Mail::send($mailable);
}

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1275,9 +1255,7 @@ public function editPublication(Request $illuminateRequest): JsonResponse
Repo::publication()->edit($publication, $params);
$publication = Repo::publication()->get($publication->getId());

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1337,9 +1315,7 @@ public function publishPublication(Request $illuminateRequest): JsonResponse

$publication = Repo::publication()->get($publication->getId());

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1381,9 +1357,8 @@ public function unpublishPublication(Request $illuminateRequest): JsonResponse

$publication = Repo::publication()->get($publication->getId());

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$userGroups = UserGroup::where('contextId', $submission->getData('contextId'))->get();


/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1425,9 +1400,8 @@ public function deletePublication(Request $illuminateRequest): JsonResponse
], Response::HTTP_FORBIDDEN);
}

$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();
$contextId = $submission->getData('contextId');
$userGroups = UserGroup::where('context_id', $contextId)->get();

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1791,7 +1765,7 @@ public function addDecision(Request $illuminateRequest): JsonResponse

protected function getFirstUserGroupInRole(Enumerable $userGroups, int $role): ?UserGroup
{
return $userGroups->first(fn (UserGroup $userGroup) => $userGroup->getRoleId() === $role);
return $userGroups->first(fn (UserGroup $userGroup) => $userGroup->roleId === $role);
}

/**
Expand Down Expand Up @@ -1907,8 +1881,7 @@ protected function getPublicationLicenseForm(Request $illuminateRequest): JsonRe

$submissionLocale = $submission->getData('locale');
$locales = $this->getPublicationFormLocales($context, $submission);
$authorUserGroups = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_AUTHOR], $submission->getData('contextId'));

$authorUserGroups = UserGroup::where('contextId', $submission->getData('contextId'))->where('roleId', Role::ROLE_ID_AUTHOR)->get();
$publicationLicenseForm = new PKPPublicationLicenseForm($publicationApiUrl, $locales, $publication, $context, $authorUserGroups);

return response()->json($this->getLocalizedForm($publicationLicenseForm, $submissionLocale, $locales), Response::HTTP_OK);
Expand Down
9 changes: 5 additions & 4 deletions classes/author/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use APP\facades\Repo;
use PKP\facades\Locale;
use PKP\identity\Identity;
use PKP\userGroup\UserGroup;

class Author extends Identity
{
Expand Down Expand Up @@ -212,10 +213,10 @@ public function getUserGroup()
{
//FIXME: should this be queried when fetching Author from DB? - see #5231.
static $userGroup; // Frequently we'll fetch the same one repeatedly
if (!$userGroup || $this->getUserGroupId() != $userGroup->getId()) {
$userGroup = Repo::userGroup()->get($this->getUserGroupId());
if (!$userGroup || $this->getData('userGroupId') != $userGroup->usergroupid) {
$userGroup = UserGroup::find($this->getData('userGroupId'));
}
return $userGroup;

}

/**
Expand All @@ -226,7 +227,7 @@ public function getUserGroup()
public function getLocalizedUserGroupName()
{
$userGroup = $this->getUserGroup();
return $userGroup->getLocalizedName();
return $userGroup->getLocalized('name');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions classes/author/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(PKPRequest $request, \PKP\context\Context $context,
{
parent::__construct($request, $context, $schemaService);

$this->authorUserGroups = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_AUTHOR], $this->context->getId());
$this->authorUserGroups = UserGroup::where('roleId', Role::ROLE_ID_AUTHOR)->where('contextId', $this->context->getId())->get();
}

/**
Expand Down Expand Up @@ -94,8 +94,8 @@ protected function mapByProperties(array $props, Author $item): array
switch ($prop) {
case 'userGroupName':
/** @var UserGroup $userGroup */
$userGroup = $this->authorUserGroups->first(fn (UserGroup $userGroup) => $userGroup->getId() === $item->getData('userGroupId'));
$output[$prop] = $userGroup ? $userGroup->getName(null) : new stdClass();
$userGroup = $this->authorUserGroups->first(fn (UserGroup $userGroup) => $userGroup->usergroupid === $item->getData('userGroupId'));
$output[$prop] = $userGroup ? $userGroup->name : new stdClass();
break;
case 'fullName':
$output[$prop] = $item->getFullName();
Expand Down
Loading

0 comments on commit f20097d

Please sign in to comment.