Completed
Push — master ( c370a7...7115e7 )
by
unknown
39:46
created

LoadRolesData::processPermission()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 25
rs 8.439
c 3
b 1
f 0
cc 5
eloc 15
nc 3
nop 4
1
<?php
2
3
namespace OroCRM\Bundle\DemoDataBundle\Migrations\Data\ORM;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
8
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
12
use Symfony\Component\Yaml\Yaml;
13
14
use Oro\Bundle\UserBundle\Entity\Role;
15
use Oro\Bundle\SecurityBundle\Acl\Persistence\AclManager;
16
17
class LoadRolesData extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    protected $container;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getDependencies()
28
    {
29
        return [
30
            'Oro\Bundle\OrganizationBundle\Migrations\Data\ORM\LoadOrganizationAndBusinessUnitData',
31
            'Oro\Bundle\UserBundle\Migrations\Data\ORM\LoadRolesData'
32
        ];
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function setContainer(ContainerInterface $container = null)
39
    {
40
        $this->container = $container;
41
    }
42
43
    /**
44
     * Load roles
45
     *
46
     * @param \Doctrine\Common\Persistence\ObjectManager $manager
47
     */
48
    public function load(ObjectManager $manager)
49
    {
50
        /** @var AclManager $manager */
51
        $aclManager = $this->container->get('oro_security.acl.manager');
52
53
        $fileName = $this->container
54
            ->get('kernel')
55
            ->locateResource('@OroCRMDemoDataBundle/Migrations/Data/ORM/CrmRoles/roles.yml');
56
57
        $fileName  = str_replace('/', DIRECTORY_SEPARATOR, $fileName);
58
        $rolesData = Yaml::parse($fileName);
59
60
        foreach ($rolesData as $roleName => $roleConfigData) {
0 ignored issues
show
Bug introduced by
The expression $rolesData of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
61
            if (isset($roleConfigData['bap_role'])) {
62
                $role = $manager->getRepository('OroUserBundle:Role')
0 ignored issues
show
Bug introduced by
The method getRepository() does not seem to exist on object<Oro\Bundle\Securi...Persistence\AclManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
                    ->findOneBy(['role' => $roleConfigData['bap_role']]);
64
            } else {
65
                $role = new Role($roleName);
66
            }
67
68
            $role->setLabel($roleConfigData['label']);
69
            $manager->persist($role);
0 ignored issues
show
Bug introduced by
The method persist() does not seem to exist on object<Oro\Bundle\Securi...Persistence\AclManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
71
            if ($aclManager->isAclEnabled()) {
72
                $sid = $aclManager->getSid($role);
73
                foreach ($roleConfigData['permissions'] as $permission => $acls) {
74
                    $this->processPermission($aclManager, $sid, $permission, $acls);
75
                }
76
            }
77
        }
78
79
        $aclManager->flush();
80
        $manager->flush();
81
    }
82
83
    /**
84
     * @param AclManager $aclManager
85
     * @param mixed $sid
86
     * @param string $permission
87
     * @param array $acls
88
     */
89
    protected function processPermission(
90
        AclManager $aclManager,
91
        SecurityIdentityInterface $sid,
92
        $permission,
93
        array $acls
94
    ) {
95
        $oid = $aclManager->getOid(str_replace('|', ':', $permission));
96
97
        $extension = $aclManager->getExtensionSelector()->select($oid);
98
        $maskBuilders = $extension->getAllMaskBuilders();
99
100
        foreach ($maskBuilders as $maskBuilder) {
101
            $mask = $maskBuilder->reset()->get();
102
103
            if (!empty($acls)) {
104
                foreach ($acls as $acl) {
105
                    if ($maskBuilder->hasMask('MASK_' . $acl)) {
106
                        $mask = $maskBuilder->add($acl)->get();
107
                    }
108
                }
109
            }
110
111
            $aclManager->setPermission($sid, $oid, $mask);
112
        }
113
    }
114
}
115