Skip to content

Commit

Permalink
#201 Optimize RoleOptionsProvider to respect the default role. (#202)
Browse files Browse the repository at this point in the history
* #201 Optimize RoleOptionsProvider to respect the default role.

* #201 Revert method signature change to avoid BC breaks

* Fix pimcore version in tests

* #201 Make RoleOptionsProvider implement SelectOptionsProvider instead of MultiSelectOptionsProvider
  • Loading branch information
scrummer authored Aug 30, 2024
1 parent 979e71f commit c6f2be2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ jobs:
matrix:
php: [ 8.2 ]
symfony: [ ^6.2 ]
pimcore: [ ~11.0.0 ]
pimcore: [ ~11.3.0 ]
include:
- pimcore: ~11.0.0
- pimcore: ~11.3.0
template_tag: v11.0.0
steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ecs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ jobs:
matrix:
php: [ 8.2 ]
symfony: [ ^6.2 ]
pimcore: [ ~11.0.0 ]
pimcore: [ ~11.3.0 ]
include:
- pimcore: ~11.0.0
- pimcore: ~11.3.0
template_tag: v11.0.0
steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -114,4 +114,4 @@ jobs:
continue-on-error: true
run: |
bin/console cache:warmup --env=test
vendor/bin/ecs check ${{ github.workspace }}/lib/test-bundle/src/ --config ${{ github.workspace }}/lib/test-bundle/ecs.php
vendor/bin/ecs check ${{ github.workspace }}/lib/test-bundle/src/ --config ${{ github.workspace }}/lib/test-bundle/ecs.php
6 changes: 3 additions & 3 deletions .github/workflows/php-stan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ jobs:
matrix:
php: [ 8.2 ]
symfony: [ ^6.2 ]
pimcore: [ ~11.0.0 ]
pimcore: [ ~11.3.0 ]
include:
- pimcore: ~11.0.0
- pimcore: ~11.3.0
template_tag: v11.0.0
steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -113,4 +113,4 @@ jobs:
- name: Php Stan
run: |
bin/console cache:warmup --env=test
vendor/bin/phpstan analyse -c${{ github.workspace }}/lib/test-bundle/phpstan.neon -a ${{ github.workspace }}/lib/test-bundle/tests/_phpstan-bootstrap.php ${{ github.workspace }}/lib/test-bundle/src -l 4
vendor/bin/phpstan analyse -c${{ github.workspace }}/lib/test-bundle/phpstan.neon -a ${{ github.workspace }}/lib/test-bundle/tests/_phpstan-bootstrap.php ${{ github.workspace }}/lib/test-bundle/src -l 4
25 changes: 8 additions & 17 deletions src/CoreExtension/Provider/RoleOptionsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\MultiSelectOptionsProviderInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\SelectOptionsProviderInterface;

class RoleOptionsProvider implements MultiSelectOptionsProviderInterface
class RoleOptionsProvider implements SelectOptionsProviderInterface
{
protected array $originalRoles;
protected array $invalidRoles = [
Expand All @@ -19,26 +20,16 @@ public function __construct(array $systemRoles)

public function getOptions(array $context, Data $fieldDefinition): array
{
$roles = [];
$roles = [$this->getDefaultValue($context, $fieldDefinition)];

/*
* Get all unique roles
*/
foreach ($this->originalRoles as $originalRole => $inheritedRoles) {
foreach ($inheritedRoles as $inheritedRole) {
$roles[] = $inheritedRole;
}

$roles[] = $originalRole;
}

$result = [];

foreach (array_unique($roles) as $role) {
$result[] = ['key' => $role, 'value' => $role];
array_push($roles, $originalRole, ...$inheritedRoles);
}

return $result;
return array_map(
static fn($role): array => ['key' => $role, 'value' => $role],
array_unique($roles)
);
}

public function hasStaticOptions(array $context, Data $fieldDefinition): bool
Expand Down

0 comments on commit c6f2be2

Please sign in to comment.