These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Oro\Bundle\OrganizationBundle\Ownership; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider; |
||
7 | use Oro\Bundle\OrganizationBundle\Entity\Organization; |
||
8 | use Oro\Bundle\OrganizationBundle\Form\Type\OwnershipType; |
||
9 | use Oro\Bundle\SecurityBundle\Acl\Domain\ObjectIdAccessor; |
||
10 | use Oro\Bundle\SecurityBundle\Owner\Metadata\OwnershipMetadataProvider; |
||
11 | |||
12 | class OwnerDeletionManager |
||
13 | { |
||
14 | /** |
||
15 | * @var ConfigProvider |
||
16 | */ |
||
17 | protected $ownershipProvider; |
||
18 | |||
19 | /** |
||
20 | * @var OwnershipMetadataProvider |
||
21 | */ |
||
22 | protected $ownershipMetadata; |
||
23 | |||
24 | /** |
||
25 | * @var EntityManager |
||
26 | */ |
||
27 | protected $em; |
||
28 | |||
29 | /** |
||
30 | * @var ObjectIdAccessor |
||
31 | */ |
||
32 | protected $objectIdAccessor; |
||
33 | |||
34 | /** |
||
35 | * @var OwnerAssignmentCheckerInterface |
||
36 | */ |
||
37 | protected $defaultChecker; |
||
38 | |||
39 | /** |
||
40 | * @var OwnerAssignmentCheckerInterface[] |
||
41 | */ |
||
42 | protected $checkers = []; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param OwnerAssignmentCheckerInterface $defaultChecker |
||
48 | * @param ConfigProvider $ownershipProvider |
||
49 | * @param OwnershipMetadataProvider $ownershipMetadata |
||
50 | * @param EntityManager $em |
||
51 | * @param ObjectIdAccessor $objectIdAccessor |
||
52 | */ |
||
53 | public function __construct( |
||
54 | OwnerAssignmentCheckerInterface $defaultChecker, |
||
55 | ConfigProvider $ownershipProvider, |
||
56 | OwnershipMetadataProvider $ownershipMetadata, |
||
57 | EntityManager $em, |
||
58 | ObjectIdAccessor $objectIdAccessor |
||
59 | ) { |
||
60 | $this->defaultChecker = $defaultChecker; |
||
61 | $this->ownershipProvider = $ownershipProvider; |
||
62 | $this->ownershipMetadata = $ownershipMetadata; |
||
63 | $this->em = $em; |
||
64 | $this->objectIdAccessor = $objectIdAccessor; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Registers an object responsible for check owner assignment for the given entity class |
||
69 | * |
||
70 | * @param string $entityClassName |
||
71 | * @param OwnerAssignmentCheckerInterface $checker |
||
72 | */ |
||
73 | public function registerAssignmentChecker($entityClassName, OwnerAssignmentCheckerInterface $checker) |
||
74 | { |
||
75 | $this->checkers[$entityClassName] = $checker; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Determines whether the given entity is an owner. |
||
80 | * |
||
81 | * @param object $entity |
||
82 | * |
||
83 | * @return bool true if the given entity is User, BusinessUnit or Organization; otherwise, false |
||
84 | */ |
||
85 | public function isOwner($entity) |
||
86 | { |
||
87 | return $this->getOwnerType($entity) !== OwnershipType::OWNER_TYPE_NONE; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Checks if the given owner owns at least one entity |
||
92 | * |
||
93 | * @param object $owner |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function hasAssignments($owner) |
||
98 | { |
||
99 | $ownerType = $this->getOwnerType($owner); |
||
100 | if ($ownerType !== OwnershipType::OWNER_TYPE_NONE) { |
||
101 | foreach ($this->ownershipProvider->getConfigs(null, true) as $config) { |
||
102 | if ($config->get('owner_type') === $ownerType) { |
||
103 | $entityClassName = $config->getId()->getClassName(); |
||
104 | $result = $this->getAssignmentChecker($entityClassName)->hasAssignments( |
||
105 | $this->objectIdAccessor->getId($owner), |
||
106 | $entityClassName, |
||
107 | $this->ownershipMetadata->getMetadata($entityClassName)->getOwnerFieldName(), |
||
108 | $this->em |
||
109 | ); |
||
110 | if ($result) { |
||
111 | return true; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return false; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Checks if the given organization owns at least one entity |
||
122 | * |
||
123 | * @param Organization $organization |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function hasOrganizationAssignments(Organization $organization) |
||
128 | { |
||
129 | foreach ($this->ownershipProvider->getConfigs(null, true) as $config) { |
||
130 | if (in_array( |
||
131 | $config->get('owner_type'), |
||
132 | [OwnershipType::OWNER_TYPE_USER, OwnershipType::OWNER_TYPE_BUSINESS_UNIT], |
||
133 | true |
||
134 | )) { |
||
135 | $entityClassName = $config->getId()->getClassName(); |
||
136 | $organizationFieldName = $this->ownershipMetadata |
||
137 | ->getMetadata($entityClassName) |
||
138 | ->getGlobalOwnerFieldName(); |
||
139 | $findResult = $this->em->getRepository($entityClassName) |
||
140 | ->createQueryBuilder('entity') |
||
141 | ->select('organization.id') |
||
142 | ->innerJoin(sprintf('entity.%s', $organizationFieldName), 'organization') |
||
143 | ->where('organization.id = :ownerId') |
||
144 | ->setParameter('ownerId', $this->objectIdAccessor->getId($organization)) |
||
145 | ->setMaxResults(1) |
||
146 | ->getQuery() |
||
147 | ->getArrayResult(); |
||
148 | |||
149 | if ($findResult) { |
||
0 ignored issues
–
show
|
|||
150 | return true; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return false; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Gets an instance of OwnerAssignmentCheckerInterface responsible |
||
160 | * for check owner assignment for the given entity class |
||
161 | * |
||
162 | * @param string $entityClassName |
||
163 | * |
||
164 | * @return OwnerAssignmentCheckerInterface |
||
165 | */ |
||
166 | protected function getAssignmentChecker($entityClassName) |
||
167 | { |
||
168 | return isset($this->checkers[$entityClassName]) |
||
169 | ? $this->checkers[$entityClassName] |
||
170 | : $this->defaultChecker; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Gets a string represents the type of the given owner |
||
175 | * |
||
176 | * @param mixed $owner |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function getOwnerType($owner) |
||
181 | { |
||
182 | if (is_a($owner, $this->ownershipMetadata->getUserClass())) { |
||
183 | return OwnershipType::OWNER_TYPE_USER; |
||
184 | } |
||
185 | |||
186 | if (is_a($owner, $this->ownershipMetadata->getBusinessUnitClass())) { |
||
187 | return OwnershipType::OWNER_TYPE_BUSINESS_UNIT; |
||
188 | } |
||
189 | |||
190 | if (is_a($owner, $this->ownershipMetadata->getOrganizationClass())) { |
||
191 | return OwnershipType::OWNER_TYPE_ORGANIZATION; |
||
192 | } |
||
193 | |||
194 | return OwnershipType::OWNER_TYPE_NONE; |
||
195 | } |
||
196 | } |
||
197 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.