[CLEANUP] Use Permission constants consistently
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / Domain / Repository / LogEntryRepository.php
1 <?php
2 namespace TYPO3\CMS\Belog\Domain\Repository;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Belog\Domain\Model\LogEntry;
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Type\Bitmask\Permission;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Sys log entry repository
23 */
24 class LogEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
25 {
26 /**
27 * Backend users, with UID as key
28 *
29 * @var array
30 */
31 protected $beUserList = [];
32
33 /**
34 * Initialize some local variables to be used during creation of objects
35 */
36 public function initializeObject()
37 {
38 $this->beUserList = $this->getBackendUsers();
39 /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface */
40 $defaultQuerySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
41 $defaultQuerySettings->setRespectStoragePage(false);
42 $this->setDefaultQuerySettings($defaultQuerySettings);
43 }
44
45 /**
46 * Finds all log entries that match all given constraints.
47 *
48 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
49 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\TYPO3\CMS\Belog\Domain\Model\LogEntry>
50 */
51 public function findByConstraint(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
52 {
53 $query = $this->createQuery();
54 $queryConstraints = $this->createQueryConstraints($query, $constraint);
55 if (!empty($queryConstraints)) {
56 $query->matching($query->logicalAnd($queryConstraints));
57 }
58 $query->setOrderings(['uid' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING]);
59 $query->setLimit($constraint->getNumber());
60 return $query->execute();
61 }
62
63 /**
64 * Create an array of query constraints from constraint object
65 *
66 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
67 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
68 * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
69 */
70 protected function createQueryConstraints(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
71 {
72 $queryConstraints = [];
73 // User / group handling
74 $this->addUsersAndGroupsToQueryConstraints($constraint, $query, $queryConstraints);
75 // Workspace
76 if ($constraint->getWorkspaceUid() != \TYPO3\CMS\Belog\Domain\Model\Workspace::UID_ANY_WORKSPACE) {
77 $queryConstraints[] = $query->equals('workspace', $constraint->getWorkspaceUid());
78 }
79 // Action (type):
80 if ($constraint->getAction() > 0) {
81 $queryConstraints[] = $query->equals('type', $constraint->getAction());
82 } elseif ($constraint->getAction() == -1) {
83 $queryConstraints[] = $query->equals('type', 5);
84 }
85 // Start / endtime handling: The timestamp calculation was already done
86 // in the controller, since we need those calculated values in the view as well.
87 $queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp());
88 $queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp());
89 // Page and level constraint if in page context
90 $this->addPageTreeConstraintsToQuery($constraint, $query, $queryConstraints);
91 return $queryConstraints;
92 }
93
94 /**
95 * Adds constraints for the page(s) to the query; this could be one single page or a whole subtree beneath a given
96 * page.
97 *
98 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
99 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
100 * @param array &$queryConstraints the query constraints to add to, will be modified
101 */
102 protected function addPageTreeConstraintsToQuery(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints)
103 {
104 if (!$constraint->getIsInPageContext()) {
105 return;
106 }
107 $pageIds = [];
108 // Check if we should get a whole tree of pages and not only a single page
109 if ($constraint->getDepth() > 0) {
110 /** @var $pageTree \TYPO3\CMS\Backend\Tree\View\PageTreeView */
111 $pageTree = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\PageTreeView::class);
112 $pageTree->init('AND ' . $GLOBALS['BE_USER']->getPagePermsClause(Permission::PAGE_SHOW));
113 $pageTree->makeHTML = 0;
114 $pageTree->fieldArray = ['uid'];
115 $pageTree->getTree($constraint->getPageId(), $constraint->getDepth());
116 $pageIds = $pageTree->ids;
117 }
118 $pageIds[] = $constraint->getPageId();
119 $queryConstraints[] = $query->in('eventPid', $pageIds);
120 }
121
122 /**
123 * Adds users and groups to the query constraints.
124 *
125 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
126 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
127 * @param array &$queryConstraints the query constraints to add to, will be modified
128 */
129 protected function addUsersAndGroupsToQueryConstraints(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints)
130 {
131 $userOrGroup = $constraint->getUserOrGroup();
132 if ($userOrGroup === '') {
133 return;
134 }
135 // Constraint for a group
136 if (substr($userOrGroup, 0, 3) === 'gr-') {
137 $groupId = (int)substr($userOrGroup, 3);
138 $userIds = [];
139 foreach ($this->beUserList as $userId => $userData) {
140 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($userData['usergroup_cached_list'], $groupId)) {
141 $userIds[] = $userId;
142 }
143 }
144 if (!empty($userIds)) {
145 $queryConstraints[] = $query->in('userid', $userIds);
146 } else {
147 // If there are no group members -> use -1 as constraint to not find anything
148 $queryConstraints[] = $query->in('userid', [-1]);
149 }
150 } elseif (substr($userOrGroup, 0, 3) === 'us-') {
151 $queryConstraints[] = $query->equals('userid', (int)substr($userOrGroup, 3));
152 } elseif ($userOrGroup === '-1') {
153 $queryConstraints[] = $query->equals('userid', (int)$GLOBALS['BE_USER']->user['uid']);
154 }
155 }
156
157 /**
158 * Deletes all messages which have the same message details
159 *
160 * @param LogEntry $logEntry
161 * @return int
162 */
163 public function deleteByMessageDetails(LogEntry $logEntry): int
164 {
165 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
166 ->getQueryBuilderForTable('sys_log');
167 $constraints = [];
168 $constraints[] = $queryBuilder->expr()->eq('details', $queryBuilder->createNamedParameter($logEntry->getDetails()));
169 // If the detailsNo is 11 or 12 we got messages that are heavily using placeholders. In this case
170 // we need to compare both the message and the actual log data to not remove too many log entries.
171 if (GeneralUtility::inList('11,12', $logEntry->getDetailsNumber())) {
172 $constraints[] = $queryBuilder->expr()->eq('log_data', $queryBuilder->createNamedParameter($logEntry->getLogData()));
173 }
174 return $queryBuilder->delete('sys_log')
175 ->where(...$constraints)
176 ->execute();
177 }
178
179 /**
180 * Get a list of all backend users that are not deleted
181 *
182 * @return array
183 */
184 protected function getBackendUsers()
185 {
186 return \TYPO3\CMS\Backend\Utility\BackendUtility::getUserNames();
187 }
188 }