AdminAclManipulator::addAdminClassAces()   C
last analyzed

Complexity

Conditions 11
Paths 24

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 6.8133
c 0
b 0
f 0
cc 11
nc 24
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Util;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
20
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
21
use Symfony\Component\Security\Acl\Model\AclInterface;
22
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
23
24
/**
25
 * @author Thomas Rabaix <[email protected]>
26
 */
27
final class AdminAclManipulator implements AdminAclManipulatorInterface
28
{
29
    /**
30
     * @var string
31
     */
32
    private $maskBuilderClass;
33
34
    /**
35
     * @param string $maskBuilderClass
36
     */
37
    public function __construct($maskBuilderClass)
38
    {
39
        $this->maskBuilderClass = $maskBuilderClass;
40
    }
41
42
    public function configureAcls(OutputInterface $output, AdminInterface $admin): void
43
    {
44
        $securityHandler = $admin->getSecurityHandler();
45
        if (!$securityHandler instanceof AclSecurityHandlerInterface) {
46
            $output->writeln(sprintf('Admin `%s` is not configured to use ACL : <info>ignoring</info>', $admin->getCode()));
47
48
            return;
49
        }
50
51
        $objectIdentity = ObjectIdentity::fromDomainObject($admin);
52
        $newAcl = false;
53
        if (null === ($acl = $securityHandler->getObjectAcl($objectIdentity))) {
54
            $acl = $securityHandler->createAcl($objectIdentity);
55
            $newAcl = true;
56
        }
57
58
        // create admin ACL
59
        $output->writeln(sprintf(' > install ACL for %s', $admin->getCode()));
60
        $configResult = $this->addAdminClassAces($output, $acl, $securityHandler, $securityHandler->buildSecurityInformation($admin));
61
62
        if ($configResult) {
63
            $securityHandler->updateAcl($acl);
64
        } else {
65
            $output->writeln(sprintf('   - %s , no roles and permissions found', ($newAcl ? 'skip' : 'removed')));
66
            $securityHandler->deleteAcl($objectIdentity);
67
        }
68
    }
69
70
    public function addAdminClassAces(
71
        OutputInterface $output,
72
        AclInterface $acl,
73
        AclSecurityHandlerInterface $securityHandler,
74
        array $roleInformation = []
75
    ) {
76
        if (!$acl instanceof MutableAclInterface) {
77
            throw new \TypeError(sprintf(
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 2 pass...bleAclInterface::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
                'Argument 2 passed to "%s()" must implement "%s".',
79
                __METHOD__,
80
                MutableAclInterface::class
81
            ));
82
        }
83
        if (\count($securityHandler->getAdminPermissions()) > 0) {
84
            $builder = new $this->maskBuilderClass();
85
86
            foreach ($roleInformation as $role => $permissions) {
87
                $aceIndex = $securityHandler->findClassAceIndexByRole($acl, $role);
88
                $roleAdminPermissions = [];
89
90
                foreach ($permissions as $permission) {
91
                    // add only the admin permissions
92
                    if (\in_array($permission, $securityHandler->getAdminPermissions(), true)) {
93
                        $builder->add($permission);
94
                        $roleAdminPermissions[] = $permission;
95
                    }
96
                }
97
98
                if (\count($roleAdminPermissions) > 0) {
99
                    if (false === $aceIndex) {
100
                        $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
101
                        $action = 'add';
102
                    } else {
103
                        $acl->updateClassAce($aceIndex, $builder->get());
104
                        $action = 'update';
105
                    }
106
107
                    if (null !== $output) {
108
                        $output->writeln(sprintf('   - %s role: %s, permissions: %s', $action, $role, json_encode($roleAdminPermissions)));
109
                    }
110
111
                    $builder->reset();
112
                } elseif (false !== $aceIndex) {
113
                    $acl->deleteClassAce($aceIndex);
114
115
                    if (null !== $output) {
116
                        $output->writeln(sprintf('   - remove role: %s', $role));
117
                    }
118
                }
119
            }
120
121
            return true;
122
        }
123
124
        return false;
125
    }
126
}
127