-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added security layer on NodesSources form type for each node-ty…
…pe-field (NodeTypeFieldVoter)
- Loading branch information
1 parent
899b397
commit 29b0332
Showing
3 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
lib/RoadizCoreBundle/src/Security/Authorization/Voter/NodeTypeFieldVoter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Security\Authorization\Voter; | ||
|
||
use RZ\Roadiz\CoreBundle\Entity\NodeTypeField; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
final class NodeTypeFieldVoter extends Voter | ||
{ | ||
public const VIEW = 'VIEW'; | ||
|
||
public function __construct( | ||
private readonly Security $security | ||
) { | ||
} | ||
|
||
protected function supports(string $attribute, mixed $subject): bool | ||
{ | ||
if (!\in_array($attribute, [self::VIEW])) { | ||
return false; | ||
} | ||
return $subject instanceof NodeTypeField; | ||
} | ||
|
||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool | ||
{ | ||
$user = $token->getUser(); | ||
|
||
if (!$user instanceof UserInterface) { | ||
// the user must be logged in; if not, deny access | ||
return false; | ||
} | ||
|
||
return match ($attribute) { | ||
self::VIEW => $this->canView($subject, $user), | ||
default => throw new \LogicException('This code should not be reached!') | ||
}; | ||
} | ||
|
||
private function canView(NodeTypeField $field, UserInterface $user): bool | ||
{ | ||
if ($field->isNodes() && !$this->security->isGranted(NodeVoter::SEARCH)) { | ||
return false; | ||
} | ||
if ($field->isDocuments() && !$this->security->isGranted('ROLE_ACCESS_DOCUMENTS')) { | ||
return false; | ||
} | ||
if ($field->isUser() && !$this->security->isGranted('ROLE_ACCESS_USERS')) { | ||
return false; | ||
} | ||
if ($field->isCustomForms() && !$this->security->isGranted('ROLE_ACCESS_CUSTOMFORMS')) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters