Skip to content

Commit

Permalink
Merge pull request #1283 from tarlepp/refactor/isgranted-attribute-in…
Browse files Browse the repository at this point in the history
…stead-of-security-attribute

Refactor/Attributes - Use `IsGranted` instead of `Security`
  • Loading branch information
tarlepp committed Jun 17, 2021
2 parents ec5db13 + f356538 commit 9364177
Show file tree
Hide file tree
Showing 56 changed files with 166 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/ArgumentResolver/LoggedInUserValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Example how to use this within your controller;
*
* #[Route(path: 'some-path')]
* #[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
* #[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
* public function someMethod(\App\Entity\User $loggedInUser): Response
* {
* ...
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/ApiKeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
use App\Rest\Controller;
use App\Rest\Traits\Actions;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

/**
* Class ApiKeyController
Expand All @@ -31,7 +32,7 @@
#[Route(
path: '/api_key',
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
class ApiKeyController extends Controller
{
use Actions\Root\CountAction;
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Profile/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use App\Entity\UserGroup;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Serializer\SerializerInterface;

/**
Expand Down Expand Up @@ -88,7 +89,7 @@ public function __construct(
path: '/profile/groups',
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
public function __invoke(User $loggedInUser): JsonResponse
{
return new JsonResponse(
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Profile/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use JsonException;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Serializer\SerializerInterface;

/**
Expand Down Expand Up @@ -78,7 +79,7 @@ public function __construct(
path: '/profile',
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
public function __invoke(User $loggedInUser): JsonResponse
{
/** @var array<string, string|array<string, string>> $output */
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Profile/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
use App\Entity\User;
use App\Security\RolesService;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

/**
* Class RolesController
Expand Down Expand Up @@ -69,7 +70,7 @@ public function __construct(
path: '/profile/roles',
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
public function __invoke(User $loggedInUser): JsonResponse
{
return new JsonResponse($this->rolesService->getInheritedRoles($loggedInUser->getRoles()));
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Role/FindOneRoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use App\Rest\Controller;
use App\Rest\Traits\Methods;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Throwable;

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(
requirements: ['role' => '^ROLE_\w+$'],
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
public function __invoke(Request $request, string $role): Response
{
return $this->findOneMethod($request, $role);
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Role/InheritedRolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
use App\Resource\RoleResource;
use App\Security\RolesService;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

/**
* Class InheritedRolesController
Expand Down Expand Up @@ -76,7 +77,7 @@ public function __construct(
requirements: ['role' => '^ROLE_\w+$'],
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
#[ParamConverter(
data: 'role',
class: RoleResource::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Role/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use App\Rest\Controller;
use App\Rest\Traits\Actions;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

/**
* Class RoleController
Expand All @@ -28,7 +29,7 @@
#[Route(
path: '/role',
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
class RoleController extends Controller
{
use Actions\Admin\CountAction;
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/User/AttachUserGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use App\Entity\UserGroup;
use App\Resource\UserGroupResource;
use App\Resource\UserResource;
use App\Security\RolesService;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -133,7 +134,7 @@ public function __construct(
],
methods: [Request::METHOD_POST],
)]
#[Security('is_granted("ROLE_ROOT")')]
#[IsGranted(RolesService::ROLE_ROOT)]
#[ParamConverter(
data: 'user',
class: UserResource::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/User/DeleteUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
use App\Resource\UserResource;
use App\Rest\Controller;
use App\Rest\Traits\Methods;
use App\Security\RolesService;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct(
],
methods: [Request::METHOD_DELETE],
)]
#[Security('is_granted("ROLE_ROOT")')]
#[IsGranted(RolesService::ROLE_ROOT)]
#[ParamConverter(
data: 'requestUser',
class: UserResource::class,
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/User/DetachUserGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
use App\Entity\UserGroup;
use App\Resource\UserGroupResource;
use App\Resource\UserResource;
use App\Security\RolesService;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;
Expand Down Expand Up @@ -120,7 +120,7 @@ public function __construct(
],
methods: [Request::METHOD_DELETE],
)]
#[Security('is_granted("ROLE_ROOT")')]
#[IsGranted(RolesService::ROLE_ROOT)]
#[ParamConverter(
data: 'user',
class: UserResource::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
use App\Rest\Controller;
use App\Rest\Traits\Actions;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

/**
* Class UserController
Expand All @@ -31,7 +32,7 @@
#[Route(
path: '/user',
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
class UserController extends Controller
{
use Actions\Admin\CountAction;
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/UserGroup/AttachUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use App\Entity\UserGroup;
use App\Resource\UserGroupResource;
use App\Resource\UserResource;
use App\Security\RolesService;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -121,7 +122,7 @@ public function __construct(
],
methods: [Request::METHOD_POST],
)]
#[Security('is_granted("ROLE_ROOT")')]
#[IsGranted(RolesService::ROLE_ROOT)]
#[ParamConverter(
data: 'userGroup',
class: UserGroupResource::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/UserGroup/DetachUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use App\Entity\UserGroup;
use App\Resource\UserGroupResource;
use App\Resource\UserResource;
use App\Security\RolesService;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function __construct(
],
methods: [Request::METHOD_DELETE],
)]
#[Security('is_granted("ROLE_ROOT")')]
#[IsGranted(RolesService::ROLE_ROOT)]
#[ParamConverter(
data: 'userGroup',
class: UserGroupResource::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/UserGroup/UserGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
use App\Rest\Controller;
use App\Rest\Traits\Actions;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;

/**
* Class UserGroupController
Expand All @@ -31,7 +32,7 @@
#[Route(
path: '/user_group',
)]
#[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
class UserGroupController extends Controller
{
use Actions\Admin\CountAction;
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/UserGroup/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
use App\Resource\UserGroupResource;
use App\Resource\UserResource;
use App\Rest\ResponseHandler;
use App\Security\RolesService;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function __construct(
],
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("ROLE_ADMIN")')]
#[IsGranted(RolesService::ROLE_ADMIN)]
#[ParamConverter(
data: 'userGroup',
class: UserGroupResource::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Rest/Traits/Actions/Admin/CountAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
namespace App\Rest\Traits\Actions\Admin;

use App\Rest\Traits\Methods\CountMethod;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Security\RolesService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -36,7 +37,7 @@ trait CountAction
path: '/count',
methods: [Request::METHOD_GET],
)]
#[Security('is_granted("ROLE_ADMIN")')]
#[IsGranted(RolesService::ROLE_ADMIN)]
public function countAction(Request $request): Response
{
return $this->countMethod($request);
Expand Down
5 changes: 3 additions & 2 deletions src/Rest/Traits/Actions/Admin/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

use App\DTO\RestDtoInterface;
use App\Rest\Traits\Methods\CreateMethod;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Security\RolesService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -37,7 +38,7 @@ trait CreateAction
path: '',
methods: [Request::METHOD_POST],
)]
#[Security('is_granted("ROLE_ADMIN")')]
#[IsGranted(RolesService::ROLE_ADMIN)]
public function createAction(Request $request, RestDtoInterface $restDto): Response
{
return $this->createMethod($request, $restDto);
Expand Down
5 changes: 3 additions & 2 deletions src/Rest/Traits/Actions/Admin/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
namespace App\Rest\Traits\Actions\Admin;

use App\Rest\Traits\Methods\DeleteMethod;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Security\RolesService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -39,7 +40,7 @@ trait DeleteAction
],
methods: [Request::METHOD_DELETE],
)]
#[Security('is_granted("ROLE_ADMIN")')]
#[IsGranted(RolesService::ROLE_ADMIN)]
public function deleteAction(Request $request, string $id): Response
{
return $this->deleteMethod($request, $id);
Expand Down
Loading

0 comments on commit 9364177

Please sign in to comment.