|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Auth\Authorization; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Auth\Authorization\AccessDeniedException; |
|
18
|
|
|
use Acme\App\Core\Port\Auth\Authorization\AuthorizationServiceInterface; |
|
19
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class AuthorizationService implements AuthorizationServiceInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var AuthorizationCheckerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $authorizationChecker; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(AuthorizationCheckerInterface $authorizationChecker) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function hasRole(string ...$roleList): bool |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->authorizationChecker->isGranted($roleList); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function isAllowed(string $action, $subject): bool |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->authorizationChecker->isGranted($action, $subject); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Throws an exception unless the specified roles and action are met on the subject. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function denyAccessUnlessGranted( |
|
47
|
|
|
array $roleList = [], |
|
48
|
|
|
string $action = '', |
|
49
|
|
|
string $message = 'Access Denied.', |
|
50
|
|
|
$subject = null |
|
51
|
|
|
): void { |
|
52
|
|
|
$attributes[] = $action; |
|
|
|
|
|
|
53
|
|
|
$attributes = \array_filter(\array_merge($roleList, $attributes)); |
|
54
|
|
|
|
|
55
|
|
|
if (!$this->authorizationChecker->isGranted($attributes, $subject)) { |
|
56
|
|
|
throw new AccessDeniedException($roleList, $action, $subject, $message); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.