Completed
Push — master ( 43fb10...664b94 )
by
unknown
12:57
created

UpdateTaskAccessLevels::updateUserRole()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 6
eloc 26
nc 6
nop 1
1
<?php
2
3
namespace Oro\Bridge\CrmTask\Migrations\Data\Demo\ORM;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
7
use Doctrine\Common\Persistence\ObjectManager;
8
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
use Oro\Bundle\SecurityBundle\Acl\Persistence\AclManager;
13
use Oro\Bundle\UserBundle\Entity\Role;
14
15
class UpdateTaskAccessLevels extends AbstractFixture implements ContainerAwareInterface, DependentFixtureInterface
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    protected $container;
21
22
    /**
23
     * @var ObjectManager
24
     */
25
    protected $objectManager;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getDependencies()
31
    {
32
        return ['OroCRM\Bundle\DemoDataBundle\Migrations\Data\ORM\LoadRolesData'];
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function setContainer(ContainerInterface $container = null)
39
    {
40
        $this->container = $container;
41
    }
42
43
    /**
44
     * Load ACL for security roles
45
     *
46
     * @param ObjectManager $manager
47
     */
48 View Code Duplication
    public function load(ObjectManager $manager)
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...
49
    {
50
        $this->objectManager = $manager;
51
52
        /** @var AclManager $aclManager */
53
        $aclManager = $this->container->get('oro_security.acl.manager');
54
55
        if ($aclManager->isAclEnabled()) {
56
            $this->updateUserRole($aclManager);
57
            $aclManager->flush();
58
        }
59
    }
60
61
    protected function updateUserRole(AclManager $manager)
62
    {
63
        $roles = [
64
            'ROLE_SALES_MANAGER',
65
            'ROLE_SALES_REP',
66
            'ROLE_ONLINE_SALES_REP',
67
            'ROLE_MARKETING_MANAGER',
68
            'ROLE_LEADS_DEVELOPMENT_REP',
69
        ];
70
        $acls = [
71
            'CREATE_SYSTEM',
72
            'VIEW_SYSTEM',
73
            'EDIT_SYSTEM',
74
            'DELETE_SYSTEM',
75
            'ASSIGN_SYSTEM'
76
        ];
77
        foreach ($roles as $roleName) {
78
            $role = $this->getRole($roleName);
79
            if ($role) {
80
                $sid = $manager->getSid($role);
81
                $oid = $manager->getOid('entity:OroCRM\Bundle\TaskBundle\Entity\Task');
82
                $extension = $manager->getExtensionSelector()->select($oid);
83
                $maskBuilders = $extension->getAllMaskBuilders();
84
85
                foreach ($maskBuilders as $maskBuilder) {
86
                    $mask = $maskBuilder->reset()->get();
87
88
                    foreach ($acls as $acl) {
89
                        if ($maskBuilder->hasMask('MASK_' . $acl)) {
90
                            $mask = $maskBuilder->add($acl)->get();
91
                        }
92
                    }
93
94
                    $manager->setPermission($sid, $oid, $mask);
95
                }
96
            }
97
        }
98
    }
99
100
    /**
101
     * @param string $roleName
102
     * @return Role|null
103
     */
104
    protected function getRole($roleName)
105
    {
106
        return $this->objectManager->getRepository('OroUserBundle:Role')->findOneBy(['role' => $roleName]);
107
    }
108
}
109