Add voter to check for projects security

This commit is contained in:
2025-01-24 11:27:57 +01:00
parent 213971bc38
commit b0dc3de297
2 changed files with 40 additions and 2 deletions
@@ -25,8 +25,8 @@ use Symfony\Component\Validator\Constraints as Assert;
#[API\GetCollection()]
#[API\Post(security: 'is_granted("ROLE_USER")')]
#[API\Get()]
#[API\Patch(security: 'is_granted("PROJECT_EDIT")')]
#[API\Delete(security: 'is_granted("PROJECT_EDIT")')]
#[API\Patch(security: 'is_granted("PROJECT_EDIT", object)')]
#[API\Delete(security: 'is_granted("PROJECT_EDIT", object)')]
class ProjectApiResource
{
#[API\ApiProperty(identifier: true, writable: false)]
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Security\Voter;
use App\ApiResource\Project\ProjectApiResource;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class ProjectVoter extends Voter
{
use UserOwnedVoterTrait;
public const EDIT = 'PROJECT_EDIT';
public const VIEW = 'PROJECT_VIEW';
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::EDIT, self::VIEW])
&& $subject instanceof ProjectApiResource;
}
/**
* @param ProjectApiResource $subject
*/
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
switch ($attribute) {
case self::EDIT:
return $this->isOwnerOf($user, $subject);
case self::VIEW:
return true;
}
return false;
}
}