forked from oroinc/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnerTreeProvider.php
162 lines (138 loc) · 4.59 KB
/
OwnerTreeProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Oro\Bundle\SecurityBundle\Owner;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\ORM\EntityManager;
use Oro\Bundle\OrganizationBundle\Entity\BusinessUnit;
use Oro\Bundle\SecurityBundle\Owner\Metadata\OwnershipMetadataProvider;
use Oro\Bundle\UserBundle\Entity\User;
/**
* Class OwnerTreeProvider
* @package Oro\Bundle\SecurityBundle\Owner
*/
class OwnerTreeProvider extends AbstractOwnerTreeProvider
{
/**
* @deprecated 1.8.0:2.1.0 use AbstractOwnerTreeProvider::CACHE_KEY instead
*/
const CACHE_KEY = 'data';
/**
* @var EntityManager
*
* @deprecated 1.8.0:2.1.0 use AbstractOwnerTreeProvider::getManagerForClass instead
*/
protected $em;
/** @var CacheProvider */
private $cache;
/** @var OwnershipMetadataProvider */
private $ownershipMetadataProvider;
/**
* {@inheritdoc}
*/
public function getCache()
{
if (!$this->cache) {
$this->cache = $this->getContainer()->get('oro_security.ownership_tree_provider.cache');
}
return $this->cache;
}
/**
* {@inheritdoc}
*/
protected function getTreeData()
{
return new OwnerTree();
}
/**
* {@inheritdoc}
*/
public function supports()
{
return $this->getContainer()->get('oro_security.security_facade')->getLoggedUser() instanceof User;
}
/**
* {@inheritdoc}
*/
public function getTree()
{
return parent::getTree();
}
/**
* @param EntityManager $em
* @param CacheProvider $cache
*
* @deprecated 1.8.0:2.1.0 use AbstractOwnerTreeProvider::getContainer instead
*/
public function __construct(EntityManager $em, CacheProvider $cache)
{
$this->cache = $cache;
$this->em = $em;
}
/**
* {@inheritdoc}
*/
protected function fillTree(OwnerTreeInterface $tree)
{
$userClass = $this->getOwnershipMetadataProvider()->getBasicLevelClass();
$businessUnitClass = $this->getOwnershipMetadataProvider()->getLocalLevelClass();
/** @var User[] $users */
$users = $this->getManagerForClass($userClass)
->getRepository($userClass)
->createQueryBuilder('u')
->leftJoin('u.owner', 'owner')
->leftJoin('u.organizations', 'organizations')
->leftJoin('u.businessUnits', 'bu')
->leftJoin('bu.organization', 'bu_organization')
->select(
'partial u.{id}',
'partial owner.{id}',
'partial organizations.{id}',
'partial bu.{id}',
'partial bu_organization.{id}'
)
->getQuery()
->getArrayResult();
/** @var BusinessUnit[] $businessUnits */
$businessUnitsRepo = $this->getManagerForClass($businessUnitClass)->getRepository($businessUnitClass);
$businessUnits = $businessUnitsRepo
->createQueryBuilder('bu')
->select([
'bu.id',
'IDENTITY(bu.organization) organization',
'IDENTITY(bu.owner) owner' //aka parent business unit
])
->getQuery()
->getArrayResult();
foreach ($businessUnits as $businessUnit) {
if (!empty($businessUnit['organization'])) {
$tree->addLocalEntity($businessUnit['id'], (int)$businessUnit['organization']);
if ($businessUnit['owner']) {
$tree->addDeepEntity($businessUnit['id'], $businessUnit['owner']);
}
}
}
$tree->buildTree();
foreach ($users as $user) {
$owner = $user['owner'];
$tree->addBasicEntity($user['id'], isset($owner['id']) ? $owner['id'] : null);
foreach ($user['organizations'] as $organization) {
$tree->addGlobalEntity($user['id'], $organization['id']);
foreach ($user['businessUnits'] as $businessUnit) {
if ($organization['id'] == $businessUnit['organization']['id']) {
$tree->addLocalEntityToBasic($user['id'], $businessUnit['id'], $organization['id']);
}
}
}
}
}
/**
* {@inheritdoc}
*/
protected function getOwnershipMetadataProvider()
{
if (!$this->ownershipMetadataProvider) {
$this->ownershipMetadataProvider = $this->getContainer()
->get('oro_security.owner.ownership_metadata_provider');
}
return $this->ownershipMetadataProvider;
}
}