ResourcesCollectionProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
namespace DoS\ResourceBundle\AclDecoratedResolver;
4
5
use DoS\ResourceBundle\AclDecoratedResolver\Grid\ResourceOwnerFilter;
6
use Sylius\Component\Rbac\Authorization\AuthorizationCheckerInterface;
7
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
8
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
9
use Sylius\Component\Rbac\Provider\CurrentIdentityProviderInterface;
10
use Sylius\Component\Resource\Repository\RepositoryInterface;
11
12
class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface
13
{
14
    /**
15
     * @var ResourcesCollectionProviderInterface
16
     */
17
    private $decoratedResolver;
18
19
    /**
20
     * @var CurrentIdentityProviderInterface
21
     */
22
    private $currentIdentityProvider;
23
24
    /**
25
     * @var AuthorizationCheckerInterface
26
     */
27
    private $rbacAuthorizationChecker;
28
29
    /**
30
     * @var string
31
     */
32
    private $ignoreWhenHasRbacRole;
33
34
    public function __construct(
35
        ResourcesCollectionProviderInterface $decoratedResolver,
36
        CurrentIdentityProviderInterface $currentIdentityProvider,
37
        AuthorizationCheckerInterface $rbacAuthorizationChecker,
38
        $ignoreWhenHasRbacRole = 'admin'
39
    ) {
40
        $this->decoratedResolver = $decoratedResolver;
41
        $this->currentIdentityProvider = $currentIdentityProvider;
42
        $this->rbacAuthorizationChecker = $rbacAuthorizationChecker;
43
        $this->ignoreWhenHasRbacRole = $ignoreWhenHasRbacRole;
44
    }
45
46 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...
47
    {
48
        if ($this->rbacAuthorizationChecker->isGranted($this->ignoreWhenHasRbacRole)) {
49
            return false;
50
        }
51
52
        if (!array_key_exists('acl_owner', $vars)) {
53
            return false;
54
        }
55
56
        return false !== $vars['acl_owner'];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository)
63
    {
64
        if ($requestConfiguration->hasPermission() && $this->requireOwnerCheck($vars = $requestConfiguration->getVars())) {
65
            $owner = $this->currentIdentityProvider->getIdentity();
66
            $ownerField = is_bool($vars['acl_owner'])
67
                ? ResourceOwnerFilter::FIELD
68
                : $vars['acl_owner']
69
            ;
70
71
            // using in grid param
72
            $requestConfiguration
73
                ->getRequest()
74
                ->query->set(ResourceOwnerFilter::TYPE, $owner)
75
            ;
76
77
            $criteria = $requestConfiguration->getCriteria();
78
            $criteria[$ownerField] = $owner;
79
80
            $requestConfiguration->getParameters()->set('criteria', $criteria);
81
        }
82
83
        return $this->decoratedResolver->get($requestConfiguration, $repository);
84
    }
85
}
86