ObjectAclManipulator::configureAcls()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.6257
c 0
b 0
f 0
cc 6
nc 10
nop 4
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\UserSecurityIdentity;
20
21
/**
22
 * @author Thomas Rabaix <[email protected]>
23
 */
24
abstract class ObjectAclManipulator implements ObjectAclManipulatorInterface
25
{
26
    /**
27
     * Configure the object ACL for the passed object identities.
28
     *
29
     * @throws \Exception
30
     *
31
     * @return array [countAdded, countUpdated]
32
     */
33
    public function configureAcls(
34
        OutputInterface $output,
35
        AdminInterface $admin,
36
        \Traversable $oids,
37
        ?UserSecurityIdentity $securityIdentity = null
38
    ) {
39
        $countAdded = 0;
40
        $countUpdated = 0;
41
        $securityHandler = $admin->getSecurityHandler();
42
        if (!$securityHandler instanceof AclSecurityHandlerInterface) {
43
            $output->writeln(sprintf('Admin `%s` is not configured to use ACL : <info>ignoring</info>', $admin->getCode()));
44
45
            return [0, 0];
46
        }
47
48
        $acls = $securityHandler->findObjectAcls($oids);
49
50
        foreach ($oids as $oid) {
51
            if ($acls->contains($oid)) {
52
                $acl = $acls->offsetGet($oid);
53
                ++$countUpdated;
54
            } else {
55
                $acl = $securityHandler->createAcl($oid);
56
                ++$countAdded;
57
            }
58
59
            if (null !== $securityIdentity) {
60
                // add object owner
61
                $securityHandler->addObjectOwner($acl, $securityIdentity);
62
            }
63
64
            $securityHandler->addObjectClassAces($acl, $securityHandler->buildSecurityInformation($admin));
65
66
            try {
67
                $securityHandler->updateAcl($acl);
68
            } catch (\Exception $e) {
69
                $output->writeln(sprintf('Error saving ObjectIdentity (%s, %s) ACL : %s <info>ignoring</info>', $oid->getIdentifier(), $oid->getType(), $e->getMessage()));
70
            }
71
        }
72
73
        return [$countAdded, $countUpdated];
74
    }
75
}
76