Skip to content

Commit

Permalink
Merge pull request from GHSA-pcpm-vc4v-cmvx
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou authored Nov 10, 2022
1 parent 6bb3849 commit 16590ec
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformAdminUiBundle\\": "src/bundle/",
"EzSystems\\EzPlatformAdminUi\\": "src/lib/"
"EzSystems\\EzPlatformAdminUi\\": "src/lib/",
"Ibexa\\AdminUi\\": "src/lib/"
}
},
"autoload-dev": {
Expand Down
1 change: 1 addition & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ imports:
- { resource: services/forms.yml }
- { resource: services/query_types.yaml }
- { resource: services/search.yml }
- { resource: services/role_form_mappers.yml }

services:
_defaults:
Expand Down
19 changes: 19 additions & 0 deletions src/bundle/Resources/config/services/role_form_mappers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
Ibexa\AdminUi\Limitation\Mapper\MemberOfLimitationMapper:
parent: ezrepoforms.limitation.form_mapper.multiple_selection
arguments:
$userService: '@ezpublish.api.service.user'
$repository: '@ezpublish.api.repository'
$searchService: '@ezpublish.api.service.search'
$translator: '@translator'
tags:
- { name: ez.limitation.formMapper, limitationType: MemberOf }
- { name: ez.limitation.valueMapper, limitationType: MemberOf }

Ibexa\AdminUi\Limitation\Mapper\RoleLimitationMapper:
parent: ezrepoforms.limitation.form_mapper.multiple_selection
arguments:
$roleService: '@ezpublish.api.service.role'
tags:
- { name: ez.limitation.formMapper, limitationType: Role }
- { name: ez.limitation.valueMapper, limitationType: Role }
5 changes: 5 additions & 0 deletions src/bundle/Resources/translations/ezrepoforms_role.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="e797dbd775ecc5788f41ceeb960b70a9d7a116ca" resname="policy.limitation.member_of.self_user_group">
<source>Self</source>
<target state="new">Self</target>
<note>key: policy.limitation.member_of.self_user_group</note>
</trans-unit>
<trans-unit id="34ba9078fe178bd49df6ada9d8357455ee8ff7b3" resname="policy_create.save">
<source>Update</source>
<target state="new">Update</target>
Expand Down
112 changes: 112 additions & 0 deletions src/lib/Limitation/Mapper/MemberOfLimitationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Limitation\Mapper;

use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\UserService;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\ContentName;
use eZ\Publish\API\Repository\Values\User\Limitation;
use EzSystems\RepositoryForms\Limitation\LimitationValueMapperInterface;
use EzSystems\RepositoryForms\Limitation\Mapper\MultipleSelectionBasedMapper;
use Ibexa\Core\Limitation\MemberOfLimitationType;
use Symfony\Component\Translation\TranslatorInterface;

final class MemberOfLimitationMapper extends MultipleSelectionBasedMapper implements LimitationValueMapperInterface
{
/** @var \eZ\Publish\API\Repository\UserService */
private $userService;

/** @var \eZ\Publish\API\Repository\Repository */
private $repository;

/** @var \eZ\Publish\API\Repository\SearchService */
private $searchService;

/** @var \Symfony\Component\Translation\TranslatorInterface */
private $translator;

public function __construct(
UserService $userService,
Repository $repository,
SearchService $searchService,
TranslatorInterface $translator
) {
$this->userService = $userService;
$this->repository = $repository;
$this->searchService = $searchService;
$this->translator = $translator;
}

protected function getSelectionChoices(): array
{
$userGroups = $this->loadUserGroups();
$choices = [];
$choices[MemberOfLimitationType::SELF_USER_GROUP] = $this->getSelfUserGroupLabel();

foreach ($userGroups as $userGroup) {
$choices[$userGroup->id] = $userGroup->getName();
}

return $choices;
}

public function mapLimitationValue(Limitation $limitation): array
{
$values = [];
foreach ($limitation->limitationValues as $groupId) {
if ($groupId === MemberOfLimitationType::SELF_USER_GROUP) {
$values[] = $this->getSelfUserGroupLabel();
continue;
}
$values[] = $this->userService->loadUserGroup($groupId)->getName();
}

return $values;
}

/**
* @return \eZ\Publish\API\Repository\Values\User\UserGroup[]
*/
private function loadUserGroups(): array
{
return $this->repository->sudo(function () {
$query = new Query();
$query->filter = new ContentTypeIdentifier('user_group');
$query->offset = 0;
$query->limit = 100;
$query->performCount = true;
$query->sortClauses[] = new ContentName();

$groups = [];
do {
$results = $this->searchService->findContent($query);
foreach ($results->searchHits as $hit) {
$groups[] = $this->userService->loadUserGroup($hit->valueObject->id);
}

$query->offset += $query->limit;
} while ($query->offset < $results->totalCount);

return $groups;
});
}

private function getSelfUserGroupLabel(): string
{
return $this->translator->trans(
/** @Desc("Self") */
'policy.limitation.member_of.self_user_group',
[],
'ezrepoforms_role'
);
}
}
47 changes: 47 additions & 0 deletions src/lib/Limitation/Mapper/RoleLimitationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Limitation\Mapper;

use eZ\Publish\API\Repository\RoleService;
use eZ\Publish\API\Repository\Values\User\Limitation;
use EzSystems\RepositoryForms\Limitation\LimitationValueMapperInterface;
use EzSystems\RepositoryForms\Limitation\Mapper\MultipleSelectionBasedMapper;

final class RoleLimitationMapper extends MultipleSelectionBasedMapper implements LimitationValueMapperInterface
{
/** @var \eZ\Publish\API\Repository\RoleService */
private $roleService;

public function __construct(
RoleService $roleService
) {
$this->roleService = $roleService;
}

protected function getSelectionChoices(): array
{
$choices = [];
foreach ($this->roleService->loadRoles() as $role) {
$choices[$role->id] = $role->identifier;
}

return $choices;
}

public function mapLimitationValue(Limitation $limitation): array
{
$values = [];

foreach ($limitation->limitationValues as $roleId) {
$values[] = $this->roleService->loadRole((int)$roleId)->identifier;
}

return $values;
}
}

0 comments on commit 16590ec

Please # to comment.