Skip to content

Commit bf937dc

Browse files
committed
#12017 standardize getting active user roles
1 parent 363b3d5 commit bf937dc

File tree

5 files changed

+24
-46
lines changed

5 files changed

+24
-46
lines changed

classes/core/PKPPageRouter.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,9 @@ public function getHomeUrl(PKPRequest $request): string
410410
// fetch user groups for the user in the current context
411411
$userGroups = UserGroup::query()
412412
->where('context_id', $context->getId())
413-
->whereHas(
414-
'userUserGroups',
415-
fn (Builder $query) => $query->where('user_id', $userId)
416-
->where(fn (Builder $q) => $q->whereNull('date_end')->orWhere('date_end', '>', now()))
417-
->where(fn (Builder $q) => $q->whereNull('date_start')->orWhere('date_start', '<=', now()))
418-
)
413+
->whereHas('userUserGroups', function (Builder $query) use ($userId) {
414+
$query->withUserId($userId)->withActive();
415+
})
419416
->get();
420417
if ($userGroups->isEmpty() || ($userGroups->count() == 1 && $userGroups->first()->role_id == Role::ROLE_ID_READER)) {
421418
return $request->url(null, 'index');

classes/security/RoleDAO.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,8 @@ public function getByUserIdGroupedByContext(int $userId): array
8989
{
9090
$userGroups = UserGroup::query()
9191
->with('userUserGroups')
92-
->whereHas('userUserGroups', function ($query) use ($userId) {
93-
$query->where('user_id', $userId)
94-
->where(function ($q) {
95-
$q->whereNull('date_end')
96-
->orWhere('date_end', '>', now());
97-
})
98-
->where(function ($q) {
99-
$q->whereNull('date_start')
100-
->orWhere('date_start', '<=', now());
101-
});
92+
->whereHas('userUserGroups', function (Builder $query) use ($userId) {
93+
$query->withUserId($userId)->withActive();
10294
})
10395
->get();
10496

classes/template/PKPTemplateManager.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
use APP\submission\Submission;
3232
use APP\template\TemplateManager;
3333
use Exception;
34-
use Illuminate\View\View;
3534
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
35+
use Illuminate\Database\Query\Builder;
3636
use Illuminate\Support\Str;
37+
use Illuminate\View\View;
3738
use Less_Parser;
3839
use PKP\config\Config;
3940
use PKP\context\Context;
@@ -1353,8 +1354,8 @@ public function fetch($template = null, $cache_id = null, $compile_id = null, $p
13531354

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

@@ -1524,16 +1525,8 @@ public function display($template = null, $cache_id = null, $compile_id = null,
15241525
if ($user) {
15251526
// Fetch user groups where the user is assigned
15261527
$userGroups = UserGroup::query()
1527-
->whereHas('userUserGroups', function ($query) use ($user) {
1528-
$query->where('user_id', $user->getId())
1529-
->where(function ($q) {
1530-
$q->whereNull('date_end')
1531-
->orWhere('date_end', '>', now());
1532-
})
1533-
->where(function ($q) {
1534-
$q->whereNull('date_start')
1535-
->orWhere('date_start', '<=', now());
1536-
});
1528+
->whereHas('userUserGroups', function (Builder $query) use ($user) {
1529+
$query->withUserId($user->getId())->withActive();
15371530
})
15381531
->get();
15391532

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

16151608
// If a blade view instance given or the template is a blade view
16161609
// just return the rendered content
1617-
if ($template instanceof View
1618-
|| (!str_contains($template, ".tpl") && view()->exists($template))
1610+
if ($template instanceof View
1611+
|| (!str_contains($template, '.tpl') && view()->exists($template))
16191612
) {
16201613
$this->shareTemplateVariables($this->getTemplateVars());
16211614

@@ -1624,7 +1617,7 @@ public function display($template = null, $cache_id = null, $compile_id = null,
16241617
: view($template)->render();
16251618
return;
16261619
}
1627-
1620+
16281621
// Actually display the template.
16291622
parent::display($template, $cache_id, $compile_id, $parent);
16301623
}
@@ -1930,6 +1923,8 @@ public function flush()
19301923

19311924
/**
19321925
* Call hooks from a template. (DEPRECATED: For new hooks, {run_hook} is preferred.
1926+
*
1927+
* @param null|mixed $smarty
19331928
*/
19341929
public function smartyCallHook($params, $smarty = null)
19351930
{

classes/user/Report.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @defgroup lib_pkp_classes_user
45
*/
@@ -19,6 +20,7 @@
1920

2021
use APP\core\Application;
2122
use APP\core\Request;
23+
use Illuminate\Database\Query\Builder;
2224
use Illuminate\Support\Collection;
2325
use PKP\facades\Locale;
2426
use PKP\userGroup\UserGroup;
@@ -79,8 +81,8 @@ private function _getHeadings(): array
7981
__('common.updated'),
8082
...$this->
8183
_getUserGroups()
82-
->map(fn (UserGroup $userGroup): string => $userGroup->getLocalizedData('name'))
83-
->toArray()
84+
->map(fn (UserGroup $userGroup): string => $userGroup->getLocalizedData('name'))
85+
->toArray()
8486
];
8587
}
8688

@@ -93,16 +95,8 @@ private function _getDataRow(User $user): array
9395
{
9496
// fetch user groups where the user is assigned
9597
$userGroups = UserGroup::query()
96-
->whereHas('userUserGroups', function ($query) use ($user) {
97-
$query->where('user_id', $user->getId())
98-
->where(function ($q) {
99-
$q->whereNull('date_end')
100-
->orWhere('date_end', '>', now());
101-
})
102-
->where(function ($q) {
103-
$q->whereNull('date_start')
104-
->orWhere('date_start', '<=', now());
105-
});
98+
->whereHas('userUserGroups', function (Builder $query) use ($user) {
99+
$query->withUserId($user->getId())->withActive();
106100
})
107101
->get();
108102

classes/user/form/RolesForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use APP\core\Application;
2020
use APP\facades\Repo;
2121
use APP\template\TemplateManager;
22+
use Illuminate\Database\Query\Builder;
2223
use PKP\user\User;
2324
use PKP\userGroup\UserGroup;
2425

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

4647
$userGroupIds = UserGroup::query()
47-
->withUserIds([$request->getUser()->getId()])
48-
->whereHas('userUserGroups', function ($query) use ($request) {
48+
->whereHas('userUserGroups', function (Builder $query) use ($request) {
4949
$query->withUserId($request->getUser()->getId())->withActive();
5050
})
5151
->get()

0 commit comments

Comments
 (0)