LogEntryRepository.php 6.08 KB
Newer Older
1
<?php
2
3
namespace TYPO3\CMS\Belog\Domain\Repository;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
Wouter Wolters's avatar
Wouter Wolters committed
16

17
18
19
/**
 * Sys log entry repository
 */
20
21
class LogEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

22
	/**
Wouter Wolters's avatar
Wouter Wolters committed
23
	 * Backend users, with UID as key
24
25
26
27
28
29
30
31
32
33
34
	 *
	 * @var array
	 */
	protected $beUserList = array();

	/**
	 * Initialize some local variables to be used during creation of objects
	 *
	 * @return void
	 */
	public function initializeObject() {
35
36
		$this->beUserList = \TYPO3\CMS\Backend\Utility\BackendUtility::getUserNames();
		/** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface */
37
		$defaultQuerySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
38
39
40
41
42
43
44
		$defaultQuerySettings->setRespectStoragePage(FALSE);
		$this->setDefaultQuerySettings($defaultQuerySettings);
	}

	/**
	 * Finds all log entries that match all given constraints.
	 *
45
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
46
	 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\TYPO3\CMS\Belog\Domain\Model\LogEntry>
47
	 */
48
	public function findByConstraint(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
49
50
51
		$query = $this->createQuery();
		$queryConstraints = $this->createQueryConstraints($query, $constraint);
		if (!empty($queryConstraints)) {
52
			$query->matching($query->logicalAnd($queryConstraints));
53
		}
54
		$query->setOrderings(array('uid' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
55
56
57
58
59
60
61
		$query->setLimit($constraint->getNumber());
		return $query->execute();
	}

	/**
	 * Create an array of query constraints from constraint object
	 *
62
63
	 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
64
	 * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
65
	 */
66
	protected function createQueryConstraints(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
67
		$queryConstraints = array();
68
		// User / group handling
69
		$this->addUsersAndGroupsToQueryConstraints($constraint, $query, $queryConstraints);
70
71
		// Workspace
		if ($constraint->getWorkspaceUid() != \TYPO3\CMS\Belog\Domain\Model\Workspace::UID_ANY_WORKSPACE) {
72
73
			$queryConstraints[] = $query->equals('workspace', $constraint->getWorkspaceUid());
		}
74
		// Action (type):
75
76
77
		if ($constraint->getAction() > 0) {
			$queryConstraints[] = $query->equals('type', $constraint->getAction());
		} elseif ($constraint->getAction() == -1) {
78
			$queryConstraints[] = $query->in('error', array(-1,1,2,3));
79
		}
80
81
		// Start / endtime handling: The timestamp calculation was already done
		// in the controller, since we need those calculated values in the view as well.
82
83
		$queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp());
		$queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp());
84
		// Page and level constraint if in page context
85
86
87
88
89
90
91
92
		$this->addPageTreeConstraintsToQuery($constraint, $query, $queryConstraints);
		return $queryConstraints;
	}

	/**
	 * Adds constraints for the page(s) to the query; this could be one single page or a whole subtree beneath a given
	 * page.
	 *
93
94
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
	 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
95
96
97
	 * @param array &$queryConstraints the query constraints to add to, will be modified
	 * @return void
	 */
98
	protected function addPageTreeConstraintsToQuery(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints) {
99
100
101
102
		if (!$constraint->getIsInPageContext()) {
			return;
		}
		$pageIds = array();
Wouter Wolters's avatar
Wouter Wolters committed
103
		// Check if we should get a whole tree of pages and not only a single page
104
		if ($constraint->getDepth() > 0) {
105
			/** @var $pageTree \TYPO3\CMS\Backend\Tree\View\PageTreeView */
106
			$pageTree = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\PageTreeView::class);
107
108
109
110
111
112
113
114
115
116
117
118
119
			$pageTree->init('AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1));
			$pageTree->makeHTML = 0;
			$pageTree->fieldArray = array('uid');
			$pageTree->getTree($constraint->getPageId(), $constraint->getDepth());
			$pageIds = $pageTree->ids;
		}
		$pageIds[] = $constraint->getPageId();
		$queryConstraints[] = $query->in('eventPid', $pageIds);
	}

	/**
	 * Adds users and groups to the query constraints.
	 *
120
121
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
	 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
122
123
124
	 * @param array &$queryConstraints the query constraints to add to, will be modified
	 * @return void
	 */
125
	protected function addUsersAndGroupsToQueryConstraints(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints) {
126
127
128
129
		$userOrGroup = $constraint->getUserOrGroup();
		if ($userOrGroup === '') {
			return;
		}
130
		// Constraint for a group
131
		if (substr($userOrGroup, 0, 3) === 'gr-') {
132
			$groupId = (int)substr($userOrGroup, 3);
133
134
			$userIds = array();
			foreach ($this->beUserList as $userId => $userData) {
135
				if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($userData['usergroup_cached_list'], $groupId)) {
136
137
138
139
140
141
					$userIds[] = $userId;
				}
			}
			if (!empty($userIds)) {
				$queryConstraints[] = $query->in('userid', $userIds);
			} else {
142
				// If there are no group members -> use -1 as constraint to not find anything
143
144
145
				$queryConstraints[] = $query->in('userid', array(-1));
			}
		} elseif (substr($userOrGroup, 0, 3) === 'us-') {
146
			$queryConstraints[] = $query->equals('userid', (int)substr($userOrGroup, 3));
147
		} elseif ($userOrGroup === '-1') {
148
			$queryConstraints[] = $query->equals('userid', (int)$GLOBALS['BE_USER']->user['uid']);
149
150
		}
	}
151

152
}