Skip to content

Commit

Permalink
Merge pull request #11 from theodo/update-to-api-platform-3
Browse files Browse the repository at this point in the history
Add api-platform 3 support
  • Loading branch information
kraynel authored Sep 20, 2023
2 parents 71281ad + 8e7d2e0 commit bb0f3e8
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 70 deletions.
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@ ACCENT (Access Control Checker Easy Neat Thorough) is a Symfony command to check

## Installation

### Make sure composer knows how to access the bundle

Add the path to the private repository in your composer.json:
```json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/theodo/accent"
}
]
```

### Require the bundle

```bash
composer require --dev forge/accent-bundle
composer require --dev theodo/accent-bundle
```

### Run the command

```bash
bin/console forge:access-control
bin/console theodo:access-control
```
23 changes: 14 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
{
"name": "forge/accent-bundle",
"name": "theodo/accent-bundle",
"description": "Access Control Checker Easy Neat Thorough",
"type": "symfony-bundle",
"keywords": ["security","api-platform","symfony","acl","checks"],
"require": {
"php": "^7.1|^8.0",
"symfony/config": "^4.0|^5.0|^6.0",
"symfony/console": "^4.0|^5.0|^6.0",
"symfony/dependency-injection": "^4.0|^5.0|^6.0",
"symfony/http-kernel": "^4.0|^5.0|^6.0",
"symfony/routing": "^4.0|^5.0|^6.0",
"api-platform/core": "^2.5"
"php": "^8.0",
"symfony/config": "^6.0",
"symfony/console": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/http-kernel": "^6.0",
"symfony/routing": "^6.0",
"api-platform/core": "^3.0"
},
"autoload": {
"psr-4": {
"Forge\\AccentBundle\\": "src/"
"Theodo\\AccentBundle\\": "src/"
}
},
"license": "MIT",
"authors": [
{
"name": "Paul Molin",
"email": "[email protected]"
},
{
"name": "Kevin Raynel",
"email": "[email protected]"
}
],
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/AccessControl/AccentReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Forge\AccentBundle\AccessControl;
namespace Theodo\AccentBundle\AccessControl;

class AccentReport
{
Expand Down
2 changes: 1 addition & 1 deletion src/AccessControl/AccentReportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Forge\AccentBundle\AccessControl;
namespace Theodo\AccentBundle\AccessControl;

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
Expand Down
3 changes: 2 additions & 1 deletion src/AccessControl/RouteAccessControlData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Forge\AccentBundle\AccessControl;
namespace Theodo\AccentBundle\AccessControl;

use Symfony\Component\Routing\Route;

Expand All @@ -11,6 +11,7 @@ class RouteAccessControlData
public const NO_ACCESS_CONTROL = 'NO_ACCESS_CONTROL';
public const NOT_API_PLATFORM_ROUTE = 'NOT_API_PLATFORM_ROUTE';
public const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
public const OPERATION_NOT_FOUND = 'OPERATION_NOT_FOUND';
public const RESOURCE_UNRELATED_ROUTE = 'RESOURCE_UNRELATED_ROUTE';

private $route;
Expand Down
27 changes: 13 additions & 14 deletions src/AccessControl/RouteAccessControlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

declare(strict_types=1);

namespace Forge\AccentBundle\AccessControl;
namespace Theodo\AccentBundle\AccessControl;

use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Util\AttributesExtractor;
use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Exception\ResourceClassNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Symfony\Component\Routing\Route;

class RouteAccessControlFactory
{
private $resourceMetadataFactory;
private $judge;

public function __construct(
ResourceMetadataFactoryInterface $resourceMetadataFactory,
RouteAccessControlJudge $routeAccessControlJudge
private ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
private RouteAccessControlJudge $judge
) {
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->judge = $routeAccessControlJudge;
}

public function createRouteAccessControlData(string $name, Route $route): RouteAccessControlData
Expand Down Expand Up @@ -53,19 +48,23 @@ protected function isControllerCorrespondingToApiPlatform(string $controller): b
protected function getAccessControlExpressionForApiPlatform(Route $route): string
{
$resourceClass = $route->getDefault('_api_resource_class');
$operationName = $route->getDefault('_api_operation_name');

$isGranted = RouteAccessControlData::RESOURCE_UNRELATED_ROUTE;

if ($resourceClass) {
try {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$attributes = AttributesExtractor::extractAttributes($route->getDefaults());
$isGranted = $resourceMetadata->getOperationAttribute($attributes, 'security', null, true);
$resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadata->getOperation($operationName);

$isGranted = $operation->getSecurity();
if (null === $isGranted) {
$isGranted = RouteAccessControlData::NO_ACCESS_CONTROL;
}
} catch (ResourceClassNotFoundException $e) {
$isGranted = RouteAccessControlData::RESOURCE_NOT_FOUND;
} catch (OperationNotFoundException $e) {
$isGranted = RouteAccessControlData::OPERATION_NOT_FOUND;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/AccessControl/RouteAccessControlJudge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Forge\AccentBundle\AccessControl;
namespace Theodo\AccentBundle\AccessControl;

class RouteAccessControlJudge
{
Expand Down
13 changes: 5 additions & 8 deletions src/Command/AccessControlCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

declare(strict_types=1);

namespace Forge\AccentBundle\Command;
namespace Theodo\AccentBundle\Command;

use Forge\AccentBundle\AccessControl\AccentReportFactory;
use Forge\AccentBundle\Descriptor\AccessControlDescriptor;
use Theodo\AccentBundle\AccessControl\AccentReportFactory;
use Theodo\AccentBundle\Descriptor\AccessControlDescriptor;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand('theodo:access-control', 'Check access control for each route.')]
class AccessControlCheckerCommand extends Command
{
protected static $defaultName = 'forge:access-control';
private $accentReportFactory;

public function __construct(AccentReportFactory $accentReportFactory)
Expand All @@ -25,10 +26,6 @@ public function __construct(AccentReportFactory $accentReportFactory)

protected function configure()
{
$this->setDescription(
'Check access control for each route.'
);

$this->setHelp(
'This command checks all the protections set up for each route, and displays them in an elegant and understandable way.'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Forge\AccentBundle\DependencyInjection;
namespace Theodo\AccentBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class ForgeAccentExtension extends Extension
class TheodoAccentExtension extends Extension
{
/**
* @throws \Exception
Expand Down
4 changes: 2 additions & 2 deletions src/Descriptor/AccessControlDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Forge\AccentBundle\Descriptor;
namespace Theodo\AccentBundle\Descriptor;

use Forge\AccentBundle\AccessControl\RouteAccessControlData;
use Theodo\AccentBundle\AccessControl\RouteAccessControlData;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down
30 changes: 15 additions & 15 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service class="Forge\AccentBundle\AccessControl\RouteAccessControlJudge"
id="forge_accent.route_access_control_judge"
<service class="Theodo\AccentBundle\AccessControl\RouteAccessControlJudge"
id="theodo_accent.route_access_control_judge"
>
</service>
<service class="Forge\AccentBundle\AccessControl\RouteAccessControlFactory"
id="forge_accent.route_access_control_extractor"
<service class="Theodo\AccentBundle\AccessControl\RouteAccessControlFactory"
id="theodo_accent.route_access_control_extractor"
>
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="forge_accent.route_access_control_judge" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
<argument type="service" id="theodo_accent.route_access_control_judge" />
</service>
<service class="Forge\AccentBundle\AccessControl\AccentReportFactory"
id="forge_accent.accent_report_factory">
<argument type="service" id="forge_accent.route_access_control_extractor" />
<argument type="service" id="forge_accent.route_access_control_judge" />
<service class="Theodo\AccentBundle\AccessControl\AccentReportFactory"
id="theodo_accent.accent_report_factory">
<argument type="service" id="theodo_accent.route_access_control_extractor" />
<argument type="service" id="theodo_accent.route_access_control_judge" />
<argument type="service" id="router" />
</service>
<service class="Forge\AccentBundle\Command\AccessControlCheckerCommand"
id="forge_accent.access_control_checker_command">
<argument type="service" id="forge_accent.accent_report_factory"/>
<service class="Theodo\AccentBundle\Command\AccessControlCheckerCommand"
id="theodo_accent.access_control_checker_command">
<argument type="service" id="theodo_accent.accent_report_factory"/>
<tag name="console.command"/>
</service>
<service id="Forge\AccentBundle\Command\AccessControlCheckerCommand"
alias="forge_accent.access_control_checker_command" />
<service id="Theodo\AccentBundle\Command\AccessControlCheckerCommand"
alias="theodo_accent.access_control_checker_command" />
</services>

</container>
4 changes: 2 additions & 2 deletions src/ForgeAccentBundle.php → src/TheodoAccentBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Forge\AccentBundle;
namespace Theodo\AccentBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ForgeAccentBundle extends Bundle
class TheodoAccentBundle extends Bundle
{
}

0 comments on commit bb0f3e8

Please sign in to comment.