Skip to content

Commit

Permalink
add ROLE_USER policy for authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasgv committed Oct 2, 2024
1 parent bb31c7d commit 2278816
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Add ROLE_USER policy to configure who is granted access to the API

## v0.1.2

* Integrate Blob File API
Expand Down
14 changes: 14 additions & 0 deletions src/Authorization/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Authorization;

use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\DependencyInjection\Configuration;
use Dbp\Relay\CoreBundle\Authorization\AbstractAuthorizationService;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Symfony\Component\HttpFoundation\Response;

class AuthorizationService extends AbstractAuthorizationService
{
public function denyAccessUnlessHasRoleUser(): void
{
if (!$this->hasRoleUser()) {
throw ApiError::withDetails(Response::HTTP_FORBIDDEN);
}
}

public function hasRoleUser(): bool
{
return $this->isGranted(Configuration::ROLE_USER);
}
}
9 changes: 7 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\DependencyInjection;

use Dbp\Relay\CoreBundle\Authorization\AuthorizationConfigDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public const ROLE_USER = 'ROLE_USER';

public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('dbp_relay_blob_connector_campusonline_dms');

// append your config definition here
$treeBuilder->getRootNode()
->append(AuthorizationConfigDefinition::create()
->addPolicy(self::ROLE_USER, 'false', 'Returns true if the current user is authorized to use the API')
->getNodeDefinition());

return $treeBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\DependencyInjection;

use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Authorization\AuthorizationService;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Service\DocumentService;
use Dbp\Relay\CoreBundle\Extension\ExtensionTrait;
use Symfony\Component\Config\FileLocator;
Expand All @@ -27,5 +28,8 @@ public function loadInternal(array $mergedConfig, ContainerBuilder $container):

$definition = $container->getDefinition(DocumentService::class);
$definition->addMethodCall('setConfig', [$mergedConfig]);

$definition = $container->getDefinition(AuthorizationService::class);
$definition->addMethodCall('setConfig', [$mergedConfig]);
}
}
4 changes: 3 additions & 1 deletion src/Entity/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
provider: DocumentProvider::class
),
new Get(
uriTemplate: '/co-dp-dms-adapter-d3/api/version/{uid}',
uriTemplate: '/co-dp-dms-adapter-d3/api/documents/version/{uid}',
outputFormats: [
'octet_stream' => 'application/octet-stream',
'jsonproblem' => 'application/problem+json',
],
controller: GetDocumentVersionContentController::class,
openapiContext: [
'tags' => ['Campusonline DMS'],
'summary' => 'Retrieves the file content for a BlobConnectorCampusonlineDmsDocumentVersionInfo resource',
'responses' => [
'200' => [
'content' => [
Expand Down Expand Up @@ -122,6 +123,7 @@
controller: CreateDocumentVersionController::class,
openapiContext: [
'tags' => ['Campusonline DMS'],
'summary' => 'Creates a new version for a BlobConnectorCampusonlineDmsDocument resource',
'requestBody' => [
'content' => [
'application/octet-stream' => [
Expand Down
1 change: 1 addition & 0 deletions src/Rest/CreateDocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(
public function __invoke(Request $request): Document
{
$this->requireAuthentication();
$this->authorizationService->denyAccessUnlessHasRoleUser();

$name = $request->request->get('name'); // TODO: validate name
$documentType = $request->request->get('documentType'); // TODO: validate document type
Expand Down
1 change: 1 addition & 0 deletions src/Rest/CreateDocumentVersionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
public function __invoke(Request $request, string $uid): ?Document
{
$this->requireAuthentication();
$this->authorizationService->denyAccessUnlessHasRoleUser();

$uploadedFile = $request->files->get('content'); // TODO: validate uploaded file

Expand Down
13 changes: 9 additions & 4 deletions src/Rest/DocumentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Rest;

use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Authorization\AuthorizationService;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Entity\Document;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Service\DocumentService;
use Dbp\Relay\CoreBundle\Rest\AbstractDataProvider;
Expand All @@ -15,11 +16,10 @@ class DocumentProvider extends AbstractDataProvider
{
protected static string $identifierName = 'uid';

private DocumentService $documentService;

public function __construct(DocumentService $placeService)
public function __construct(
private readonly DocumentService $documentService,
private readonly AuthorizationService $authorizationService)
{
$this->documentService = $placeService;
}

protected function getItemById(string $id, array $filters = [], array $options = []): ?Document
Expand All @@ -31,4 +31,9 @@ protected function getPage(int $currentPageNumber, int $maxNumItemsPerPage, arra
{
throw new \RuntimeException('not available');
}

protected function isCurrentUserGrantedOperationAccess(int $operation): bool
{
return $this->authorizationService->hasRoleUser();
}
}
12 changes: 6 additions & 6 deletions src/Rest/DocumentVersionInfoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Rest;

use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Authorization\AuthorizationService;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Entity\DocumentVersionInfo;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Service\DocumentService;
use Dbp\Relay\CoreBundle\Rest\AbstractDataProvider;
Expand All @@ -15,11 +16,10 @@ class DocumentVersionInfoProvider extends AbstractDataProvider
{
protected static string $identifierName = 'uid';

private DocumentService $documentService;

public function __construct(DocumentService $placeService)
public function __construct(
private readonly DocumentService $documentService,
private readonly AuthorizationService $authorizationService)
{
$this->documentService = $placeService;
}

protected function getItemById(string $id, array $filters = [], array $options = []): ?DocumentVersionInfo
Expand All @@ -32,8 +32,8 @@ protected function getPage(int $currentPageNumber, int $maxNumItemsPerPage, arra
throw new \RuntimeException('not available');
}

protected function isUserGrantedOperationAccess(int $operation): bool
protected function isCurrentUserGrantedOperationAccess(int $operation): bool
{
return $this->isAuthenticated();
return $this->authorizationService->hasRoleUser();
}
}
13 changes: 9 additions & 4 deletions src/Rest/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Rest;

use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Authorization\AuthorizationService;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Entity\File;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Service\DocumentService;
use Dbp\Relay\CoreBundle\Rest\AbstractDataProcessor;
Expand All @@ -12,11 +13,10 @@ class FileProcessor extends AbstractDataProcessor
{
protected static string $identifierName = 'uid';

private DocumentService $documentService;

public function __construct(DocumentService $placeService)
public function __construct(
private readonly DocumentService $documentService,
private readonly AuthorizationService $authorizationService)
{
$this->documentService = $placeService;
}

protected function addItem(mixed $data, array $filters): File
Expand All @@ -32,4 +32,9 @@ protected function replaceItem(mixed $identifier, mixed $data, mixed $previousDa

return $this->documentService->replaceFile($identifier, $data);
}

protected function isCurrentUserGrantedOperationAccess(int $operation): bool
{
return $this->authorizationService->hasRoleUser();
}
}
13 changes: 9 additions & 4 deletions src/Rest/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Rest;

use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Authorization\AuthorizationService;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Entity\File;
use Dbp\Relay\BlobConnectorCampusonlineDmsBundle\Service\DocumentService;
use Dbp\Relay\CoreBundle\Rest\AbstractDataProvider;
Expand All @@ -15,11 +16,10 @@ class FileProvider extends AbstractDataProvider
{
protected static string $identifierName = 'uid';

private DocumentService $documentService;

public function __construct(DocumentService $placeService)
public function __construct(
private readonly DocumentService $documentService,
private readonly AuthorizationService $authorizationService)
{
$this->documentService = $placeService;
}

protected function getItemById(string $id, array $filters = [], array $options = []): ?File
Expand All @@ -31,4 +31,9 @@ protected function getPage(int $currentPageNumber, int $maxNumItemsPerPage, arra
{
throw new \RuntimeException('not available');
}

protected function isCurrentUserGrantedOperationAccess(int $operation): bool
{
return $this->authorizationService->hasRoleUser();
}
}
5 changes: 4 additions & 1 deletion src/Rest/GetDocumentVersionContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public function __construct(
{
}

/**
* @throws \Exception
*/
public function __invoke(Request $request, string $uid): Response
{
$this->requireAuthentication();
$this->authorizationService->denyAccessUnlessHasRoleUser();

// TODO: get blob file with given version and return its content
return $this->documentService->getDocumentVersionBinaryFileResponse($uid);
}
}

0 comments on commit 2278816

Please sign in to comment.