Complex classes like CoreQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CoreQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class CoreQueryBuilder extends NC22ExtendedQueryBuilder { |
||
56 | |||
57 | |||
58 | use TArrayTools; |
||
59 | |||
60 | |||
61 | const SINGLE = 'single'; |
||
62 | const CIRCLE = 'circle'; |
||
63 | const MEMBER = 'member'; |
||
64 | const OWNER = 'owner'; |
||
65 | const FEDERATED_EVENT = 'federatedEvent'; |
||
66 | const REMOTE = 'remote'; |
||
67 | const BASED_ON = 'basedOn'; |
||
68 | const INITIATOR = 'initiator'; |
||
69 | const MEMBERSHIPS = 'memberships'; |
||
70 | const UPSTREAM_MEMBERSHIPS = 'upstreamMemberships'; |
||
71 | const INHERITANCE_FROM = 'inheritanceFrom'; |
||
72 | const INHERITED_BY = 'inheritedBy'; |
||
73 | const MOUNT = 'mount'; |
||
74 | const MOUNTPOINT = 'mountpoint'; |
||
75 | const SHARE = 'share'; |
||
76 | const FILE_CACHE = 'fileCache'; |
||
77 | const STORAGES = 'storages'; |
||
78 | const OPTIONS = 'options'; |
||
79 | |||
80 | |||
81 | public static $SQL_PATH = [ |
||
82 | self::SINGLE => [ |
||
83 | self::MEMBER |
||
84 | ], |
||
85 | self::CIRCLE => [ |
||
86 | self::OPTIONS => [ |
||
87 | 'getPersonalCircle' => true |
||
88 | ], |
||
89 | self::MEMBER, |
||
90 | self::OWNER => [ |
||
91 | self::BASED_ON |
||
92 | ], |
||
93 | self::MEMBERSHIPS, |
||
94 | self::INITIATOR => [ |
||
95 | self::BASED_ON, |
||
96 | self::INHERITED_BY => [ |
||
97 | self::MEMBERSHIPS |
||
98 | ] |
||
99 | ], |
||
100 | self::REMOTE => [ |
||
101 | self::MEMBER, |
||
102 | self::CIRCLE => [ |
||
103 | self::OWNER |
||
104 | ] |
||
105 | ] |
||
106 | ], |
||
107 | self::MEMBER => [ |
||
108 | self::MEMBERSHIPS, |
||
109 | self::INHERITANCE_FROM, |
||
110 | self::CIRCLE => [ |
||
111 | self::OPTIONS => [ |
||
112 | 'getData' => true |
||
113 | ], |
||
114 | self::OWNER, |
||
115 | self::MEMBERSHIPS, |
||
116 | self::INITIATOR => [ |
||
117 | self::OPTIONS => [ |
||
118 | 'mustBeMember' => true, |
||
119 | 'canBeVisitor' => false |
||
120 | ], |
||
121 | self::BASED_ON, |
||
122 | self::INHERITED_BY => [ |
||
123 | self::MEMBERSHIPS |
||
124 | ] |
||
125 | ] |
||
126 | ], |
||
127 | self::BASED_ON => [ |
||
128 | self::OWNER, |
||
129 | self::MEMBERSHIPS, |
||
130 | self::INITIATOR => [ |
||
131 | self::BASED_ON, |
||
132 | self::INHERITED_BY => [ |
||
133 | self::MEMBERSHIPS |
||
134 | ] |
||
135 | ] |
||
136 | ], |
||
137 | self::REMOTE => [ |
||
138 | self::MEMBER, |
||
139 | self::CIRCLE => [ |
||
140 | self::OWNER |
||
141 | ] |
||
142 | ] |
||
143 | ], |
||
144 | self::SHARE => [ |
||
145 | self::SHARE, |
||
146 | self::FILE_CACHE => [ |
||
147 | self::STORAGES |
||
148 | ], |
||
149 | self::UPSTREAM_MEMBERSHIPS => [ |
||
150 | self::MEMBERSHIPS, |
||
151 | self::INHERITED_BY => [ |
||
152 | self::BASED_ON |
||
153 | ], |
||
154 | self::SHARE, |
||
155 | ], |
||
156 | self::MEMBERSHIPS, |
||
157 | self::INHERITANCE_FROM, |
||
158 | self::INHERITED_BY => [ |
||
159 | self::BASED_ON |
||
160 | ], |
||
161 | self::CIRCLE => [ |
||
162 | self::OWNER |
||
163 | ], |
||
164 | self::INITIATOR => [ |
||
165 | self::BASED_ON, |
||
166 | self::INHERITED_BY => [ |
||
167 | self::MEMBERSHIPS |
||
168 | ] |
||
169 | ] |
||
170 | ], |
||
171 | self::REMOTE => [ |
||
172 | self::MEMBER |
||
173 | ], |
||
174 | self::MOUNT => [ |
||
175 | self::MEMBER => [ |
||
176 | self::REMOTE |
||
177 | ], |
||
178 | self::INITIATOR => [ |
||
179 | self::INHERITED_BY => [ |
||
180 | self::MEMBERSHIPS |
||
181 | ] |
||
182 | ], |
||
183 | self::MOUNTPOINT, |
||
184 | self::MEMBERSHIPS |
||
185 | ] |
||
186 | ]; |
||
187 | |||
188 | |||
189 | /** @var ConfigService */ |
||
190 | private $configService; |
||
191 | |||
192 | |||
193 | /** @var array */ |
||
194 | private $options = []; |
||
195 | |||
196 | |||
197 | /** |
||
198 | * CoreRequestBuilder constructor. |
||
199 | */ |
||
200 | public function __construct() { |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @param IFederatedModel $federatedModel |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getInstance(IFederatedModel $federatedModel): string { |
||
217 | |||
218 | |||
219 | /** |
||
220 | * @param string $id |
||
221 | */ |
||
222 | public function limitToCircleId(string $id): void { |
||
225 | |||
226 | /** |
||
227 | * @param string $name |
||
228 | */ |
||
229 | public function limitToName(string $name): void { |
||
232 | |||
233 | /** |
||
234 | * @param int $config |
||
235 | */ |
||
236 | public function limitToConfig(int $config): void { |
||
239 | |||
240 | /** |
||
241 | * @param int $source |
||
242 | */ |
||
243 | public function limitToSource(int $source): void { |
||
246 | |||
247 | /** |
||
248 | * @param int $config |
||
249 | */ |
||
250 | public function limitToConfigFlag(int $config): void { |
||
253 | |||
254 | |||
255 | /** |
||
256 | * @param string $singleId |
||
257 | */ |
||
258 | public function limitToSingleId(string $singleId): void { |
||
261 | |||
262 | |||
263 | /** |
||
264 | * @param string $itemId |
||
265 | */ |
||
266 | public function limitToItemId(string $itemId): void { |
||
269 | |||
270 | |||
271 | /** |
||
272 | * @param string $host |
||
273 | */ |
||
274 | public function limitToInstance(string $host): void { |
||
277 | |||
278 | |||
279 | /** |
||
280 | * @param int $userType |
||
281 | */ |
||
282 | public function limitToUserType(int $userType): void { |
||
285 | |||
286 | |||
287 | /** |
||
288 | * @param int $shareType |
||
289 | */ |
||
290 | public function limitToShareType(int $shareType): void { |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @param string $shareWith |
||
297 | */ |
||
298 | public function limitToShareWith(string $shareWith): void { |
||
301 | |||
302 | |||
303 | /** |
||
304 | * @param int $nodeId |
||
305 | */ |
||
306 | public function limitToFileSource(int $nodeId): void { |
||
309 | |||
310 | /** |
||
311 | * @param array $files |
||
312 | */ |
||
313 | public function limitToFileSourceArray(array $files): void { |
||
316 | |||
317 | |||
318 | /** |
||
319 | * @param int $shareId |
||
320 | */ |
||
321 | public function limitToShareParent(int $shareId): void { |
||
324 | |||
325 | |||
326 | /** |
||
327 | * @param Circle $circle |
||
328 | */ |
||
329 | public function filterCircle(Circle $circle): void { |
||
338 | |||
339 | |||
340 | /** |
||
341 | * left join RemoteInstance based on a Member |
||
342 | */ |
||
343 | public function leftJoinRemoteInstance(string $alias): void { |
||
356 | |||
357 | |||
358 | /** |
||
359 | * @param string $alias |
||
360 | * @param RemoteInstance $remoteInstance |
||
361 | * @param bool $filterSensitiveData |
||
362 | * @param string $aliasCircle |
||
363 | * |
||
364 | * @throws RequestBuilderException |
||
365 | */ |
||
366 | public function limitToRemoteInstance( |
||
382 | |||
383 | |||
384 | /** |
||
385 | * Left join RemoteInstance based on an incoming request |
||
386 | * |
||
387 | * @param string $alias |
||
388 | * @param RemoteInstance $remoteInstance |
||
389 | * |
||
390 | * @throws RequestBuilderException |
||
391 | */ |
||
392 | public function leftJoinRemoteInstanceIncomingRequest( |
||
407 | |||
408 | |||
409 | /** |
||
410 | * left join members to check memberships of someone from instance |
||
411 | * |
||
412 | * @param string $alias |
||
413 | * @param RemoteInstance $remoteInstance |
||
414 | * @param string $aliasCircle |
||
415 | * |
||
416 | * @throws RequestBuilderException |
||
417 | */ |
||
418 | private function leftJoinMemberFromInstance( |
||
441 | |||
442 | |||
443 | /** |
||
444 | * left join circle is member of a circle from remote instance |
||
445 | * |
||
446 | * @param string $alias |
||
447 | * @param RemoteInstance $remoteInstance |
||
448 | * @param string $aliasCircle |
||
449 | * |
||
450 | * @throws RequestBuilderException |
||
451 | */ |
||
452 | private function leftJoinMemberFromRemoteCircle( |
||
488 | |||
489 | |||
490 | /** |
||
491 | * - global_scale: visibility on all Circles |
||
492 | * - trusted: visibility on all FEDERATED Circle if owner is local |
||
493 | * - external: visibility on all FEDERATED Circle if owner is local and: |
||
494 | * - with if Circle contains at least one member from the remote instance |
||
495 | * - one circle from the remote instance contains the local circle as member, and confirmed (using |
||
496 | * sync locally) |
||
497 | * - passive: like external, but the members list will only contains member from the local instance and |
||
498 | * from the remote instance. |
||
499 | * |
||
500 | * @param string $alias |
||
501 | * @param bool $sensitive |
||
502 | * @param string $aliasCircle |
||
503 | * |
||
504 | * @throws RequestBuilderException |
||
505 | */ |
||
506 | protected function limitRemoteVisibility(string $alias, bool $sensitive, string $aliasCircle) { |
||
555 | |||
556 | |||
557 | /** |
||
558 | * @param string $alias |
||
559 | * @param Member $member |
||
560 | * |
||
561 | * @throws RequestBuilderException |
||
562 | */ |
||
563 | public function limitToDirectMembership(string $alias, Member $member): void { |
||
582 | |||
583 | |||
584 | /** |
||
585 | * @param string $aliasMember |
||
586 | * @param Member $member |
||
587 | */ |
||
588 | public function filterDirectMembership(string $aliasMember, Member $member): void { |
||
624 | |||
625 | |||
626 | /** |
||
627 | * @param string $alias |
||
628 | * @param IFederatedUser|null $initiator |
||
629 | * @param string $field |
||
630 | * |
||
631 | * @throws RequestBuilderException |
||
632 | */ |
||
633 | public function leftJoinCircle( |
||
634 | string $alias, |
||
635 | ?IFederatedUser $initiator = null, |
||
636 | string $field = 'circle_id' |
||
637 | ): void { |
||
638 | if ($this->getType() !== QueryBuilder::SELECT) { |
||
639 | return; |
||
640 | } |
||
641 | |||
642 | $aliasCircle = $this->generateAlias($alias, self::CIRCLE, $options); |
||
643 | $getData = $this->getBool('getData', $options, false); |
||
644 | $expr = $this->expr(); |
||
645 | |||
646 | if ($getData) { |
||
647 | $this->generateCircleSelectAlias($aliasCircle); |
||
648 | } |
||
649 | |||
650 | $this->leftJoin( |
||
651 | $alias, CoreRequestBuilder::TABLE_CIRCLE, $aliasCircle, |
||
652 | $expr->eq($aliasCircle . '.unique_id', $alias . '.' . $field) |
||
653 | ); |
||
654 | |||
655 | if (!is_null($initiator)) { |
||
656 | $this->limitToInitiator($aliasCircle, $initiator); |
||
657 | } |
||
658 | |||
659 | $this->leftJoinOwner($aliasCircle); |
||
660 | } |
||
661 | |||
662 | |||
663 | /** |
||
664 | * @param string $aliasMember |
||
665 | * @param IFederatedUser|null $initiator |
||
666 | * |
||
667 | * @throws RequestBuilderException |
||
668 | */ |
||
669 | public function leftJoinBasedOn( |
||
670 | string $aliasMember, |
||
671 | ?IFederatedUser $initiator = null |
||
672 | ): void { |
||
673 | if ($this->getType() !== QueryBuilder::SELECT) { |
||
674 | return; |
||
675 | } |
||
676 | |||
677 | try { |
||
678 | $aliasBasedOn = $this->generateAlias($aliasMember, self::BASED_ON, $options); |
||
679 | } catch (RequestBuilderException $e) { |
||
680 | return; |
||
681 | } |
||
682 | |||
683 | $expr = $this->expr(); |
||
684 | $this->generateCircleSelectAlias($aliasBasedOn) |
||
685 | ->leftJoin( |
||
686 | $aliasMember, CoreRequestBuilder::TABLE_CIRCLE, $aliasBasedOn, |
||
687 | $expr->eq($aliasBasedOn . '.unique_id', $aliasMember . '.single_id') |
||
688 | ); |
||
689 | |||
690 | if (!is_null($initiator)) { |
||
691 | $this->leftJoinInitiator($aliasBasedOn, $initiator); |
||
692 | $this->leftJoinOwner($aliasBasedOn); |
||
693 | } |
||
694 | } |
||
695 | |||
696 | |||
697 | /** |
||
698 | * @param string $alias |
||
699 | * @param string $field |
||
700 | * |
||
701 | * @throws RequestBuilderException |
||
702 | */ |
||
703 | public function leftJoinOwner(string $alias, string $field = 'unique_id'): void { |
||
704 | if ($this->getType() !== QueryBuilder::SELECT) { |
||
705 | return; |
||
706 | } |
||
707 | |||
708 | try { |
||
709 | $aliasMember = $this->generateAlias($alias, self::OWNER, $options); |
||
710 | $getData = $this->getBool('getData', $options, false); |
||
711 | } catch (RequestBuilderException $e) { |
||
712 | return; |
||
713 | } |
||
714 | |||
715 | $expr = $this->expr(); |
||
716 | $this->generateMemberSelectAlias($aliasMember) |
||
717 | ->leftJoin( |
||
718 | $alias, CoreRequestBuilder::TABLE_MEMBER, $aliasMember, |
||
719 | $expr->andX( |
||
720 | $expr->eq($aliasMember . '.circle_id', $alias . '.' . $field), |
||
721 | $expr->eq( |
||
722 | $aliasMember . '.level', |
||
723 | $this->createNamedParameter(Member::LEVEL_OWNER, self::PARAM_INT) |
||
724 | ) |
||
725 | ) |
||
726 | ); |
||
727 | |||
728 | $this->leftJoinBasedOn($aliasMember); |
||
729 | } |
||
730 | |||
731 | |||
732 | /** |
||
733 | * @param string $alias |
||
734 | * @param string $fieldCircleId |
||
735 | * @param string $fieldSingleId |
||
736 | * |
||
737 | * @throws RequestBuilderException |
||
738 | */ |
||
739 | public function leftJoinMember( |
||
740 | string $alias, |
||
741 | string $fieldCircleId = 'circle_id', |
||
742 | string $fieldSingleId = 'single_id' |
||
743 | ): void { |
||
744 | if ($this->getType() !== QueryBuilder::SELECT) { |
||
745 | return; |
||
746 | } |
||
747 | |||
748 | try { |
||
749 | $aliasMember = $this->generateAlias($alias, self::MEMBER, $options); |
||
750 | $getData = $this->getBool('getData', $options, false); |
||
751 | } catch (RequestBuilderException $e) { |
||
752 | return; |
||
753 | } |
||
754 | |||
755 | $expr = $this->expr(); |
||
756 | $this->generateMemberSelectAlias($aliasMember) |
||
757 | ->leftJoin( |
||
758 | $alias, CoreRequestBuilder::TABLE_MEMBER, $aliasMember, |
||
759 | $expr->andX( |
||
760 | $expr->eq($aliasMember . '.circle_id', $alias . '.' . $fieldCircleId), |
||
761 | $expr->eq($aliasMember . '.single_id', $alias . '.' . $fieldSingleId), |
||
762 | $expr->gte( |
||
763 | $aliasMember . '.level', |
||
764 | $this->createNamedParameter(Member::LEVEL_MEMBER, self::PARAM_INT) |
||
765 | ) |
||
766 | ) |
||
767 | ); |
||
768 | |||
769 | $this->leftJoinRemoteInstance($aliasMember); |
||
770 | $this->leftJoinBasedOn($aliasMember); |
||
771 | } |
||
772 | |||
773 | |||
774 | /** |
||
775 | * if 'getData' is true, will returns 'inheritanceBy': the Member at the end of a sub-chain of |
||
776 | * memberships (based on $field for Top Circle's singleId) |
||
777 | * |
||
778 | * @param string $alias |
||
779 | * @param string $field |
||
780 | * @param string $aliasInheritedBy |
||
781 | * |
||
782 | * @throws RequestBuilderException |
||
783 | */ |
||
784 | public function leftJoinInheritedMembers(string $alias, string $field = '', string $aliasInheritedBy = '' |
||
785 | ): void { |
||
786 | $expr = $this->expr(); |
||
787 | |||
788 | $field = ($field === '') ? 'circle_id' : $field; |
||
789 | $aliasMembership = $this->generateAlias($alias, self::MEMBERSHIPS, $options); |
||
790 | |||
791 | $this->leftJoin( |
||
792 | $alias, CoreRequestBuilder::TABLE_MEMBERSHIP, $aliasMembership, |
||
793 | $expr->eq($aliasMembership . '.circle_id', $alias . '.' . $field) |
||
794 | ); |
||
795 | |||
796 | // if (!$this->getBool('getData', $options, false)) { |
||
797 | // return; |
||
798 | // } |
||
799 | |||
800 | if ($aliasInheritedBy === '') { |
||
801 | $aliasInheritedBy = $this->generateAlias($alias, self::INHERITED_BY); |
||
802 | } |
||
803 | $this->generateMemberSelectAlias($aliasInheritedBy) |
||
804 | ->leftJoin( |
||
805 | $alias, CoreRequestBuilder::TABLE_MEMBER, $aliasInheritedBy, |
||
806 | $expr->andX( |
||
807 | $expr->eq($aliasMembership . '.inheritance_last', $aliasInheritedBy . '.circle_id'), |
||
808 | $expr->eq($aliasMembership . '.single_id', $aliasInheritedBy . '.single_id') |
||
809 | ) |
||
810 | ); |
||
811 | |||
812 | $this->leftJoinBasedOn($aliasInheritedBy); |
||
813 | } |
||
814 | |||
815 | |||
816 | /** |
||
817 | * @throws RequestBuilderException |
||
818 | */ |
||
819 | public function limitToInheritedMemberships(string $alias, string $singleId, string $field = ''): void { |
||
820 | $expr = $this->expr(); |
||
821 | $field = ($field === '') ? 'circle_id' : $field; |
||
822 | $aliasUpstreamMembership = $this->generateAlias($alias, self::UPSTREAM_MEMBERSHIPS, $options); |
||
823 | $this->leftJoin( |
||
824 | $alias, CoreRequestBuilder::TABLE_MEMBERSHIP, $aliasUpstreamMembership, |
||
825 | $expr->eq($aliasUpstreamMembership . '.single_id', $this->createNamedParameter($singleId)) |
||
826 | ); |
||
827 | |||
828 | $orX = $expr->orX( |
||
829 | $expr->eq($aliasUpstreamMembership . '.circle_id', $alias . '.' . $field), |
||
830 | $expr->eq($alias . '.' . $field, $this->createNamedParameter($singleId)) |
||
831 | ); |
||
832 | |||
833 | $this->andWhere($orX); |
||
834 | } |
||
835 | |||
836 | |||
837 | /** |
||
838 | * limit the request to Members and Sub Members of a Circle. |
||
839 | * |
||
840 | * @param string $alias |
||
841 | * @param string $singleId |
||
842 | * |
||
843 | * @throws RequestBuilderException |
||
844 | */ |
||
845 | public function limitToMembersByInheritance(string $alias, string $singleId): void { |
||
846 | $this->leftJoinMembersByInheritance($alias); |
||
847 | |||
848 | $expr = $this->expr(); |
||
849 | $aliasMembership = $this->generateAlias($alias, self::MEMBERSHIPS); |
||
850 | $this->andWhere($expr->eq($aliasMembership . '.circle_id', $this->createNamedParameter($singleId))); |
||
851 | } |
||
852 | |||
853 | |||
854 | /** |
||
855 | * if 'getData' is true, will returns 'inheritanceFrom': the Circle-As-Member of the Top Circle |
||
856 | * that explain the membership of a Member (based on $field for singleId) to a specific Circle |
||
857 | * |
||
858 | * // TODO: returns the link/path ? |
||
859 | * |
||
860 | * @param string $alias |
||
861 | * @param string $field |
||
862 | * |
||
863 | * @throws RequestBuilderException |
||
864 | */ |
||
865 | public function leftJoinMembersByInheritance(string $alias, string $field = ''): void { |
||
866 | $expr = $this->expr(); |
||
867 | |||
868 | $field = ($field === '') ? 'circle_id' : $field; |
||
869 | $aliasMembership = $this->generateAlias($alias, self::MEMBERSHIPS, $options); |
||
870 | |||
871 | $this->leftJoin( |
||
872 | $alias, CoreRequestBuilder::TABLE_MEMBERSHIP, $aliasMembership, |
||
873 | // $expr->andX( |
||
874 | $expr->eq($aliasMembership . '.inheritance_last', $alias . '.' . $field) |
||
875 | // $expr->eq($aliasMembership . '.single_id', $alias . '.single_id') |
||
876 | // ) |
||
877 | ); |
||
878 | |||
879 | if (!$this->getBool('getData', $options, false)) { |
||
880 | return; |
||
881 | } |
||
882 | |||
883 | $aliasInheritanceFrom = $this->generateAlias($alias, self::INHERITANCE_FROM); |
||
884 | $this->generateMemberSelectAlias($aliasInheritanceFrom) |
||
885 | ->leftJoin( |
||
886 | $aliasMembership, CoreRequestBuilder::TABLE_MEMBER, $aliasInheritanceFrom, |
||
887 | $expr->andX( |
||
888 | $expr->eq($aliasMembership . '.circle_id', $aliasInheritanceFrom . '.circle_id'), |
||
889 | $expr->eq($aliasMembership . '.inheritance_first', $aliasInheritanceFrom . '.single_id') |
||
890 | ) |
||
891 | ); |
||
892 | } |
||
893 | |||
894 | |||
895 | /** |
||
896 | * limit the result to the point of view of a FederatedUser |
||
897 | * |
||
898 | * @param string $alias |
||
899 | * @param IFederatedUser $user |
||
900 | * @param string $field |
||
901 | * |
||
902 | * @throws RequestBuilderException |
||
903 | */ |
||
904 | public function limitToInitiator(string $alias, IFederatedUser $user, string $field = ''): void { |
||
905 | $this->leftJoinInitiator($alias, $user, $field); |
||
906 | $this->limitInitiatorVisibility($alias); |
||
907 | |||
908 | $aliasInitiator = $this->generateAlias($alias, self::INITIATOR, $options); |
||
909 | if ($this->getBool('getData', $options, false)) { |
||
910 | $this->leftJoinBasedOn($aliasInitiator); |
||
911 | } |
||
912 | } |
||
913 | |||
914 | |||
915 | /** |
||
916 | * Left join members to filter userId as initiator. |
||
917 | * |
||
918 | * @param string $alias |
||
919 | * @param IFederatedUser $initiator |
||
920 | * @param string $field |
||
921 | * |
||
922 | * @throws RequestBuilderException |
||
923 | */ |
||
924 | public function leftJoinInitiator(string $alias, IFederatedUser $initiator, string $field = ''): void { |
||
925 | if ($this->getType() !== QueryBuilder::SELECT) { |
||
926 | return; |
||
927 | } |
||
928 | |||
929 | $expr = $this->expr(); |
||
930 | $field = ($field === '') ? 'unique_id' : $field; |
||
931 | $aliasMembership = $this->generateAlias($alias, self::MEMBERSHIPS, $options); |
||
932 | |||
933 | $this->leftJoin( |
||
934 | $alias, CoreRequestBuilder::TABLE_MEMBERSHIP, $aliasMembership, |
||
935 | $expr->andX( |
||
936 | $expr->eq( |
||
937 | $aliasMembership . '.single_id', |
||
938 | $this->createNamedParameter($initiator->getSingleId()) |
||
939 | ), |
||
940 | $expr->eq($aliasMembership . '.circle_id', $alias . '.' . $field) |
||
941 | ) |
||
942 | ); |
||
943 | |||
944 | if (!$this->getBool('getData', $options, false)) { |
||
945 | return; |
||
946 | } |
||
947 | |||
948 | try { |
||
949 | $aliasInitiator = $this->generateAlias($alias, self::INITIATOR, $options); |
||
950 | $this->leftJoin( |
||
951 | $aliasMembership, CoreRequestBuilder::TABLE_MEMBER, $aliasInitiator, |
||
952 | $expr->andX( |
||
953 | $expr->eq($aliasMembership . '.inheritance_first', $aliasInitiator . '.single_id'), |
||
954 | $expr->eq($aliasMembership . '.circle_id', $aliasInitiator . '.circle_id') |
||
955 | ) |
||
956 | ); |
||
957 | |||
958 | $aliasInheritedBy = $this->generateAlias($aliasInitiator, self::INHERITED_BY); |
||
959 | $this->leftJoin( |
||
960 | $aliasInitiator, CoreRequestBuilder::TABLE_MEMBER, $aliasInheritedBy, |
||
961 | $expr->andX( |
||
962 | $expr->eq($aliasMembership . '.single_id', $aliasInheritedBy . '.single_id'), |
||
963 | $expr->eq($aliasMembership . '.inheritance_last', $aliasInheritedBy . '.circle_id') |
||
964 | ) |
||
965 | ); |
||
966 | |||
967 | $default = []; |
||
968 | if ($this->getBool('canBeVisitor', $options, false)) { |
||
969 | $default = [ |
||
970 | 'user_id' => $initiator->getUserId(), |
||
971 | 'single_id' => $initiator->getSingleId(), |
||
972 | 'user_type' => $initiator->getUserType(), |
||
973 | 'instance' => $initiator->getInstance() |
||
974 | ]; |
||
975 | } |
||
976 | $this->generateMemberSelectAlias($aliasInitiator, $default); |
||
977 | |||
978 | $this->generateMemberSelectAlias($aliasInheritedBy); |
||
979 | $aliasInheritedByMembership = $this->generateAlias($aliasInheritedBy, self::MEMBERSHIPS); |
||
980 | $this->generateMembershipSelectAlias($aliasMembership, $aliasInheritedByMembership); |
||
981 | } catch (RequestBuilderException $e) { |
||
982 | \OC::$server->getLogger()->log(3, '-- ' . $e->getMessage()); |
||
983 | } |
||
984 | } |
||
985 | |||
986 | |||
987 | /** |
||
988 | * @param string $alias |
||
989 | * |
||
990 | * @throws RequestBuilderException |
||
991 | */ |
||
992 | protected function limitInitiatorVisibility(string $alias) { |
||
993 | $aliasMembership = $this->generateAlias($alias, self::MEMBERSHIPS, $options); |
||
994 | $getPersonalCircle = $this->getBool('getPersonalCircle', $options, false); |
||
995 | |||
996 | $expr = $this->expr(); |
||
997 | |||
998 | // Visibility to non-member is |
||
999 | // - 0 (default), if initiator is member |
||
1000 | // - 2 (Personal), if initiator is owner) |
||
1001 | // - 4 (Visible to everyone) |
||
1002 | $orX = $expr->orX(); |
||
1003 | $orX->add( |
||
1004 | $expr->andX( |
||
1005 | $expr->gte($aliasMembership . '.level', $this->createNamedParameter(Member::LEVEL_MEMBER)) |
||
1006 | ) |
||
1007 | ); |
||
1008 | |||
1009 | if ($getPersonalCircle) { |
||
1010 | $orX->add( |
||
1011 | $expr->andX( |
||
1012 | $expr->bitwiseAnd($alias . '.config', Circle::CFG_PERSONAL), |
||
1013 | $expr->eq($aliasMembership . '.level', $this->createNamedParameter(Member::LEVEL_OWNER)) |
||
1014 | ) |
||
1015 | ); |
||
1016 | } |
||
1017 | if (!$this->getBool('mustBeMember', $options, true)) { |
||
1018 | $orX->add($expr->bitwiseAnd($alias . '.config', Circle::CFG_VISIBLE)); |
||
1019 | } |
||
1020 | if ($this->getBool('canBeVisitor', $options, false)) { |
||
1021 | // TODO: should find a better way, also filter on remote initiator on non-federated ? |
||
1022 | $orX->add($expr->gte($alias . '.config', $this->createNamedParameter(0))); |
||
1023 | } |
||
1024 | if ($this->getBool('canBeVisitorOnOpen', $options, false)) { |
||
1025 | echo '!!!!'; |
||
1026 | $andOpen = $expr->andX(); |
||
1027 | $andOpen->add($expr->bitwiseAnd($alias . '.config', Circle::CFG_OPEN)); |
||
1028 | $andOpen->add( |
||
1029 | $this->createFunction('NOT') . $expr->bitwiseAnd($alias . '.config', Circle::CFG_REQUEST) |
||
1030 | ); |
||
1031 | $orX->add($andOpen); |
||
1032 | } |
||
1033 | $this->andWhere($orX); |
||
1034 | |||
1035 | |||
1036 | // $orTypes = $this->generateLimit($qb, $circleUniqueId, $userId, $type, $name, $forceAll); |
||
1037 | // if (sizeof($orTypes) === 0) { |
||
1038 | // throw new ConfigNoCircleAvailableException( |
||
1039 | // $this->l10n->t( |
||
1040 | // 'You cannot use the Circles Application until your administrator has allowed at least one type of circles' |
||
1041 | // ) |
||
1042 | // ); |
||
1043 | // } |
||
1044 | |||
1045 | // $orXTypes = $this->expr() |
||
1046 | // ->orX(); |
||
1047 | // foreach ($orTypes as $orType) { |
||
1048 | // $orXTypes->add($orType); |
||
1049 | // } |
||
1050 | // |
||
1051 | // $qb->andWhere($orXTypes); |
||
1052 | } |
||
1053 | |||
1054 | |||
1055 | /** |
||
1056 | * CFG_SINGLE, CFG_HIDDEN and CFG_BACKEND means hidden from listing. |
||
1057 | * |
||
1058 | * @param string $aliasCircle |
||
1059 | * @param int $flag |
||
1060 | */ |
||
1061 | public function filterCircles( |
||
1079 | |||
1080 | |||
1081 | /** |
||
1082 | * Limit visibility on Sensitive information when search for members. |
||
1083 | * |
||
1084 | * @param string $alias |
||
1085 | * |
||
1086 | * @return ICompositeExpression |
||
1087 | */ |
||
1088 | private function limitRemoteVisibility_Sensitive_Members(string $alias): ICompositeExpression { |
||
1114 | |||
1115 | |||
1116 | /** |
||
1117 | * |
||
1118 | * @param string $aliasCircle |
||
1119 | * @param int $flag |
||
1120 | */ |
||
1121 | public function filterConfig(string $aliasCircle, int $flag): void { |
||
1124 | |||
1125 | |||
1126 | /** |
||
1127 | * Link to storage/filecache |
||
1128 | * |
||
1129 | * @param string $aliasShare |
||
1130 | * |
||
1131 | * @throws RequestBuilderException |
||
1132 | */ |
||
1133 | public function leftJoinFileCache(string $aliasShare) { |
||
1160 | |||
1161 | |||
1162 | /** |
||
1163 | * @param string $aliasShare |
||
1164 | * @param string $aliasShareMemberships |
||
1165 | * |
||
1166 | * @throws RequestBuilderException |
||
1167 | */ |
||
1168 | public function leftJoinShareChild(string $aliasShare, string $aliasShareMemberships = '') { |
||
1193 | |||
1194 | |||
1195 | /** |
||
1196 | * @param string $alias |
||
1197 | * @param FederatedUser $federatedUser |
||
1198 | * @param bool $reshares |
||
1199 | */ |
||
1200 | public function limitToShareOwner(string $alias, FederatedUser $federatedUser, bool $reshares): void { |
||
1201 | $expr = $this->expr(); |
||
1202 | |||
1203 | $orX = $expr->orX($this->exprLimit('uid_initiator', $federatedUser->getUserId(), $alias)); |
||
1204 | |||
1211 | |||
1212 | |||
1213 | /** |
||
1214 | * @param string $aliasMount |
||
1215 | * @param string $aliasMountMemberships |
||
1216 | * |
||
1217 | * @throws RequestBuilderException |
||
1218 | */ |
||
1219 | public function leftJoinMountpoint(string $aliasMount, string $aliasMountMemberships = '') { |
||
1238 | |||
1239 | |||
1240 | /** |
||
1241 | * @param string $alias |
||
1242 | * @param array $default |
||
1243 | * |
||
1244 | * @return CoreQueryBuilder |
||
1245 | */ |
||
1246 | private function generateCircleSelectAlias(string $alias, array $default = []): self { |
||
1256 | |||
1257 | /** |
||
1258 | * @param string $alias |
||
1259 | * @param array $default |
||
1260 | * |
||
1261 | * @return $this |
||
1262 | */ |
||
1263 | private function generateMemberSelectAlias(string $alias, array $default = []): self { |
||
1273 | |||
1274 | |||
1275 | /** |
||
1276 | * @param string $alias |
||
1277 | * @param array $default |
||
1278 | * @param string $prefix |
||
1279 | * |
||
1280 | * @return $this |
||
1281 | */ |
||
1282 | private function generateMembershipSelectAlias( |
||
1296 | |||
1297 | |||
1298 | /** |
||
1299 | * @param string $alias |
||
1300 | * @param array $default |
||
1301 | * |
||
1302 | * @return $this |
||
1303 | */ |
||
1304 | private function generateRemoteInstanceSelectAlias(string $alias, array $default = []): self { |
||
1314 | |||
1315 | |||
1316 | /** |
||
1317 | * @param array $path |
||
1318 | * @param array $options |
||
1319 | */ |
||
1320 | public function setOptions(array $path, array $options): void { |
||
1328 | |||
1329 | |||
1330 | /** |
||
1331 | * @param string $base |
||
1332 | * @param string $extension |
||
1333 | * @param array|null $options |
||
1334 | * |
||
1335 | * @return string |
||
1336 | * @throws RequestBuilderException |
||
1337 | */ |
||
1338 | public function generateAlias(string $base, string $extension, ?array &$options = []): string { |
||
1362 | |||
1363 | |||
1364 | /** |
||
1365 | * @param string $prefix |
||
1366 | * |
||
1367 | * @return array |
||
1368 | */ |
||
1369 | public function getAvailablePath(string $prefix): array { |
||
1385 | |||
1386 | } |
||
1387 | |||
1388 |