diff --git a/Classes/DataSources/RoleDataSource.php b/Classes/DataSources/RoleDataSource.php index d9fed69..4469b73 100644 --- a/Classes/DataSources/RoleDataSource.php +++ b/Classes/DataSources/RoleDataSource.php @@ -15,6 +15,7 @@ use Neos\Neos\Service\DataSource\AbstractDataSource; use Neos\ContentRepository\Domain\Model\NodeInterface; use Neos\Flow\Annotations as Flow; +use Neos\Flow\I18n\Translator; class RoleDataSource extends AbstractDataSource { @@ -41,6 +42,19 @@ class RoleDataSource extends AbstractDataSource */ protected $policyService; + /** + * @Flow\Inject + * @var Translator + */ + protected $translator; + + /** + * @Flow\InjectConfiguration(package="PunktDe.NodeRestrictions") + * @var array + */ + protected $roleSettings; + + /** * @param NodeInterface|null $node * @param array $arguments @@ -48,7 +62,17 @@ class RoleDataSource extends AbstractDataSource */ public function getData(NodeInterface $node = null, array $arguments = []) { - $roles = ['' => ['label' => 'Not restricted']]; + if (key_exists('translation', $this->roleSettings)) { + $defaultTranslationSetting = $this->roleSettings['translation']; + + $defaultValue = $this->translator->translateById($defaultTranslationSetting['id'], [], null, null, $defaultTranslationSetting['source'], $defaultTranslationSetting['package']); + } + + if (!$defaultValue) { + $defaultValue = 'Not restricted'; + } + + $roles = ['' => ['label' => $defaultValue]]; $matchPatternArray = function ($patternArray, $identifier): bool { foreach ($patternArray as $pattern) { @@ -61,13 +85,53 @@ public function getData(NodeInterface $node = null, array $arguments = []) foreach ($this->policyService->getRoles() as $role) { if (!$matchPatternArray($this->excludedPackages, $role->getPackageKey()) && !$matchPatternArray($this->excludedRoles, $role->getIdentifier())) { - $roles[$role->getIdentifier()] = [ - 'label' => $role->getName(), - 'icon' => 'icon-users' - ]; + $roles[$role->getIdentifier()] = $this->getDataByFullName($role->getPackageKey(), $role->getName()); } } return $roles; } -} + + /** + * Get the translation and icon of a role by his name and package key + * + * @param string $packageName + * @param string $roleName + * @return array + */ + protected function getDataByFullName(string $packageName, string $roleName): array + { + $translationSettings = $this->roleSettings['overwriteRoles']; + $translation = null; + $icon = 'icon-users'; + $fullRoleName = $packageName . ':' . $roleName; + + // Get the translation from the settings + if (key_exists($fullRoleName, $translationSettings)) { + $translate = $translationSettings[$fullRoleName]; + + if (key_exists('source', $translate) && key_exists('package', $translate)) { + $translation = $this->translator->translateById($roleName, [], null, null, $translate['source'], $translate['package']); + } + + if (key_exists('icon', $translate)) { + $icon = $translate['icon']; + } + } + + if (!$translation) { + // Get the default translation by a static source + $translation = $this->translator->translateById($roleName, [], null, null, 'Roles', $packageName); + + // Fallback: Use the role name + if (!$translation) { + $translation = $roleName; + } + } + + return [ + 'label' => $translation, + 'icon' => $icon + ]; + } +} \ No newline at end of file diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 3b84e52..e3385c2 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -5,3 +5,12 @@ PunktDe: - Neos.Setup excludeSpecificRoles: - Neos.Neos:Editor +# overwriteRoles: +# 'Foo.Bar:DemoRole': +# source: 'Main' +# package: 'Foo.Bar' +# icon: 'fas fa-cogs' +# translation: +# id: 'defaultValue' +# source: 'Roles' +# package: 'PunktDe.NodeRestrictions' \ No newline at end of file diff --git a/Readme.md b/Readme.md index 98a0498..58d12ea 100644 --- a/Readme.md +++ b/Readme.md @@ -13,33 +13,67 @@ The packages enables the editor to restrict the access of a node and its subnode Add roles and privilege targets to your projects `Policy.yaml` using the ReadNodePrivilege. In this example, we use the package [Flowpack.Neos.FrontendLogin](https://github.com/Flowpack/Flowpack.Neos.FrontendLogin) for authentication, so the role inherits from `Flowpack.Neos.FrontendLogin:User`. - privilegeTargets: - 'PunktDe\NodeRestrictions\Security\Authorization\Privilege\Node\ReadNodePrivilege': - 'Vendor.Customer:RestrictNodeToRole1': - matcher: 'nodePropertyIs("accessRestriction", "Vendor.Customer:Role1") || parentNodePropertyIs("accessRestriction", "Vendor.Customer:Role1")' +```yaml +privilegeTargets: + 'PunktDe\NodeRestrictions\Security\Authorization\Privilege\Node\ReadNodePrivilege': + 'Vendor.Customer:RestrictNodeToRole1': + matcher: 'nodePropertyIs("accessRestriction", "Vendor.Customer:Role1") || parentNodePropertyIs("accessRestriction", "Vendor.Customer:Role1")' - roles: - 'Vendor.Customer:Role1': - parentRoles: ['Flowpack.Neos.FrontendLogin:User'] - privileges: - - - privilegeTarget: 'Vendor.Customer:RestrictNodeToRole1' - permission: GRANT - - 'Neos.Neos:AbstractEditor': - privileges: - - - privilegeTarget: 'Vendor.Customer:RestrictNodeToRole1' - permission: GRANT - +roles: + 'Vendor.Customer:Role1': + parentRoles: ['Flowpack.Neos.FrontendLogin:User'] + privileges: + - + privilegeTarget: 'Vendor.Customer:RestrictNodeToRole1' + permission: GRANT + + 'Neos.Neos:AbstractEditor': + privileges: + - + privilegeTarget: 'Vendor.Customer:RestrictNodeToRole1' + permission: GRANT +``` + ### Exclude Roles from Selection Some system roles, especially the backend roles shouldn't be displayed in the backend selector. You can exclude them using the setting. Globbing is supported.: - PunktDe: - NodeRestrictions: - excludeRolesFromPackages: - - Neos.Neos - - Flowpack.* - excludeSpecificRoles: - - Neos.Neos:Editor +```yaml +PunktDe: + NodeRestrictions: + excludeRolesFromPackages: + - Neos.Neos + - Flowpack.* + excludeSpecificRoles: + - Neos.Neos:Editor +``` + +### Translate and change the icon of a Role + +You can translate and change the icon of a role by adjusting the settings. + +```yaml +PunktDe: + NodeRestrictions: + overwriteRoles: + 'Foo.Bar:DemoRole': + source: 'Main' + package: 'Foo.Bar' + icon: 'fas fa-cogs' +``` + +Or if you created the role by your own you can use a XLF-File in the package of the role (package key name). +The Name of the XLF-File is `Roles.xlf`. + +```xml + + + + + + Demo + + + + +``` \ No newline at end of file