1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\AclDecoratedResolver; |
4
|
|
|
|
5
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
6
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface; |
7
|
|
|
use Sylius\Component\Rbac\Authorization\AuthorizationCheckerInterface as SyliusAuthorizationCheckerInterface; |
8
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
12
|
|
|
|
13
|
|
|
class SingleResourceProvider implements SingleResourceProviderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var SingleResourceProviderInterface |
17
|
|
|
*/ |
18
|
|
|
private $singleResourceDecorator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AuthorizationCheckerInterface |
22
|
|
|
*/ |
23
|
|
|
private $authorizationChecker; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SyliusAuthorizationCheckerInterface |
27
|
|
|
*/ |
28
|
|
|
private $rbacAuthorizationChecker; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $ignoreWhenHasRbacRole; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
SingleResourceProviderInterface $singleResourceDecorator, |
37
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
38
|
|
|
SyliusAuthorizationCheckerInterface $rbacAuthorizationChecker, |
39
|
|
|
$ignoreWhenHasRbacRole = 'admin' |
40
|
|
|
) { |
41
|
|
|
$this->singleResourceDecorator = $singleResourceDecorator; |
42
|
|
|
$this->authorizationChecker = $authorizationChecker; |
43
|
|
|
$this->rbacAuthorizationChecker = $rbacAuthorizationChecker; |
44
|
|
|
$this->ignoreWhenHasRbacRole = $ignoreWhenHasRbacRole; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
private function requireOwnerCheck(array $vars) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if ($this->rbacAuthorizationChecker->isGranted($this->ignoreWhenHasRbacRole)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!array_key_exists('acl_owner', $vars)) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false !== $vars['acl_owner']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository) |
64
|
|
|
{ |
65
|
|
|
$resource = $this->singleResourceDecorator->get($requestConfiguration, $repository); |
66
|
|
|
|
67
|
|
|
if ($resource && $this->requireOwnerCheck($requestConfiguration->getVars())) { |
68
|
|
|
try { |
69
|
|
|
if ($this->authorizationChecker->isGranted('OWNER', $resource)) { |
70
|
|
|
return $resource; |
71
|
|
|
} |
72
|
|
|
} catch (AuthenticationCredentialsNotFoundException $e) { |
73
|
|
|
throw new AccessDeniedException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new AccessDeniedException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $resource; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.