Completed
Push — 3.x ( 3e834f...38b337 )
by Grégoire
03:36
created

AdminAclManipulator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
 * @final since sonata-project/admin-bundle 3.52
26
 *
27
 * @author Thomas Rabaix <[email protected]>
28
 */
29
class AdminAclManipulator implements AdminAclManipulatorInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $maskBuilderClass;
35
36
    /**
37
     * @param string $maskBuilderClass
38
     */
39
    public function __construct($maskBuilderClass)
40
    {
41
        $this->maskBuilderClass = $maskBuilderClass;
42
    }
43
44
    public function configureAcls(OutputInterface $output, AdminInterface $admin)
45
    {
46
        $securityHandler = $admin->getSecurityHandler();
47
        if (!$securityHandler instanceof AclSecurityHandlerInterface) {
48
            $output->writeln(sprintf('Admin `%s` is not configured to use ACL : <info>ignoring</info>', $admin->getCode()));
49
50
            return;
51
        }
52
53
        $objectIdentity = ObjectIdentity::fromDomainObject($admin);
54
        $newAcl = false;
55
        if (null === ($acl = $securityHandler->getObjectAcl($objectIdentity))) {
56
            $acl = $securityHandler->createAcl($objectIdentity);
57
            $newAcl = true;
58
        }
59
60
        // create admin ACL
61
        $output->writeln(sprintf(' > install ACL for %s', $admin->getCode()));
62
        $configResult = $this->addAdminClassAces($output, $acl, $securityHandler, $securityHandler->buildSecurityInformation($admin));
63
64
        if ($configResult) {
65
            $securityHandler->updateAcl($acl);
66
        } else {
67
            $output->writeln(sprintf('   - %s , no roles and permissions found', ($newAcl ? 'skip' : 'removed')));
68
            $securityHandler->deleteAcl($objectIdentity);
69
        }
70
    }
71
72
    public function addAdminClassAces(
73
        OutputInterface $output,
74
        AclInterface $acl,
75
        AclSecurityHandlerInterface $securityHandler,
76
        array $roleInformation = []
77
    ) {
78
        if (!$acl instanceof MutableAclInterface) {
79
            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...
80
                'Argument 2 passed to "%s()" must implement "%s".',
81
                __METHOD__,
82
                MutableAclInterface::class
83
            ));
84
        }
85
        if (\count($securityHandler->getAdminPermissions()) > 0) {
86
            $builder = new $this->maskBuilderClass();
87
88
            foreach ($roleInformation as $role => $permissions) {
89
                $aceIndex = $securityHandler->findClassAceIndexByRole($acl, $role);
90
                $roleAdminPermissions = [];
91
92
                foreach ($permissions as $permission) {
93
                    // add only the admin permissions
94
                    if (\in_array($permission, $securityHandler->getAdminPermissions(), true)) {
95
                        $builder->add($permission);
96
                        $roleAdminPermissions[] = $permission;
97
                    }
98
                }
99
100
                if (\count($roleAdminPermissions) > 0) {
101
                    if (false === $aceIndex) {
102
                        $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
103
                        $action = 'add';
104
                    } else {
105
                        $acl->updateClassAce($aceIndex, $builder->get());
106
                        $action = 'update';
107
                    }
108
109
                    if (null !== $output) {
110
                        $output->writeln(sprintf('   - %s role: %s, permissions: %s', $action, $role, json_encode($roleAdminPermissions)));
111
                    }
112
113
                    $builder->reset();
114
                } elseif (false !== $aceIndex) {
115
                    $acl->deleteClassAce($aceIndex);
116
117
                    if (null !== $output) {
118
                        $output->writeln(sprintf('   - remove role: %s', $role));
119
                    }
120
                }
121
            }
122
123
            return true;
124
        }
125
126
        return false;
127
    }
128
}
129