[TASK] Stabilize / Solidify public API of adminPanel
[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 * @internal
29 */
30 class FrontendGroupsRepository
31 {
32 /**
33 * returns an array of all available frontend user groups including hidden ones.
34 *
35 * @return array
36 */
37 public function getAvailableFrontendUserGroups(): array
38 {
39 $optionCount = $this->getUserGroupOptionCountByBackendUser($this->getBackendUser());
40
41 $frontendGroups = [];
42 if ($optionCount > 0) {
43 $frontendGroups = $this->getUserGroupsForPagesByBackendUser($this->getBackendUser());
44 }
45
46 return $frontendGroups;
47 }
48
49 /**
50 * fetches the amount of user groups
51 *
52 * @param FrontendBackendUserAuthentication $beUser
53 * @return int
54 */
55 protected function getUserGroupOptionCountByBackendUser(FrontendBackendUserAuthentication $beUser): int
56 {
57 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
58 ->getQueryBuilderForTable('fe_groups');
59
60 $queryBuilder->getRestrictions()
61 ->removeAll()
62 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
63
64 $optionCount = $queryBuilder->count('fe_groups.uid')
65 ->from('fe_groups', 'fe_groups')
66 ->innerJoin(
67 'fe_groups',
68 'pages',
69 'pages',
70 $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid'))
71 )
72 ->where(
73 $beUser->getPagePermsClause(Permission::PAGE_SHOW)
74 )
75 ->execute()
76 ->fetchColumn(0);
77
78 return (int)$optionCount;
79 }
80
81 /**
82 * fetches all frontend user groups, except deleted, for pages
83 *
84 * @param FrontendBackendUserAuthentication $beUser
85 * @return array
86 */
87 protected function getUserGroupsForPagesByBackendUser(FrontendBackendUserAuthentication $beUser): array
88 {
89 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
90 ->getQueryBuilderForTable('fe_groups');
91
92 $queryBuilder->getRestrictions()
93 ->removeAll()
94 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
95
96 return $queryBuilder->select('fe_groups.uid', 'fe_groups.title')
97 ->from('fe_groups')
98 ->innerJoin(
99 'fe_groups',
100 'pages',
101 'pages',
102 $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid'))
103 )
104 ->where(
105 $beUser->getPagePermsClause(Permission::PAGE_SHOW)
106 )
107 ->orderBy('fe_groups.title')
108 ->execute()
109 ->fetchAll();
110 }
111
112 /**
113 * Returns the current BE user.
114 *
115 * @return FrontendBackendUserAuthentication
116 */
117 protected function getBackendUser(): FrontendBackendUserAuthentication
118 {
119 return $GLOBALS['BE_USER'];
120 }
121 }