Skip to content

Commit

Permalink
pkp#10506 Fix User Group filter by context IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Nov 29, 2024
1 parent 0bc8342 commit e6186ed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
4 changes: 2 additions & 2 deletions classes/security/authorization/UserRolesRequiredPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function effect()
$userGroups = UserGroup::withUserIds([$user->getId()])
->withContextIds($context ? [$context->getId(), Application::SITE_CONTEXT_ID] : [Application::SITE_CONTEXT_ID])
->get()
->toArray();
->all();

$roleIds = array_map(fn ($userGroup) => $userGroup->getRoleId(), $userGroups);
$roleIds = array_map(fn ($userGroup) => $userGroup->roleId, $userGroups);
$this->addAuthorizedContextObject(Application::ASSOC_TYPE_USER_ROLES, $roleIds);
$this->addAuthorizedContextObject(Application::ASSOC_TYPE_USER_GROUP, $userGroups);

Expand Down
79 changes: 42 additions & 37 deletions classes/userGroup/UserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@
* @brief Eloquent Model for UserGroup
*/

namespace PKP\userGroup;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use PKP\core\traits\ModelWithSettings;
use PKP\stageAssignment\StageAssignment;
use PKP\userGroup\relationships\UserUserGroup;
use PKP\userGroup\relationships\UserGroupStage;
use PKP\facades\Locale;
use PKP\plugins\Hook;
use PKP\facades\Repo;
use PKP\services\PKPSchemaService;
use PKP\core\PKPApplication;
use APP\core\Application;

namespace PKP\userGroup;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use PKP\core\PKPApplication;
use PKP\core\traits\ModelWithSettings;
use PKP\facades\Locale;
use PKP\facades\Repo;
use PKP\plugins\Hook;
use PKP\services\PKPSchemaService;
use PKP\stageAssignment\StageAssignment;
use PKP\userGroup\relationships\UserGroupStage;
use PKP\userGroup\relationships\UserUserGroup;

class UserGroup extends Model
{

use ModelWithSettings;


Expand Down Expand Up @@ -151,12 +148,30 @@ protected function scopeWithContextIds(EloquentBuilder $builder, $contextIds): E
if (empty($contextIds)) {
return $builder;
}

if (!is_array($contextIds)) {
$contextIds = [$contextIds];
}

return $builder->whereIn('context_id', $contextIds);

$filteredIds = [];
$siteWide = false;
foreach ($contextIds as $contextId) {
if ($contextId == PKPApplication::SITE_CONTEXT_ID) {
$siteWide = true;
continue;
}

$filteredIds[] = $contextId;
}

return $builder
->when(!empty($filteredIds), fn (EloquentBuilder $builder) => $builder->whereIn('context_id', $filteredIds))
->when($siteWide, fn (EloquentBuilder $builder) => $builder
->when(
empty($filteredIds),
fn (EloquentBuilder $builder) => $builder->whereNull('context_id'),
fn (EloquentBuilder $builder) => $builder->orWhereNull('context_id')
));
}

/**
Expand All @@ -167,7 +182,7 @@ protected function scopeWithUserGroupIds(EloquentBuilder $builder, $userGroupIds
if (!is_array($userGroupIds)) {
$userGroupIds = [$userGroupIds];
}

return $builder->whereIn('user_group_id', $userGroupIds);
}

Expand All @@ -179,7 +194,7 @@ protected function scopeWithRoleIds(EloquentBuilder $builder, $roleIds): Eloquen
if (!is_array($roleIds)) {
$roleIds = [$roleIds];
}

return $builder->whereIn('role_id', $roleIds);
}

Expand Down Expand Up @@ -349,20 +364,17 @@ protected function scopeWithActiveUserCount(EloquentBuilder $builder, ?int $cont
* Ensure casts are string values.
*
* @param array $casts
* @return array
*/
protected function ensureCastsAreStringValues($casts): array
{
return array_map(fn($cast) => (string) $cast, $casts);
return array_map(fn ($cast) => (string) $cast, $casts);
}


/**
* Scope a query to filter by publication IDs.
*
* @param EloquentBuilder $builder
* @param array<int> $publicationIds Array of publication IDs to filter by.
* @return EloquentBuilder
*/
protected function scopeWithPublicationIds(EloquentBuilder $builder, array $publicationIds): EloquentBuilder
{
Expand All @@ -374,8 +386,6 @@ protected function scopeWithPublicationIds(EloquentBuilder $builder, array $publ
/**
* Set the name attribute.
*
* @param mixed $value
* @return void
*/
public function setNameAttribute($value): void
{
Expand All @@ -388,8 +398,6 @@ public function setNameAttribute($value): void
/**
* Set the abbrev attribute.
*
* @param mixed $value
* @return void
*/
public function setAbbrevAttribute($value): void
{
Expand All @@ -402,8 +410,6 @@ public function setAbbrevAttribute($value): void
/**
* Localize non-localized data.
*
* @param string $value
* @return array
*/
protected function localizeNonLocalizedData(string $value): array
{
Expand All @@ -413,9 +419,6 @@ protected function localizeNonLocalizedData(string $value): array
/**
* Find a UserGroup by ID and optional context ID.
*
* @param int $id
* @param int|null $contextId
* @return self|null
*/
public static function findById(int $id, ?int $contextId = null): ?self
{
Expand All @@ -431,8 +434,10 @@ public static function findById(int $id, ?int $contextId = null): ?self
/**
* Save the model to the database.
*
* @param array $options
* @return bool
*
* @hook UserGroup::add [[$this]]
* @hook UserGroup::edit [[$this]]
*/
public function save(array $options = [])
{
Expand Down Expand Up @@ -463,7 +468,7 @@ public function save(array $options = [])
/**
* Booted method to handle model events.
*
* @return void
* @hook UserGroup::delete::before [[$userGroup]]
*/
protected static function booted()
{
Expand Down

0 comments on commit e6186ed

Please sign in to comment.