Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway;
8
9 use eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway;
10 use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
11 use eZ\Publish\SPI\Persistence\User\Policy;
12 use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
13 use eZ\Publish\SPI\Persistence\User\Role;
14
18 class DoctrineDatabase extends Gateway
19 {
25 protected $handler;
26
32 public function __construct(DatabaseHandler $handler)
33 {
34 $this->handler = $handler;
35 }
36
44 public function createRole(Role $role)
45 {
46 // Role original ID is set when creating a draft from an existing role
47 if ($role->status === Role::STATUS_DRAFT && $role->id) {
48 $roleOriginalId = $role->id;
49 } elseif ($role->status === Role::STATUS_DRAFT) {
50 // Not using a constant here as this is legacy storage engine specific.
51 // -1 means "Newly created role".
52 $roleOriginalId = -1;
53 } else {
54 // Role::STATUS_DEFINED value is 0, which is the expected value for version column for this status.
55 $roleOriginalId = Role::STATUS_DEFINED;
56 }
57
58 $query = $this->handler->createInsertQuery();
59 $query
60 ->insertInto($this->handler->quoteTable('ezrole'))
61 ->set(
62 $this->handler->quoteColumn('id'),
63 $this->handler->getAutoIncrementValue('ezrole', 'id')
64 )->set(
65 $this->handler->quoteColumn('is_new'),
66 0
67 )->set(
68 $this->handler->quoteColumn('name'),
69 $query->bindValue($role->identifier)
70 )->set(
71 $this->handler->quoteColumn('value'),
72 0
73 )->set(
74 // Column name "version" is misleading here as it stores originalId when creating a draft from an existing role.
75 // But hey, this is legacy! :-)
76 $this->handler->quoteColumn('version'),
77 $query->bindValue($roleOriginalId)
78 );
79 $query->prepare()->execute();
80
81 if (!isset($role->id) || (int)$role->id < 1 || $role->status === Role::STATUS_DRAFT) {
82 $role->id = $this->handler->lastInsertId(
83 $this->handler->getSequenceName('ezrole', 'id')
84 );
85 }
86
87 $role->originalId = $roleOriginalId;
88 }
89
98 public function loadRole($roleId, $status = Role::STATUS_DEFINED)
99 {
100 $query = $this->handler->createSelectQuery();
101 if ($status === Role::STATUS_DEFINED) {
102 $draftCondition = $query->expr->eq(
103 $this->handler->quoteColumn('version', 'ezrole'),
104 $query->bindValue($status, null, \PDO::PARAM_INT)
105 );
106 } else {
107 $draftCondition = $query->expr->neq(
108 $this->handler->quoteColumn('version', 'ezrole'),
109 $query->bindValue(Role::STATUS_DEFINED, null, \PDO::PARAM_INT)
110 );
111 }
112
113 $query->select(
114 $this->handler->aliasedColumn($query, 'id', 'ezrole'),
115 $this->handler->aliasedColumn($query, 'name', 'ezrole'),
116 $this->handler->aliasedColumn($query, 'version', 'ezrole'),
117 $this->handler->aliasedColumn($query, 'id', 'ezpolicy'),
118 $this->handler->aliasedColumn($query, 'function_name', 'ezpolicy'),
119 $this->handler->aliasedColumn($query, 'module_name', 'ezpolicy'),
120 $this->handler->aliasedColumn($query, 'original_id', 'ezpolicy'),
121 $this->handler->aliasedColumn($query, 'identifier', 'ezpolicy_limitation'),
122 $this->handler->aliasedColumn($query, 'value', 'ezpolicy_limitation_value')
123 )->from(
124 $this->handler->quoteTable('ezrole')
125 )->leftJoin(
126 $this->handler->quoteTable('ezpolicy'),
127 $query->expr->eq(
128 $this->handler->quoteColumn('role_id', 'ezpolicy'),
129 $this->handler->quoteColumn('id', 'ezrole')
130 )
131 )->leftJoin(
132 $this->handler->quoteTable('ezpolicy_limitation'),
133 $query->expr->eq(
134 $this->handler->quoteColumn('policy_id', 'ezpolicy_limitation'),
135 $this->handler->quoteColumn('id', 'ezpolicy')
136 )
137 )->leftJoin(
138 $this->handler->quoteTable('ezpolicy_limitation_value'),
139 $query->expr->eq(
140 $this->handler->quoteColumn('limitation_id', 'ezpolicy_limitation_value'),
141 $this->handler->quoteColumn('id', 'ezpolicy_limitation')
142 )
143 )->where(
144 $query->expr->lAnd(
145 $query->expr->eq(
146 $this->handler->quoteColumn('id', 'ezrole'),
147 $query->bindValue($roleId, null, \PDO::PARAM_INT)
148 ),
149 $draftCondition
150 )
151 );
152
153 $statement = $query->prepare();
154 $statement->execute();
155
156 return $statement->fetchAll(\PDO::FETCH_ASSOC);
157 }
158
167 public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED)
168 {
169 $query = $this->handler->createSelectQuery();
170 if ($status === Role::STATUS_DEFINED) {
171 $roleVersionCondition = $query->expr->eq(
172 $this->handler->quoteColumn('version', 'ezrole'),
173 $query->bindValue($status, null, \PDO::PARAM_INT)
174 );
175 } else {
176 $roleVersionCondition = $query->expr->neq(
177 $this->handler->quoteColumn('version', 'ezrole'),
178 $query->bindValue($status, null, \PDO::PARAM_INT)
179 );
180 }
181
182 $query->select(
183 $this->handler->aliasedColumn($query, 'id', 'ezrole'),
184 $this->handler->aliasedColumn($query, 'name', 'ezrole'),
185 $this->handler->aliasedColumn($query, 'version', 'ezrole'),
186 $this->handler->aliasedColumn($query, 'id', 'ezpolicy'),
187 $this->handler->aliasedColumn($query, 'function_name', 'ezpolicy'),
188 $this->handler->aliasedColumn($query, 'module_name', 'ezpolicy'),
189 $this->handler->aliasedColumn($query, 'original_id', 'ezpolicy'),
190 $this->handler->aliasedColumn($query, 'identifier', 'ezpolicy_limitation'),
191 $this->handler->aliasedColumn($query, 'value', 'ezpolicy_limitation_value')
192 )->from(
193 $this->handler->quoteTable('ezrole')
194 )->leftJoin(
195 $this->handler->quoteTable('ezpolicy'),
196 $query->expr->eq(
197 $this->handler->quoteColumn('role_id', 'ezpolicy'),
198 $this->handler->quoteColumn('id', 'ezrole')
199 )
200 )->leftJoin(
201 $this->handler->quoteTable('ezpolicy_limitation'),
202 $query->expr->eq(
203 $this->handler->quoteColumn('policy_id', 'ezpolicy_limitation'),
204 $this->handler->quoteColumn('id', 'ezpolicy')
205 )
206 )->leftJoin(
207 $this->handler->quoteTable('ezpolicy_limitation_value'),
208 $query->expr->eq(
209 $this->handler->quoteColumn('limitation_id', 'ezpolicy_limitation_value'),
210 $this->handler->quoteColumn('id', 'ezpolicy_limitation')
211 )
212 )->where(
213 $query->expr->lAnd(
214 $query->expr->eq(
215 $this->handler->quoteColumn('name', 'ezrole'),
216 $query->bindValue($identifier, null, \PDO::PARAM_STR)
217 ),
218 $roleVersionCondition
219 )
220 );
221
222 $statement = $query->prepare();
223 $statement->execute();
224
225 return $statement->fetchAll(\PDO::FETCH_ASSOC);
226 }
227
235 public function loadRoleDraftByRoleId($roleId)
236 {
237 $query = $this->handler->createSelectQuery();
238 $query->select(
239 $this->handler->aliasedColumn($query, 'id', 'ezrole'),
240 $this->handler->aliasedColumn($query, 'name', 'ezrole'),
241 $this->handler->aliasedColumn($query, 'version', 'ezrole'),
242 $this->handler->aliasedColumn($query, 'id', 'ezpolicy'),
243 $this->handler->aliasedColumn($query, 'function_name', 'ezpolicy'),
244 $this->handler->aliasedColumn($query, 'module_name', 'ezpolicy'),
245 $this->handler->aliasedColumn($query, 'original_id', 'ezpolicy'),
246 $this->handler->aliasedColumn($query, 'identifier', 'ezpolicy_limitation'),
247 $this->handler->aliasedColumn($query, 'value', 'ezpolicy_limitation_value')
248 )->from(
249 $this->handler->quoteTable('ezrole')
250 )->leftJoin(
251 $this->handler->quoteTable('ezpolicy'),
252 $query->expr->eq(
253 $this->handler->quoteColumn('role_id', 'ezpolicy'),
254 $this->handler->quoteColumn('id', 'ezrole')
255 )
256 )->leftJoin(
257 $this->handler->quoteTable('ezpolicy_limitation'),
258 $query->expr->eq(
259 $this->handler->quoteColumn('policy_id', 'ezpolicy_limitation'),
260 $this->handler->quoteColumn('id', 'ezpolicy')
261 )
262 )->leftJoin(
263 $this->handler->quoteTable('ezpolicy_limitation_value'),
264 $query->expr->eq(
265 $this->handler->quoteColumn('limitation_id', 'ezpolicy_limitation_value'),
266 $this->handler->quoteColumn('id', 'ezpolicy_limitation')
267 )
268 )->where(
269 $query->expr->eq(
270 // Column name "version" is misleading as it stores originalId when creating a draft from an existing role.
271 // But hey, this is legacy! :-)
272 $this->handler->quoteColumn('version', 'ezrole'),
273 $query->bindValue($roleId, null, \PDO::PARAM_STR)
274 )
275 );
276
277 $statement = $query->prepare();
278 $statement->execute();
279
280 return $statement->fetchAll(\PDO::FETCH_ASSOC);
281 }
282
290 public function loadRoles($status = Role::STATUS_DEFINED)
291 {
292 $query = $this->handler->createSelectQuery();
293 if ($status === Role::STATUS_DEFINED) {
294 $roleVersionCondition = $query->expr->eq(
295 $this->handler->quoteColumn('version', 'ezrole'),
296 $query->bindValue($status, null, \PDO::PARAM_INT)
297 );
298 } else {
299 $roleVersionCondition = $query->expr->neq(
300 $this->handler->quoteColumn('version', 'ezrole'),
301 $query->bindValue($status, null, \PDO::PARAM_INT)
302 );
303 }
304
305 $query->select(
306 $this->handler->aliasedColumn($query, 'id', 'ezrole'),
307 $this->handler->aliasedColumn($query, 'name', 'ezrole'),
308 $this->handler->aliasedColumn($query, 'version', 'ezrole'),
309 $this->handler->aliasedColumn($query, 'contentobject_id', 'ezuser_role'),
310 $this->handler->aliasedColumn($query, 'id', 'ezpolicy'),
311 $this->handler->aliasedColumn($query, 'function_name', 'ezpolicy'),
312 $this->handler->aliasedColumn($query, 'module_name', 'ezpolicy'),
313 $this->handler->aliasedColumn($query, 'original_id', 'ezpolicy'),
314 $this->handler->aliasedColumn($query, 'identifier', 'ezpolicy_limitation'),
315 $this->handler->aliasedColumn($query, 'value', 'ezpolicy_limitation_value')
316 )->from(
317 $this->handler->quoteTable('ezrole')
318 )->leftJoin(
319 $this->handler->quoteTable('ezuser_role'),
320 $query->expr->eq(
321 $this->handler->quoteColumn('role_id', 'ezuser_role'),
322 $this->handler->quoteColumn('id', 'ezrole')
323 )
324 )->leftJoin(
325 $this->handler->quoteTable('ezpolicy'),
326 $query->expr->eq(
327 $this->handler->quoteColumn('role_id', 'ezpolicy'),
328 $this->handler->quoteColumn('id', 'ezrole')
329 )
330 )->leftJoin(
331 $this->handler->quoteTable('ezpolicy_limitation'),
332 $query->expr->eq(
333 $this->handler->quoteColumn('policy_id', 'ezpolicy_limitation'),
334 $this->handler->quoteColumn('id', 'ezpolicy')
335 )
336 )->leftJoin(
337 $this->handler->quoteTable('ezpolicy_limitation_value'),
338 $query->expr->eq(
339 $this->handler->quoteColumn('limitation_id', 'ezpolicy_limitation_value'),
340 $this->handler->quoteColumn('id', 'ezpolicy_limitation')
341 )
342 )->where($roleVersionCondition);
343
344 $statement = $query->prepare();
345 $statement->execute();
346
347 return $statement->fetchAll(\PDO::FETCH_ASSOC);
348 }
349
358 public function loadRolesForContentObjects($contentIds, $status = Role::STATUS_DEFINED)
359 {
360 $query = $this->handler->createSelectQuery();
361 if ($status === Role::STATUS_DEFINED) {
362 $roleIdCondition = $query->expr->eq(
363 $this->handler->quoteColumn('id', 'ezrole'),
364 $this->handler->quoteColumn('role_id', 'ezuser_role_search')
365 );
366 } else {
367 $roleIdCondition = $query->expr->eq(
368 $this->handler->quoteColumn('version', 'ezrole'),
369 $this->handler->quoteColumn('role_id', 'ezuser_role_search')
370 );
371 }
372
373 $query->select(
374 $this->handler->aliasedColumn($query, 'contentobject_id', 'ezuser_role'),
375 $this->handler->aliasedColumn($query, 'id', 'ezrole'),
376 $this->handler->aliasedColumn($query, 'name', 'ezrole'),
377 $this->handler->aliasedColumn($query, 'version', 'ezrole'),
378 $this->handler->aliasedColumn($query, 'id', 'ezpolicy'),
379 $this->handler->aliasedColumn($query, 'function_name', 'ezpolicy'),
380 $this->handler->aliasedColumn($query, 'module_name', 'ezpolicy'),
381 $this->handler->aliasedColumn($query, 'original_id', 'ezpolicy'),
382 $this->handler->aliasedColumn($query, 'identifier', 'ezpolicy_limitation'),
383 $this->handler->aliasedColumn($query, 'value', 'ezpolicy_limitation_value')
384 )->from(
385 $query->alias(
386 $this->handler->quoteTable('ezuser_role'),
387 $this->handler->quoteIdentifier('ezuser_role_search')
388 )
389 )->leftJoin(
390 $this->handler->quoteTable('ezrole'),
391 $roleIdCondition
392 )->leftJoin(
393 $this->handler->quoteTable('ezuser_role'),
394 $query->expr->eq(
395 $this->handler->quoteColumn('role_id', 'ezuser_role'),
396 $this->handler->quoteColumn('id', 'ezrole')
397 )
398 )->leftJoin(
399 $this->handler->quoteTable('ezpolicy'),
400 $query->expr->eq(
401 $this->handler->quoteColumn('role_id', 'ezpolicy'),
402 $this->handler->quoteColumn('id', 'ezrole')
403 )
404 )->leftJoin(
405 $this->handler->quoteTable('ezpolicy_limitation'),
406 $query->expr->eq(
407 $this->handler->quoteColumn('policy_id', 'ezpolicy_limitation'),
408 $this->handler->quoteColumn('id', 'ezpolicy')
409 )
410 )->leftJoin(
411 $this->handler->quoteTable('ezpolicy_limitation_value'),
412 $query->expr->eq(
413 $this->handler->quoteColumn('limitation_id', 'ezpolicy_limitation_value'),
414 $this->handler->quoteColumn('id', 'ezpolicy_limitation')
415 )
416 )->where(
417 $query->expr->in(
418 $this->handler->quoteColumn('contentobject_id', 'ezuser_role_search'),
419 $contentIds
420 )
421 );
422
423 $statement = $query->prepare();
424 $statement->execute();
425
426 return $statement->fetchAll(\PDO::FETCH_ASSOC);
427 }
428
436 public function loadRoleAssignment($roleAssignmentId)
437 {
438 $query = $this->handler->createSelectQuery();
439 $query->select(
440 $this->handler->quoteColumn('id'),
441 $this->handler->quoteColumn('contentobject_id'),
442 $this->handler->quoteColumn('limit_identifier'),
443 $this->handler->quoteColumn('limit_value'),
444 $this->handler->quoteColumn('role_id')
445 )->from(
446 $this->handler->quoteTable('ezuser_role')
447 )->where(
448 $query->expr->eq(
449 $this->handler->quoteColumn('id'),
450 $query->bindValue($roleAssignmentId, null, \PDO::PARAM_INT)
451 )
452 );
453
454 $statement = $query->prepare();
455 $statement->execute();
456
457 return $statement->fetchAll(\PDO::FETCH_ASSOC);
458 }
459
468 public function loadRoleAssignmentsByGroupId($groupId, $inherited = false)
469 {
470 $query = $this->handler->createSelectQuery();
471 $query->select(
472 $this->handler->quoteColumn('id'),
473 $this->handler->quoteColumn('contentobject_id'),
474 $this->handler->quoteColumn('limit_identifier'),
475 $this->handler->quoteColumn('limit_value'),
476 $this->handler->quoteColumn('role_id')
477 )->from(
478 $this->handler->quoteTable('ezuser_role')
479 );
480
481 if ($inherited) {
482 $groupIds = $this->fetchUserGroups($groupId);
483 $groupIds[] = $groupId;
484 $query->where(
485 $query->expr->in(
486 $this->handler->quoteColumn('contentobject_id'),
487 $groupIds
488 )
489 );
490 } else {
491 $query->where(
492 $query->expr->eq(
493 $this->handler->quoteColumn('contentobject_id'),
494 $query->bindValue($groupId, null, \PDO::PARAM_INT)
495 )
496 );
497 }
498
499 $statement = $query->prepare();
500 $statement->execute();
501
502 return $statement->fetchAll(\PDO::FETCH_ASSOC);
503 }
504
512 public function loadRoleAssignmentsByRoleId($roleId)
513 {
514 $query = $this->handler->createSelectQuery();
515 $query->select(
516 $this->handler->quoteColumn('id'),
517 $this->handler->quoteColumn('contentobject_id'),
518 $this->handler->quoteColumn('limit_identifier'),
519 $this->handler->quoteColumn('limit_value'),
520 $this->handler->quoteColumn('role_id')
521 )->from(
522 $this->handler->quoteTable('ezuser_role')
523 )->where(
524 $query->expr->eq(
525 $this->handler->quoteColumn('role_id'),
526 $query->bindValue($roleId, null, \PDO::PARAM_INT)
527 )
528 );
529
530 $statement = $query->prepare();
531 $statement->execute();
532
533 return $statement->fetchAll(\PDO::FETCH_ASSOC);
534 }
535
543 public function loadPoliciesByUserId($userId)
544 {
545 $groupIds = $this->fetchUserGroups($userId);
546 $groupIds[] = $userId;
547
548 return $this->loadRolesForContentObjects($groupIds);
549 }
550
562 protected function fetchUserGroups($userId)
563 {
564 $query = $this->handler->createSelectQuery();
565 $query->select(
566 $this->handler->quoteColumn('path_string', 'ezcontentobject_tree')
567 )->from(
568 $this->handler->quoteTable('ezcontentobject_tree')
569 )->where(
570 $query->expr->eq(
571 $this->handler->quoteColumn('contentobject_id', 'ezcontentobject_tree'),
572 $query->bindValue($userId)
573 )
574 );
575
576 $statement = $query->prepare();
577 $statement->execute();
578
579 $paths = $statement->fetchAll(\PDO::FETCH_COLUMN);
580 $nodeIDs = array_unique(
581 array_reduce(
582 array_map(
583 function ($pathString) {
584 return array_filter(explode('/', $pathString));
585 },
586 $paths
587 ),
588 'array_merge_recursive',
589 []
590 )
591 );
592
593 if (empty($nodeIDs)) {
594 return [];
595 }
596
597 $query = $this->handler->createSelectQuery();
598 $query->select(
599 $this->handler->quoteColumn('id', 'ezcontentobject')
600 )->from(
601 $this->handler->quoteTable('ezcontentobject_tree')
602 )->innerJoin(
603 $this->handler->quoteTable('ezcontentobject'),
604 $query->expr->eq(
605 $this->handler->quoteColumn('id', 'ezcontentobject'),
606 $this->handler->quoteColumn('contentobject_id', 'ezcontentobject_tree')
607 )
608 )->where(
609 $query->expr->in(
610 $this->handler->quoteColumn('node_id', 'ezcontentobject_tree'),
611 $nodeIDs
612 )
613 );
614
615 $statement = $query->prepare();
616 $statement->execute();
617
618 return $statement->fetchAll(\PDO::FETCH_COLUMN);
619 }
620
630 public function updateRole(RoleUpdateStruct $role)
631 {
632 $query = $this->handler->createUpdateQuery();
633 $query
634 ->update($this->handler->quoteTable('ezrole'))
635 ->set(
636 $this->handler->quoteColumn('name'),
637 $query->bindValue($role->identifier)
638 )->where(
639 $query->expr->eq(
640 $this->handler->quoteColumn('id'),
641 $query->bindValue($role->id, null, \PDO::PARAM_INT)
642 )
643 );
644 $statement = $query->prepare();
645 $statement->execute();
646
647 // Commented due to EZP-24698: Role update leads to NotFoundException
648 // Should be fixed with PDO::MYSQL_ATTR_FOUND_ROWS instead
649 /*if ($statement->rowCount() < 1) {
650 throw new NotFoundException('role', $role->id);
651 }*/
652 }
653
661 public function deleteRole($roleId, $status = Role::STATUS_DEFINED)
662 {
663 $deleteRoleQuery = $this->handler->createDeleteQuery();
664 if ($status !== Role::STATUS_DRAFT) {
665 $deleteAssignmentsQuery = $this->handler->createDeleteQuery();
666 $deleteAssignmentsQuery
667 ->deleteFrom($this->handler->quoteTable('ezuser_role'))
668 ->where(
669 $deleteAssignmentsQuery->expr->eq(
670 $this->handler->quoteColumn('role_id'),
671 $deleteAssignmentsQuery->bindValue($roleId, null, \PDO::PARAM_INT)
672 )
673 );
674
675 $draftCondition = $deleteRoleQuery->expr->eq(
676 $this->handler->quoteColumn('version', 'ezrole'),
677 $deleteRoleQuery->bindValue(Role::STATUS_DEFINED, null, \PDO::PARAM_INT)
678 );
679 } else {
680 $draftCondition = $deleteRoleQuery->expr->neq(
681 $this->handler->quoteColumn('version', 'ezrole'),
682 $deleteRoleQuery->bindValue(Role::STATUS_DEFINED, null, \PDO::PARAM_INT)
683 );
684 }
685
686 $deleteRoleQuery
687 ->deleteFrom($this->handler->quoteTable('ezrole'))
688 ->where(
689 $deleteRoleQuery->expr->lAnd(
690 $deleteRoleQuery->expr->eq(
691 $this->handler->quoteColumn('id'),
692 $deleteRoleQuery->bindValue($roleId, null, \PDO::PARAM_INT)
693 ),
694 $draftCondition
695 )
696 );
697
698 if ($status !== Role::STATUS_DRAFT) {
699 $deleteAssignmentsQuery->prepare()->execute();
700 }
701 $deleteRoleQuery->prepare()->execute();
702 }
703
711 public function publishRoleDraft($roleDraftId, $originalRoleId = null)
712 {
713 $query = $this->handler->createUpdateQuery();
714 $query
715 ->update($this->handler->quoteTable('ezrole'))
716 ->set(
717 $this->handler->quoteColumn('version'),
718 $query->bindValue(Role::STATUS_DEFINED, null, \PDO::PARAM_INT)
719 );
720 // Draft was created from an existing role, so published role must get the original ID.
721 if ($originalRoleId !== null) {
722 $query->set(
723 $this->handler->quoteColumn('id'),
724 $query->bindValue($originalRoleId, null, \PDO::PARAM_INT)
725 );
726 }
727
728 $query->where(
729 $query->expr->eq(
730 $this->handler->quoteColumn('id'),
731 $query->bindValue($roleDraftId, null, \PDO::PARAM_INT)
732 )
733 );
734 $statement = $query->prepare();
735 $statement->execute();
736
737 $policyQuery = $this->handler->createUpdateQuery();
738 $policyQuery
739 ->update($this->handler->quoteTable('ezpolicy'))
740 ->set(
741 $this->handler->quoteColumn('original_id'),
742 $policyQuery->bindValue(0, null, \PDO::PARAM_INT)
743 );
744 // Draft was created from an existing role, so published policies must get the original role ID.
745 if ($originalRoleId !== null) {
746 $policyQuery->set(
747 $this->handler->quoteColumn('role_id'),
748 $policyQuery->bindValue($originalRoleId, null, \PDO::PARAM_INT)
749 );
750 }
751
752 $policyQuery->where(
753 $policyQuery->expr->eq(
754 $this->handler->quoteColumn('role_id'),
755 $policyQuery->bindValue($roleDraftId, null, \PDO::PARAM_INT)
756 )
757 );
758 $queryStatement = $policyQuery->prepare();
759 $queryStatement->execute();
760 }
761
770 public function addPolicy($roleId, Policy $policy)
771 {
772 $query = $this->handler->createInsertQuery();
773 $query
774 ->insertInto($this->handler->quoteTable('ezpolicy'))
775 ->set(
776 $this->handler->quoteColumn('id'),
777 $this->handler->getAutoIncrementValue('ezpolicy', 'id')
778 )->set(
779 $this->handler->quoteColumn('function_name'),
780 $query->bindValue($policy->function)
781 )->set(
782 $this->handler->quoteColumn('module_name'),
783 $query->bindValue($policy->module)
784 )->set(
785 $this->handler->quoteColumn('original_id'),
786 $query->bindValue($policy->originalId ?: 0, null, \PDO::PARAM_INT)
787 )->set(
788 $this->handler->quoteColumn('role_id'),
789 $query->bindValue($roleId, null, \PDO::PARAM_INT)
790 );
791 $query->prepare()->execute();
792
793 $policy->id = $this->handler->lastInsertId(
794 $this->handler->getSequenceName('ezpolicy', 'id')
795 );
796
797 $policy->roleId = $roleId;
798
799 // Handle the only valid non-array value "*" by not inserting
800 // anything. Still has not been documented by eZ Systems. So we
801 // assume this is the right way to handle it.
802 if (is_array($policy->limitations)) {
803 $this->addPolicyLimitations($policy->id, $policy->limitations);
804 }
805
806 return $policy;
807 }
808
815 public function addPolicyLimitations($policyId, array $limitations)
816 {
817 foreach ($limitations as $identifier => $values) {
818 $query = $this->handler->createInsertQuery();
819 $query
820 ->insertInto($this->handler->quoteTable('ezpolicy_limitation'))
821 ->set(
822 $this->handler->quoteColumn('id'),
823 $this->handler->getAutoIncrementValue('ezpolicy_limitation', 'id')
824 )->set(
825 $this->handler->quoteColumn('identifier'),
826 $query->bindValue($identifier)
827 )->set(
828 $this->handler->quoteColumn('policy_id'),
829 $query->bindValue($policyId, null, \PDO::PARAM_INT)
830 );
831 $query->prepare()->execute();
832
833 $limitationId = $this->handler->lastInsertId(
834 $this->handler->getSequenceName('ezpolicy_limitation', 'id')
835 );
836
837 foreach ($values as $value) {
838 $query = $this->handler->createInsertQuery();
839 $query
840 ->insertInto($this->handler->quoteTable('ezpolicy_limitation_value'))
841 ->set(
842 $this->handler->quoteColumn('id'),
843 $this->handler->getAutoIncrementValue('ezpolicy_limitation_value', 'id')
844 )->set(
845 $this->handler->quoteColumn('value'),
846 $query->bindValue($value)
847 )->set(
848 $this->handler->quoteColumn('limitation_id'),
849 $query->bindValue($limitationId, null, \PDO::PARAM_INT)
850 );
851 $query->prepare()->execute();
852 }
853 }
854 }
855
861 public function removePolicy($policyId)
862 {
863 $this->removePolicyLimitations($policyId);
864
865 $query = $this->handler->createDeleteQuery();
866 $query
867 ->deleteFrom($this->handler->quoteTable('ezpolicy'))
868 ->where(
869 $query->expr->eq(
870 $this->handler->quoteColumn('id'),
871 $query->bindValue($policyId, null, \PDO::PARAM_INT)
872 )
873 );
874 $query->prepare()->execute();
875 }
876
882 public function removePolicyLimitations($policyId)
883 {
884 $query = $this->handler->createSelectQuery();
885 $query->select(
886 $this->handler->aliasedColumn($query, 'id', 'ezpolicy_limitation'),
887 $this->handler->aliasedColumn($query, 'id', 'ezpolicy_limitation_value')
888 )->from(
889 $this->handler->quoteTable('ezpolicy')
890 )->leftJoin(
891 $this->handler->quoteTable('ezpolicy_limitation'),
892 $query->expr->eq(
893 $this->handler->quoteColumn('policy_id', 'ezpolicy_limitation'),
894 $this->handler->quoteColumn('id', 'ezpolicy')
895 )
896 )->leftJoin(
897 $this->handler->quoteTable('ezpolicy_limitation_value'),
898 $query->expr->eq(
899 $this->handler->quoteColumn('limitation_id', 'ezpolicy_limitation_value'),
900 $this->handler->quoteColumn('id', 'ezpolicy_limitation')
901 )
902 )->where(
903 $query->expr->eq(
904 $this->handler->quoteColumn('id', 'ezpolicy'),
905 $query->bindValue($policyId, null, \PDO::PARAM_INT)
906 )
907 );
908
909 $statement = $query->prepare();
910 $statement->execute();
911
912 $limitationIdsSet = [];
913 $limitationValuesSet = [];
914 while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
915 if ($row['ezpolicy_limitation_id'] !== null) {
916 $limitationIdsSet[$row['ezpolicy_limitation_id']] = true;
917 }
918
919 if ($row['ezpolicy_limitation_value_id'] !== null) {
920 $limitationValuesSet[$row['ezpolicy_limitation_value_id']] = true;
921 }
922 }
923
924 if (!empty($limitationIdsSet)) {
925 $query = $this->handler->createDeleteQuery();
926 $query
927 ->deleteFrom($this->handler->quoteTable('ezpolicy_limitation'))
928 ->where(
929 $query->expr->in($this->handler->quoteColumn('id'), array_keys($limitationIdsSet))
930 );
931 $query->prepare()->execute();
932 }
933
934 if (!empty($limitationValuesSet)) {
935 $query = $this->handler->createDeleteQuery();
936 $query
937 ->deleteFrom($this->handler->quoteTable('ezpolicy_limitation_value'))
938 ->where(
939 $query->expr->in($this->handler->quoteColumn('id'), array_keys($limitationValuesSet))
940 );
941 $query->prepare()->execute();
942 }
943 }
944 }
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase
User Role gateway implementation using the Doctrine database.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:19
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\publishRoleDraft
publishRoleDraft($roleDraftId, $originalRoleId=null)
Publish the specified role draft.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:711
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRole
loadRole($roleId, $status=Role::STATUS_DEFINED)
Loads a specified role by id.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:98
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRoleAssignmentsByRoleId
loadRoleAssignmentsByRoleId($roleId)
Loads role assignments for given role ID.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:512
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\removePolicy
removePolicy($policyId)
Removes a policy from a role.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:861
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\$handler
$handler
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:25
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRoleAssignmentsByGroupId
loadRoleAssignmentsByGroupId($groupId, $inherited=false)
Loads role assignments for specified content ID.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:468
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRoleDraftByRoleId
loadRoleDraftByRoleId($roleId)
Loads a role draft by the original role ID.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:235
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\addPolicy
addPolicy($roleId, Policy $policy)
Adds a policy to a role.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:770
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRoleByIdentifier
loadRoleByIdentifier($identifier, $status=Role::STATUS_DEFINED)
Loads a specified role by $identifier.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:167
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\createRole
createRole(Role $role)
Create new role.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:44
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\updateRole
updateRole(RoleUpdateStruct $role)
Update role (draft).
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:630
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadPoliciesByUserId
loadPoliciesByUserId($userId)
Returns the user policies associated with the user.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:543
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRoleAssignment
loadRoleAssignment($roleAssignmentId)
Loads role assignment for specified assignment ID.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:436
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRolesForContentObjects
loadRolesForContentObjects($contentIds, $status=Role::STATUS_DEFINED)
Loads all roles associated with the given content objects.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:358
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\deleteRole
deleteRole($roleId, $status=Role::STATUS_DEFINED)
Delete the specified role (draft).
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:661
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\__construct
__construct(DatabaseHandler $handler)
Construct from database handler.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:32
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\removePolicyLimitations
removePolicyLimitations($policyId)
Remove all limitations for a policy.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:882
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\loadRoles
loadRoles($status=Role::STATUS_DEFINED)
Loads all roles.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:290
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\fetchUserGroups
fetchUserGroups($userId)
Fetch all group IDs the user belongs to.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:562
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway\DoctrineDatabase\addPolicyLimitations
addPolicyLimitations($policyId, array $limitations)
Adds limitations to an existing policy.
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:815
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway
Base class for content type gateways.
Definition: Persistence/Legacy/User/Role/Gateway.php:17
eZ\Publish\SPI\Persistence\User\Policy
Definition: SPI/Persistence/User/Policy.php:12
eZ\Publish\SPI\Persistence\User\RoleUpdateStruct
Definition: SPI/Persistence/User/RoleUpdateStruct.php:12
eZ\Publish\SPI\Persistence\User\Role
Definition: SPI/Persistence/User/Role.php:12
eZ\Publish\SPI\Persistence\User\Role\STATUS_DRAFT
const STATUS_DRAFT
Definition: SPI/Persistence/User/Role.php:17
eZ\Publish\SPI\Persistence\User\Role\STATUS_DEFINED
const STATUS_DEFINED
Definition: SPI/Persistence/User/Role.php:14
eZ\Publish\SPI\Persistence\User\Role\$status
$status
Definition: SPI/Persistence/User/Role.php:53
eZ\Publish\SPI\Persistence\User\Role\$identifier
$identifier
Definition: SPI/Persistence/User/Role.php:44
eZ\Publish\Core\Persistence\Database\DatabaseHandler
Interface for relational database abstractions supported by eZ.
Definition: DatabaseHandler.php:18
eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway
Definition: Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.