LogEntryRepository.php 6.13 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
20
21
/**
 * Sys log entry repository
 *
 * @author Christian Kuhn <lolli@schwarzbu.ch>
 */
22
23
class LogEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

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

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

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

	/**
	 * Create an array of query constraints from constraint object
	 *
64
65
	 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
66
	 * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
67
	 */
68
	protected function createQueryConstraints(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
69
		$queryConstraints = array();
70
		// User / group handling
71
		$this->addUsersAndGroupsToQueryConstraints($constraint, $query, $queryConstraints);
72
73
		// Workspace
		if ($constraint->getWorkspaceUid() != \TYPO3\CMS\Belog\Domain\Model\Workspace::UID_ANY_WORKSPACE) {
74
75
			$queryConstraints[] = $query->equals('workspace', $constraint->getWorkspaceUid());
		}
76
		// Action (type):
77
78
79
		if ($constraint->getAction() > 0) {
			$queryConstraints[] = $query->equals('type', $constraint->getAction());
		} elseif ($constraint->getAction() == -1) {
80
			$queryConstraints[] = $query->in('error', array(-1,1,2,3));
81
		}
82
83
		// Start / endtime handling: The timestamp calculation was already done
		// in the controller, since we need those calculated values in the view as well.
84
85
		$queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp());
		$queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp());
86
		// Page and level constraint if in page context
87
88
89
90
91
92
93
94
		$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.
	 *
95
96
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
	 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
97
98
99
	 * @param array &$queryConstraints the query constraints to add to, will be modified
	 * @return void
	 */
100
	protected function addPageTreeConstraintsToQuery(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints) {
101
102
103
104
		if (!$constraint->getIsInPageContext()) {
			return;
		}
		$pageIds = array();
Wouter Wolters's avatar
Wouter Wolters committed
105
		// Check if we should get a whole tree of pages and not only a single page
106
		if ($constraint->getDepth() > 0) {
107
			/** @var $pageTree \TYPO3\CMS\Backend\Tree\View\PageTreeView */
108
			$pageTree = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\PageTreeView::class);
109
110
111
112
113
114
115
116
117
118
119
120
121
			$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.
	 *
122
123
	 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
	 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
124
125
126
	 * @param array &$queryConstraints the query constraints to add to, will be modified
	 * @return void
	 */
127
	protected function addUsersAndGroupsToQueryConstraints(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints) {
128
129
130
131
		$userOrGroup = $constraint->getUserOrGroup();
		if ($userOrGroup === '') {
			return;
		}
132
		// Constraint for a group
133
		if (substr($userOrGroup, 0, 3) === 'gr-') {
134
			$groupId = (int)substr($userOrGroup, 3);
135
136
			$userIds = array();
			foreach ($this->beUserList as $userId => $userData) {
137
				if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($userData['usergroup_cached_list'], $groupId)) {
138
139
140
141
142
143
					$userIds[] = $userId;
				}
			}
			if (!empty($userIds)) {
				$queryConstraints[] = $query->in('userid', $userIds);
			} else {
144
				// If there are no group members -> use -1 as constraint to not find anything
145
146
147
				$queryConstraints[] = $query->in('userid', array(-1));
			}
		} elseif (substr($userOrGroup, 0, 3) === 'us-') {
148
			$queryConstraints[] = $query->equals('userid', (int)substr($userOrGroup, 3));
149
		} elseif ($userOrGroup === '-1') {
150
			$queryConstraints[] = $query->equals('userid', (int)$GLOBALS['BE_USER']->user['uid']);
151
152
		}
	}
153

154
}