Completed
Push — master ( 3a5e1f...635326 )
by
03:34
created

SingleResourceProvider::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 4
nop 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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