[TASK] Decouple adminPanel from frontend
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Repositories / FrontendGroupsRepository.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Repositories;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Type\Bitmask\Permission;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Admin Panel Frontend Groups Repository
27 */
28 class FrontendGroupsRepository
29 {
30 /**
31 * returns an array of all available frontend user groups including hidden ones.
32 *
33 * @return array
34 */
35 public function getAvailableFrontendUserGroups(): array
36 {
37 $optionCount = $this->getUserGroupOptionCountByBackendUser($this->getBackendUser());
38
39 $frontendGroups = [];
40 if ($optionCount > 0) {
41 $frontendGroups = $this->getUserGroupsForPagesByBackendUser($this->getBackendUser());
42 }
43
44 return $frontendGroups;
45 }
46
47 /**
48 * fetches the amount of user groups
49 *
50 * @param FrontendBackendUserAuthentication $beUser
51 * @return int
52 */
53 protected function getUserGroupOptionCountByBackendUser(FrontendBackendUserAuthentication $beUser): int
54 {
55 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
56 ->getQueryBuilderForTable('fe_groups');
57
58 $queryBuilder->getRestrictions()
59 ->removeAll()
60 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
61
62 $optionCount = $queryBuilder->count('fe_groups.uid')
63 ->from('fe_groups', 'fe_groups')
64 ->innerJoin(
65 'fe_groups',
66 'pages',
67 'pages',
68 $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid'))
69 )
70 ->where(
71 $beUser->getPagePermsClause(Permission::PAGE_SHOW)
72 )
73 ->execute()
74 ->fetchColumn(0);
75
76 return (int)$optionCount;
77 }
78
79 /**
80 * fetches all frontend user groups, except deleted, for pages
81 *
82 * @param FrontendBackendUserAuthentication $beUser
83 * @return array
84 */
85 protected function getUserGroupsForPagesByBackendUser(FrontendBackendUserAuthentication $beUser): array
86 {
87 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
88 ->getQueryBuilderForTable('fe_groups');
89
90 $queryBuilder->getRestrictions()
91 ->removeAll()
92 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
93
94 return $queryBuilder->select('fe_groups.uid', 'fe_groups.title')
95 ->from('fe_groups')
96 ->innerJoin(
97 'fe_groups',
98 'pages',
99 'pages',
100 $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid'))
101 )
102 ->where(
103 $beUser->getPagePermsClause(Permission::PAGE_SHOW)
104 )
105 ->orderBy('fe_groups.title')
106 ->execute()
107 ->fetchAll();
108 }
109
110 /**
111 * Returns the current BE user.
112 *
113 * @return FrontendBackendUserAuthentication
114 */
115 protected function getBackendUser(): FrontendBackendUserAuthentication
116 {
117 return $GLOBALS['BE_USER'];
118 }
119 }