Skip to content

Commit

Permalink
TASK: NeosUserRole enum to reduce amount of magic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaidelich committed Dec 20, 2024
1 parent e339da0 commit 5eccf4b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Neos\EventStore\Model\EventStream\EventStreamFilter;
use Neos\EventStore\Model\EventStream\ExpectedVersion;
use Neos\EventStore\Model\EventStream\VirtualStreamName;
use Neos\Neos\Domain\Model\NeosUserRole;
use Neos\Neos\Domain\Model\WorkspaceClassification;
use Neos\Neos\Domain\Model\WorkspaceRole;
use Neos\Neos\Domain\Model\WorkspaceRoleSubjectType;
Expand Down Expand Up @@ -719,18 +720,18 @@ public function migrateWorkspaceMetadataToWorkspaceService(\Closure $outputFn):
if ($workspaceName->isLive()) {
$roleAssignments[] = [
'subject_type' => WorkspaceRoleSubjectType::GROUP->value,
'subject' => 'Neos.Neos:LivePublisher',
'subject' => NeosUserRole::LIVE_PUBLISHER->value,
'role' => WorkspaceRole::COLLABORATOR->value,
];
$roleAssignments[] = [
'subject_type' => WorkspaceRoleSubjectType::GROUP->value,
'subject' => 'Neos.Flow:Everybody',
'subject' => NeosUserRole::EVERYBODY->value,
'role' => WorkspaceRole::VIEWER->value,
];
} elseif ($isInternalWorkspace) {
$roleAssignments[] = [
'subject_type' => WorkspaceRoleSubjectType::GROUP->value,
'subject' => 'Neos.Neos:AbstractEditor',
'subject' => NeosUserRole::ABSTRACT_EDITOR->value,
'role' => WorkspaceRole::COLLABORATOR->value,
];
} elseif ($isPrivateWorkspace) {
Expand Down
22 changes: 22 additions & 0 deletions Neos.Neos/Classes/Domain/Model/NeosUserRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Neos\Neos\Domain\Model;

use Neos\Neos\Domain\Service\WorkspaceService;

/**
* ...
*
* @api
*/
enum NeosUserRole : string
{
case EVERYBODY = 'Neos.Flow:Everybody';
case AUTHENTICATED_USER = 'Neos.Flow:AuthenticatedUser';
case ADMINISTRATOR = 'Neos.Neos:Administrator';
case ABSTRACT_EDITOR = 'Neos.Neos:AbstractEditor';
case EDITOR = 'Neos.Neos:Editor';
case LIVE_PUBLISHER = 'Neos.Neos:LivePublisher';
}
6 changes: 3 additions & 3 deletions Neos.Neos/Classes/Domain/Model/WorkspaceRoleAssignments.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public static function createForLiveWorkspace(): self
{
return new self(
WorkspaceRoleAssignment::createForGroup(
'Neos.Neos:LivePublisher',
NeosUserRole::LIVE_PUBLISHER->value,
WorkspaceRole::COLLABORATOR
),
WorkspaceRoleAssignment::createForGroup(
'Neos.Flow:Everybody',
NeosUserRole::EVERYBODY->value,
WorkspaceRole::VIEWER
)
);
Expand All @@ -76,7 +76,7 @@ public static function createForSharedWorkspace(UserId $userId): self
WorkspaceRole::MANAGER,
),
WorkspaceRoleAssignment::createForGroup(
'Neos.Neos:AbstractEditor',
NeosUserRole::ABSTRACT_EDITOR->value,
WorkspaceRole::COLLABORATOR,
)
);
Expand Down
9 changes: 5 additions & 4 deletions Neos.Neos/Classes/Domain/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Neos\Flow\Session\SessionManager;
use Neos\Flow\Utility\Now;
use Neos\Neos\Domain\Exception;
use Neos\Neos\Domain\Model\NeosUserRole;
use Neos\Neos\Domain\Model\User;
use Neos\Neos\Domain\Model\UserId;
use Neos\Neos\Domain\Repository\UserRepository;
Expand Down Expand Up @@ -321,7 +322,7 @@ public function addUser(
$authenticationProviderName = null
) {
if ($roleIdentifiers === null) {
$roleIdentifiers = ['Neos.Neos:Editor'];
$roleIdentifiers = [NeosUserRole::EDITOR->value];
}
$roleIdentifiers = $this->normalizeRoleIdentifiers($roleIdentifiers);
$account = $this->accountFactory->createAccountWithPassword(
Expand Down Expand Up @@ -663,7 +664,7 @@ public function deactivateUser(User $user): void
*/
public function currentUserIsAdministrator(): bool
{
return $this->securityContext->hasRole('Neos.Neos:Administrator');
return $this->securityContext->hasRole(NeosUserRole::ADMINISTRATOR->value);
}

/**
Expand Down Expand Up @@ -736,8 +737,8 @@ protected function normalizeRoleIdentifier($roleIdentifier)
public function getAllRoles(User $user): array
{
$roles = [
'Neos.Flow:Everybody' => $this->policyService->getRole('Neos.Flow:Everybody'),
'Neos.Flow:AuthenticatedUser' => $this->policyService->getRole('Neos.Flow:AuthenticatedUser')
NeosUserRole::EVERYBODY->value => $this->policyService->getRole(NeosUserRole::EVERYBODY->value),
NeosUserRole::AUTHENTICATED_USER->value => $this->policyService->getRole(NeosUserRole::AUTHENTICATED_USER->value)
];

/** @var Account $account */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Neos\Flow\Security\Context;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Flow\Security\Policy\Role;
use Neos\Neos\Domain\Model\NeosUserRole;
use Neos\Neos\Domain\Model\NodePermissions;
use Neos\Neos\Domain\Model\UserId;
use Neos\Neos\Domain\Model\WorkspacePermissions;
Expand All @@ -33,8 +34,6 @@
#[Flow\Scope('singleton')]
final readonly class ContentRepositoryAuthorizationService
{
private const ROLE_NEOS_ADMINISTRATOR = 'Neos.Neos:Administrator';

public function __construct(
private WorkspaceMetadataAndRoleRepository $metadataAndRoleRepository,
private PolicyService $policyService,
Expand Down Expand Up @@ -63,7 +62,7 @@ public function getWorkspacePermissions(ContentRepositoryId $contentRepositoryId
* We hardcode the check against administrators to always grant manage permissions. This is done to allow administrators to fix permissions of all workspaces.
* We don't allow all rights like read and write. Admins should be able to grant themselves permissions to write to other personal workspaces, but they should not have this permission automagically.
*/
$userIsAdministrator = in_array(self::ROLE_NEOS_ADMINISTRATOR, $roleIdentifiers, true);
$userIsAdministrator = in_array(NeosUserRole::ADMINISTRATOR->value, $roleIdentifiers, true);
$userWorkspaceRole = $this->metadataAndRoleRepository->getMostPrivilegedWorkspaceRoleForSubjects($contentRepositoryId, $workspaceName, WorkspaceRoleSubjects::fromArray($subjects));
if ($userWorkspaceRole === null) {
if ($userIsAdministrator) {
Expand Down

0 comments on commit 5eccf4b

Please sign in to comment.