Skip to content

Commit

Permalink
pkp#10506 Refactor UserGroup to use Eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
Hafsa-Naeem authored and Vitaliy-1 committed Nov 29, 2024
1 parent 8d8ada9 commit 82a3050
Show file tree
Hide file tree
Showing 97 changed files with 1,993 additions and 1,944 deletions.
9 changes: 6 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,10 @@ 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::withContextIds($contextId)->get();


$genreDao = DAORegistry::getDAO('GenreDAO'); /** @var GenreDAO $genreDao */
$genres = $genreDao->getByContextId($submission->getData('contextId'))->toArray();
Expand Down
19 changes: 10 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,10 @@ public function getMany(Request $illuminateRequest): JsonResponse

$submissions = $collector->getMany();

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

$userGroups = UserGroup::withContextIds($contextId)->cursor();


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

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

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

$submissions = $collector->getMany();

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

/** @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::withUserGroupIds($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
112 changes: 46 additions & 66 deletions api/v1/submissions/PKPSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Illuminate\Support\Enumerable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\LazyCollection;
use PKP\components\forms\FormComponent;
use PKP\components\forms\publication\PKPCitationsForm;
use PKP\components\forms\publication\PKPMetadataForm;
Expand Down Expand Up @@ -70,6 +71,7 @@
use PKP\submissionFile\SubmissionFile;
use PKP\userGroup\UserGroup;


class PKPSubmissionController extends PKPBaseController
{
use AnonymizeData;
Expand Down Expand Up @@ -425,9 +427,7 @@ public function getMany(Request $illuminateRequest): JsonResponse

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

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

/** @var \PKP\submission\GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -533,9 +533,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::withContextIds($submission->getData('contextId'))->cursor();


// 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 +598,20 @@ public function add(Request $illuminateRequest): JsonResponse
}
}
}
$submitterUserGroups = UserGroup::withContextIds($context->getId())
->withRoleIds([Role::ROLE_ID_MANAGER, Role::ROLE_ID_AUTHOR])
->whereHas('userUserGroups', function ($query) use ($user) {
$query->withUserId($user->getId());
})
->get();

$submitterUserGroups = Repo::userGroup()
->getCollector()
->filterByContextIds([$context->getId()])
->filterByUserIds([$user->getId()])
->filterByRoleIds([Role::ROLE_ID_MANAGER, Role::ROLE_ID_AUTHOR])
->getMany();

$userGroupIdPropName = 'userGroupId';

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

Expand All @@ -655,32 +654,34 @@ public function add(Request $illuminateRequest): JsonResponse
Repo::stageAssignment()
->build(
$submission->getId(),
$submitAsUserGroup->getId(),
$submitAsUserGroup->id,
$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->id);
$authorId = Repo::author()->add($author);
Repo::publication()->edit($publication, ['primaryContactId' => $authorId]);
}

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

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
$genres = $genreDao->getByContextId($submission->getData('contextId'))->toArray();

if (!$userGroups instanceof LazyCollection) {
$userGroups = $userGroups->lazy();
}

return response()->json(Repo::submission()->getSchemaMap()->map($submission, $userGroups, $genres), Response::HTTP_OK);
}

Expand Down Expand Up @@ -712,9 +713,7 @@ public function edit(Request $illuminateRequest): JsonResponse

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

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

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -766,11 +765,7 @@ public function saveForLater(Request $illuminateRequest): JsonResponse
Mail::send($mailable);

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

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

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

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



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

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


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

$publications = $collector->getMany();

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

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

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

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1117,9 +1107,8 @@ 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::withContextIds($submission->getData('contextId'))->cursor();


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

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

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1274,9 +1261,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::withContextIds($submission->getData('contextId'))->cursor();

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

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

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


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

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

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


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

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

/** @var GenreDAO $genreDao */
$genreDao = DAORegistry::getDAO('GenreDAO');
Expand Down Expand Up @@ -1790,7 +1771,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 @@ -1906,8 +1887,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::query()->withContextIds([$submission->getData('contextId')])->withRoleIds([Role::ROLE_ID_AUTHOR])->cursor();
$publicationLicenseForm = new PKPPublicationLicenseForm($publicationApiUrl, $locales, $publication, $context, $authorUserGroups);

return response()->json($this->getLocalizedForm($publicationLicenseForm, $submissionLocale, $locales), Response::HTTP_OK);
Expand Down
Loading

0 comments on commit 82a3050

Please sign in to comment.