|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Linio\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
10
|
|
|
|
|
11
|
|
|
trait AuthorizationAware |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var AuthorizationCheckerInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $authorizationChecker; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return AuthorizationCheckerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getAuthorizationChecker() |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->authorizationChecker; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Checks if the attributes are granted against the current authentication token and optionally supplied object. |
|
36
|
|
|
* |
|
37
|
|
|
* @param mixed $attributes The attributes |
|
38
|
|
|
* @param mixed $object The object |
|
39
|
|
|
* |
|
40
|
|
|
* @throws LogicException |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function isGranted($attributes, $object = null) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->authorizationChecker->isGranted($attributes, $object); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Throws an exception unless the attributes are granted against the current authentication token and optionally |
|
51
|
|
|
* supplied object. |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $attributes The attributes |
|
54
|
|
|
* @param mixed $object The object |
|
55
|
|
|
* @param string $message The message passed to the exception |
|
56
|
|
|
* |
|
57
|
|
|
* @throws AccessDeniedException |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$this->isGranted($attributes, $object)) { |
|
62
|
|
|
throw new AccessDeniedException($message); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|