Skip to content

Commit

Permalink
feat(translations): Use settings to overwrite the label, icon and def…
Browse files Browse the repository at this point in the history
…ault

Make it possible to overwrite the default value label and the icon in the
selection
- use the settings or a default translation source
  • Loading branch information
erkenes committed Mar 1, 2021
1 parent 5147d99 commit 6e0f036
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 31 deletions.
76 changes: 70 additions & 6 deletions Classes/DataSources/RoleDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -41,14 +42,37 @@ 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
* @return array
*/
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) {
Expand All @@ -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
];
}
}
9 changes: 9 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
84 changes: 59 additions & 25 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="" product-name="Foo.Bar" source-language="en" datatype="plaintext">
<body>
<trans-unit id="DemoRole" xml:space="preserve" approved="yes">
<source>Demo</source>
</trans-unit>
</body>
</file>
</xliff>
```

0 comments on commit 6e0f036

Please sign in to comment.