Skip to content

Commit

Permalink
Merge pull request #8 from theodo/sf6-compatibility
Browse files Browse the repository at this point in the history
Sf6 compatibility
  • Loading branch information
Paulmolin authored Aug 29, 2022
2 parents abebfeb + d4a227a commit 71281ad
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 108 deletions.
8 changes: 4 additions & 4 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
$finder = PhpCsFixer\Finder::create()
$finder = (new PhpCsFixer\Finder())
->in(
[
__DIR__.'/src'
]
);
return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setFinder($finder)
->setRiskyAllowed(true)
->setUsingCache(false) // cache is always an issue when you switch branch because the cached file is ignored by git
Expand All @@ -26,10 +26,10 @@
'mb_str_functions' => true,
'modernize_types_casting' => true,
'no_alternative_syntax' => true,
'no_multiline_whitespace_before_semicolons' => true,
'multiline_whitespace_before_semicolons' => true,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_short_echo_tag' => true,
'echo_tag_syntax' => ['format' => 'long'],
'no_superfluous_elseif' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ACCENT - API Platform route access-control checker

ACCENT (Access Control Checker Easy Neat Thorough) is a Symfony command to check that all your API Platform routes have an access control.

## 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
```

### Run the command

```bash
bin/console forge:access-control
```
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"type": "symfony-bundle",
"require": {
"php": "^7.1|^8.0",
"symfony/config": "^4.0|^5.0",
"symfony/console": "^4.0|^5.0",
"symfony/dependency-injection": "^4.0|^5.0",
"symfony/http-kernel": "^4.0|^5.0",
"symfony/routing": "^4.0|^5.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"
},
"autoload": {
Expand All @@ -24,7 +24,7 @@
}
],
"require-dev": {
"phpstan/phpstan": "^0.11.1",
"friendsofphp/php-cs-fixer": "^2.14"
"phpstan/phpstan": "^1.8",
"friendsofphp/php-cs-fixer": "^3.9"
}
}
20 changes: 0 additions & 20 deletions doc/install.md

This file was deleted.

3 changes: 0 additions & 3 deletions src/AccessControl/AccentReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public function getUnprotectedRoutesCount()
return $this->unprotectedRoutesCount;
}

/**
* @param int $unprotectedRoutesCount
*/
public function setUnprotectedRoutesCount(int $unprotectedRoutesCount): void
{
$this->unprotectedRoutesCount = $unprotectedRoutesCount;
Expand Down
5 changes: 0 additions & 5 deletions src/AccessControl/AccentReportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public function __construct(
$this->router = $router;
}

/**
* @return AccentReport
*/
public function createAccentReport(): AccentReport
{
$routes = $this->router->getRouteCollection();
Expand All @@ -40,8 +37,6 @@ public function createAccentReport(): AccentReport
}

/**
* @param RouteCollection $routes
*
* @return RouteAccessControlData[]
*/
public function getAllRoutesAccessControlData(RouteCollection $routes): array
Expand Down
32 changes: 4 additions & 28 deletions src/AccessControl/RouteAccessControlData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,51 @@

class RouteAccessControlData
{
const NO_ACCESS_CONTROL = 'NO_ACCESS_CONTROL';
const NOT_API_PLATFORM_ROUTE = 'NOT_API_PLATFORM_ROUTE';
const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
const RESOURCE_UNRELATED_ROUTE = 'RESOURCE_UNRELATED_ROUTE';
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 RESOURCE_UNRELATED_ROUTE = 'RESOURCE_UNRELATED_ROUTE';

private $route;
private $routeName;
private $expression;
private $correct;

/**
* @return string
*/
public function getRouteName(): string
{
return $this->routeName;
}

/**
* @param string $routeName
*/
public function setRouteName(string $routeName): void
{
$this->routeName = $routeName;
}

/**
* @return Route
*/
public function getRoute(): Route
{
return $this->route;
}

/**
* @param Route $route
*/
public function setRoute(Route $route): void
{
$this->route = $route;
}

/**
* @return string
*/
public function getExpression(): string
{
return $this->expression;
}

/**
* @param string $expression
*/
public function setExpression(string $expression): void
{
$this->expression = $expression;
}

/**
* @return bool
*/
public function isCorrect(): bool
{
return $this->correct;
}

/**
* @param bool $correct
*/
public function setCorrect(bool $correct): void
{
$this->correct = $correct;
Expand Down
16 changes: 0 additions & 16 deletions src/AccessControl/RouteAccessControlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public function __construct(
$this->judge = $routeAccessControlJudge;
}

/**
* @param string $name
* @param Route $route
*
* @return RouteAccessControlData
*/
public function createRouteAccessControlData(string $name, Route $route): RouteAccessControlData
{
$controller = $route->getDefault('_controller');
Expand All @@ -49,23 +43,13 @@ public function createRouteAccessControlData(string $name, Route $route): RouteA
return $accessControlRouteData;
}

/**
* @param string $controller
*
* @return bool
*/
protected function isControllerCorrespondingToApiPlatform(string $controller): bool
{
$apiPlatformPrefix = 'api_platform';

return 0 === mb_strpos($controller, $apiPlatformPrefix);
}

/**
* @param Route $route
*
* @return string
*/
protected function getAccessControlExpressionForApiPlatform(Route $route): string
{
$resourceClass = $route->getDefault('_api_resource_class');
Expand Down
18 changes: 1 addition & 17 deletions src/AccessControl/RouteAccessControlJudge.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@

class RouteAccessControlJudge
{
const API_PLATFORM_DOCUMENTATION_ROUTES = [
public const API_PLATFORM_DOCUMENTATION_ROUTES = [
'api_entrypoint',
'api_doc',
];

/**
* @param string $routeName
* @param string $expression
*
* @return bool
*/
public function isAccessControlCorrect(string $routeName, string $expression): bool
{
if ($this->hasNoAccessControl($expression)) {
Expand All @@ -30,21 +24,11 @@ public function isAccessControlCorrect(string $routeName, string $expression): b
return true;
}

/**
* @param string $expression
*
* @return bool
*/
protected function hasNoAccessControl(string $expression): bool
{
return RouteAccessControlData::NO_ACCESS_CONTROL === $expression;
}

/**
* @param string $routeName
*
* @return bool
*/
protected function isUnnecessaryExposedApiPlatformDocumentationRoute(string $routeName): bool
{
return \in_array($routeName, self::API_PLATFORM_DOCUMENTATION_ROUTES, true);
Expand Down
3 changes: 0 additions & 3 deletions src/DependencyInjection/ForgeAccentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
class ForgeAccentExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $container
*
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container)
Expand Down
6 changes: 1 addition & 5 deletions src/Descriptor/AccessControlDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class AccessControlDescriptor
{
const ACCESS_CONTROL_TRANSLATIONS = [
public const ACCESS_CONTROL_TRANSLATIONS = [
RouteAccessControlData::NO_ACCESS_CONTROL => '<fg=white;bg=red>No access control.</>',
RouteAccessControlData::NOT_API_PLATFORM_ROUTE => 'This route is not linked to API Platform.',
RouteAccessControlData::RESOURCE_NOT_FOUND => 'The resource linked to his route was not found.',
Expand All @@ -19,7 +19,6 @@ class AccessControlDescriptor
private $output;

/**
* @param OutputInterface $output
* @param RouteAccessControlData[] $routeAccessControlData
*/
public function describe(OutputInterface $output, array $routeAccessControlData)
Expand All @@ -46,9 +45,6 @@ public function describe(OutputInterface $output, array $routeAccessControlData)
$table->render();
}

/**
* @return OutputInterface
*/
public function getOutput(): OutputInterface
{
return $this->output;
Expand Down

0 comments on commit 71281ad

Please sign in to comment.