Skip to content
Open

12017 #12019

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions api/v1/submissions/PKPSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use PKP\mail\mailables\SubmissionSavedForLater;
use PKP\notification\Notification;
use PKP\notification\NotificationSubscriptionSettingsDAO;
use PKP\observers\events\MetadataChanged;
use PKP\orcid\OrcidManager;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
Expand All @@ -73,13 +74,11 @@
use PKP\security\Role;
use PKP\security\Validation;
use PKP\services\PKPSchemaService;
use PKP\stageAssignment\StageAssignment;
use PKP\submission\GenreDAO;
use PKP\submission\reviewAssignment\ReviewAssignment;
use PKP\submissionFile\SubmissionFile;
use PKP\userGroup\UserGroup;
use PKP\observers\events\MetadataChanged;
use PKP\stageAssignment\StageAssignment;


class PKPSubmissionController extends PKPBaseController
{
Expand Down Expand Up @@ -643,7 +642,7 @@ 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());
$query->withUserId($user->getId())->withActive();
})
->get();

Expand Down Expand Up @@ -1461,7 +1460,7 @@ public function publishPublication(Request $illuminateRequest): JsonResponse

foreach ($stageAssignments as $stageAssignment) {
$userGroup = $stageAssignment->userGroup;
if ($userGroup && $userGroup->roleId === Role::ROLE_ID_AUTHOR){
if ($userGroup && $userGroup->roleId === Role::ROLE_ID_AUTHOR) {
$stageAssignment->canChangeMetadata = 0;
$stageAssignment->save();
}
Expand Down
11 changes: 4 additions & 7 deletions classes/core/PKPPageRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace PKP\core;

use APP\core\Application;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Facades\Auth;
use PKP\config\Config;
use PKP\context\Context;
Expand Down Expand Up @@ -410,12 +410,9 @@ public function getHomeUrl(PKPRequest $request): string
// 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()))
)
->whereHas('userUserGroups', function (EloquentBuilder $query) use ($userId) {
$query->withUserId($userId)->withActive();
})
->get();
if ($userGroups->isEmpty() || ($userGroups->count() == 1 && $userGroups->first()->role_id == Role::ROLE_ID_READER)) {
return $request->url(null, 'index');
Expand Down
13 changes: 3 additions & 10 deletions classes/security/RoleDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace PKP\security;

use APP\core\Application;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use PKP\core\Core;
Expand Down Expand Up @@ -89,16 +90,8 @@ public function getByUserIdGroupedByContext(int $userId): array
{
$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());
});
->whereHas('userUserGroups', function (EloquentBuilder $query) use ($userId) {
$query->withUserId($userId)->withActive();
})
->get();

Expand Down
12 changes: 4 additions & 8 deletions classes/security/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,10 @@ public static function getAdministrationLevel(

// single query to fetch user groups assigned to either user
$allUserGroups = UserGroup::query()
->whereHas(
'userUserGroups',
fn ($q) =>
$q->withActive()->withUserIds([$administratorUserId, $administeredUserId])
)
->with(['userUserGroups' => fn ($q) =>
$q->withActive()->withUserIds([$administratorUserId, $administeredUserId])
])
->whereHas('userUserGroups', function ($query) use ($administratorUserId, $administeredUserId) {
$query->withUserIds([$administratorUserId, $administeredUserId])
->withActive();
})
->get();

$administratorMap = [];
Expand Down
27 changes: 11 additions & 16 deletions classes/template/PKPTemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
use APP\submission\Submission;
use APP\template\TemplateManager;
use Exception;
use Illuminate\View\View;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Less_Parser;
use PKP\config\Config;
use PKP\context\Context;
Expand Down Expand Up @@ -1353,8 +1354,8 @@ public function fetch($template = null, $cache_id = null, $compile_id = null, $p

// If a blade view instance given or the template is a blade view
// just return the rendered content
if ($template instanceof View
|| (!str_contains($template, ".tpl") && view()->exists($template))
if ($template instanceof View
|| (!str_contains($template, '.tpl') && view()->exists($template))
) {
$this->shareTemplateVariables($this->getTemplateVars());

Expand Down Expand Up @@ -1524,16 +1525,8 @@ public function display($template = null, $cache_id = null, $compile_id = null,
if ($user) {
// Fetch user groups where the user is assigned
$userGroups = UserGroup::query()
->whereHas('userUserGroups', function ($query) use ($user) {
$query->where('user_id', $user->getId())
->where(function ($q) {
$q->whereNull('date_end')
->orWhere('date_end', '>', now());
})
->where(function ($q) {
$q->whereNull('date_start')
->orWhere('date_start', '<=', now());
});
->whereHas('userUserGroups', function (EloquentBuilder $query) use ($user) {
$query->withUserId($user->getId())->withActive();
})
->get();

Expand Down Expand Up @@ -1614,8 +1607,8 @@ public function display($template = null, $cache_id = null, $compile_id = null,

// If a blade view instance given or the template is a blade view
// just return the rendered content
if ($template instanceof View
|| (!str_contains($template, ".tpl") && view()->exists($template))
if ($template instanceof View
|| (!str_contains($template, '.tpl') && view()->exists($template))
) {
$this->shareTemplateVariables($this->getTemplateVars());

Expand All @@ -1624,7 +1617,7 @@ public function display($template = null, $cache_id = null, $compile_id = null,
: view($template)->render();
return;
}

// Actually display the template.
parent::display($template, $cache_id, $compile_id, $parent);
}
Expand Down Expand Up @@ -1930,6 +1923,8 @@ public function flush()

/**
* Call hooks from a template. (DEPRECATED: For new hooks, {run_hook} is preferred.
*
* @param null|mixed $smarty
*/
public function smartyCallHook($params, $smarty = null)
{
Expand Down
18 changes: 6 additions & 12 deletions classes/user/Report.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @defgroup lib_pkp_classes_user
*/
Expand All @@ -19,6 +20,7 @@

use APP\core\Application;
use APP\core\Request;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
use PKP\facades\Locale;
use PKP\userGroup\UserGroup;
Expand Down Expand Up @@ -79,8 +81,8 @@ private function _getHeadings(): array
__('common.updated'),
...$this->
_getUserGroups()
->map(fn (UserGroup $userGroup): string => $userGroup->getLocalizedData('name'))
->toArray()
->map(fn (UserGroup $userGroup): string => $userGroup->getLocalizedData('name'))
->toArray()
];
}

Expand All @@ -93,16 +95,8 @@ private function _getDataRow(User $user): array
{
// fetch user groups where the user is assigned
$userGroups = UserGroup::query()
->whereHas('userUserGroups', function ($query) use ($user) {
$query->where('user_id', $user->getId())
->where(function ($q) {
$q->whereNull('date_end')
->orWhere('date_end', '>', now());
})
->where(function ($q) {
$q->whereNull('date_start')
->orWhere('date_start', '<=', now());
});
->whereHas('userUserGroups', function (EloquentBuilder $query) use ($user) {
$query->withUserId($user->getId())->withActive();
})
->get();

Expand Down
4 changes: 2 additions & 2 deletions classes/user/form/RolesForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use APP\core\Application;
use APP\facades\Repo;
use APP\template\TemplateManager;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use PKP\user\User;
use PKP\userGroup\UserGroup;

Expand All @@ -44,8 +45,7 @@ public function fetch($request, $template = null, $display = false)
$templateMgr = TemplateManager::getManager($request);

$userGroupIds = UserGroup::query()
->withUserIds([$request->getUser()->getId()])
->whereHas('userUserGroups', function ($query) use ($request) {
->whereHas('userUserGroups', function (EloquentBuilder $query) use ($request) {
$query->withUserId($request->getUser()->getId())->withActive();
})
->get()
Expand Down
9 changes: 6 additions & 3 deletions classes/userGroup/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use APP\facades\Repo;
use Carbon\Carbon;
use DateInterval;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\LazyCollection;
Expand Down Expand Up @@ -191,8 +192,11 @@ public function getByRoleIds(array $roleIds, int $contextId, ?bool $default = nu
public function userUserGroups(int $userId, ?int $contextId = null, UserUserGroupStatus $status = UserUserGroupStatus::STATUS_ACTIVE): LazyCollection
{
$query = UserGroup::query()
->withUserIds([$userId])
->withUserUserGroupStatus($status->value);
->whereHas('userUserGroups', function (EloquentBuilder $query) use ($userId, $status) {
$query->withUserId($userId)
->when($status == UserUserGroupStatus::STATUS_ACTIVE, fn (EloquentBuilder $query) => $query->withActive())
->when($status == UserUserGroupStatus::STATUS_ENDED, fn (EloquentBuilder $query) => $query->withEnded());
});

if ($contextId !== null) {
$query->withContextIds([$contextId]);
Expand All @@ -209,7 +213,6 @@ public function getUserUserGroupsContextIds(int $userId): Collection
{
return UserGroup::query()
->withUserIds([$userId])
->withUserUserGroupStatus(UserUserGroupStatus::STATUS_ALL->value)
->masthead(true)
->pluck('context_id')
->unique();
Expand Down
27 changes: 0 additions & 27 deletions classes/userGroup/UserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,33 +246,6 @@ protected function scopeIsRecommendOnly(EloquentBuilder $builder, bool $isRecomm
return $builder->where('recommendOnly', $isRecommendOnly);
}

/**
* Scope a query to filter by UserUserGroupStatus.
*/
protected function scopeWithUserUserGroupStatus(EloquentBuilder $builder, string $status): EloquentBuilder
{
$currentDateTime = now();

if ($status === 'active') {
$builder->whereHas('userUserGroups', function (EloquentBuilder $q) use ($currentDateTime) {
$q->where(function (EloquentBuilder $q) use ($currentDateTime) {
$q->where('date_start', '<=', $currentDateTime)
->orWhereNull('date_start');
})->where(function (EloquentBuilder $q) use ($currentDateTime) {
$q->where('date_end', '>', $currentDateTime)
->orWhereNull('date_end');
});
});
} elseif ($status === 'ended') {
$builder->whereHas('userUserGroups', function (EloquentBuilder $q) use ($currentDateTime) {
$q->whereNotNull('date_end')
->where('date_end', '<=', $currentDateTime);
});
}
// Implement other statuses if needed
return $builder;
}

/**
* Scope a query to order by role ID.
*/
Expand Down
7 changes: 6 additions & 1 deletion controllers/grid/settings/roles/form/UserGroupForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use APP\core\Request;
use APP\facades\Repo;
use APP\template\TemplateManager;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use PKP\core\JSONMessage;
use PKP\db\DAORegistry;
use PKP\facades\Locale;
Expand Down Expand Up @@ -116,7 +117,11 @@ public function initData()
// Get a list of all settings-accessible user groups for the current user in
// order to prevent them from locking themselves out by disabling the only one.
$mySettingsAccessUserGroupIds = UserGroup::withContextIds([$this->getContextId()])
->withUserIds([Application::get()->getRequest()->getUser()->getId()])
->whereHas(
'userUserGroups',
fn (EloquentBuilder $q) =>
$q->withActive()->withUserId(Application::get()->getRequest()->getUser()->getId())
)
->get()
->filter(fn ($userGroup) => $userGroup->permitSettings)
->map(fn ($userGroup) => $userGroup->id)
Expand Down
Loading