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) { |
|
|
|
|
61
|
|
|
if (isset($roleConfigData['bap_role'])) { |
62
|
|
|
$role = $manager->getRepository('OroUserBundle:Role') |
|
|
|
|
63
|
|
|
->findOneBy(['role' => $roleConfigData['bap_role']]); |
64
|
|
|
} else { |
65
|
|
|
$role = new Role($roleName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$role->setLabel($roleConfigData['label']); |
69
|
|
|
$manager->persist($role); |
|
|
|
|
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
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.