-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathAbstractDataHandlerHook.php
More file actions
166 lines (146 loc) · 5.76 KB
/
AbstractDataHandlerHook.php
File metadata and controls
166 lines (146 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace IchHabRecht\ContentDefender\Hooks;
/*
* This file is part of the TYPO3 extension content_defender.
*
* (c) Nicole Cordes <typo3@cordes.co>
*
* 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.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
use IchHabRecht\ContentDefender\Repository\ContentRepository;
use TYPO3\CMS\Backend\Form\FormDataCompiler;
use TYPO3\CMS\Backend\Form\FormDataGroup\OnTheFly;
use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue;
use TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca;
use TYPO3\CMS\Backend\Form\FormDataProvider\PageTsConfig;
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessCommon;
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem;
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsRemoveUnused;
use TYPO3\CMS\Backend\Form\FormDataProvider\UserTsConfig;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
abstract class AbstractDataHandlerHook
{
/**
* @var ContentRepository
*/
protected $contentRepository;
/**
* @param ContentRepository $contentRepository
*/
public function __construct(ContentRepository $contentRepository = null)
{
$this->contentRepository = $contentRepository ?? GeneralUtility::makeInstance(ContentRepository::class);
}
/**
* @param array $columnConfiguration
* @param array $record
* @return bool
*/
protected function isRecordAllowedByRestriction(array $columnConfiguration, array $record)
{
if (empty($columnConfiguration['allowed.']) && empty($columnConfiguration['disallowed.'])) {
return true;
}
$formDataGroup = GeneralUtility::makeInstance(OnTheFly::class);
$formDataGroup->setProviderList(
[
UserTsConfig::class,
PageTsConfig::class,
InitializeProcessedTca::class,
DatabaseRecordTypeValue::class,
TcaColumnsProcessCommon::class,
TcaColumnsProcessShowitem::class,
TcaColumnsRemoveUnused::class,
]
);
$formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
$formDataCompilerInput = [
'command' => 'edit',
'databaseRow' => $record,
'effectivePid' => $record['pid'],
'tableName' => 'tt_content',
'vanillaUid' => (int)$record['uid'],
];
$result = $formDataCompiler->compile($formDataCompilerInput);
$allowedConfiguration = array_intersect_key($columnConfiguration['allowed.'] ?? [], $result['processedTca']['columns']);
foreach ($allowedConfiguration as $field => $value) {
$allowedValues = GeneralUtility::trimExplode(',', $value);
if (($record['CType'] == 'shortcut') && (!$this->checkShortcut($record, $field, $allowedValues))) {
return false;
}
if (!$this->isAllowedValue($record, $field, $allowedValues)) {
return false;
}
}
$disallowedConfiguration = array_intersect_key($columnConfiguration['disallowed.'] ?? [], $result['processedTca']['columns']);
foreach ($disallowedConfiguration as $field => $value) {
$disallowedValues = GeneralUtility::trimExplode(',', $value);
if (($record['CType'] == 'shortcut') && (!$this->checkShortcut($record, $field, $disallowedValues, false))) {
return false;
}
if (!$this->isAllowedValue($record, $field, $disallowedValues, false)) {
return false;
}
}
return true;
}
/**
* @param array $record
* @param string $field
* @param array $values
* @param bool $allowed
* @return bool
*/
protected function isAllowedValue(array $record, $field, array $values, $allowed = true)
{
return !isset($record[$field])
|| ($allowed && in_array($record[$field], $values))
|| (!$allowed && !in_array($record[$field], $values));
}
/**
* @param array $columnConfiguration
* @param array $record
* @return bool
*/
protected function isRecordAllowedByItemsCount(array $columnConfiguration, array $record)
{
if (empty($columnConfiguration['maxitems'])) {
return true;
}
return (int)$columnConfiguration['maxitems'] >= $this->contentRepository->addRecordToColPos($record);
}
/**
* @param array $shortcut
* @param string $field
* @param array $values
* @param bool $allowed
* @return bool
*/
protected function checkShortcut(array $shortcut, $field, array $values, $allowed = true)
{
$references = GeneralUtility::trimExplode(',', $shortcut['records']);
foreach ($references as $reference) {
$referenceParts = GeneralUtility::revExplode('_', $reference, 2);
$uid = $referenceParts[count($referenceParts) - 1];
$table = 'tt_content';
if (count($referenceParts) == 2) {
$table = $referenceParts[0];
}
$referencedRecord = BackendUtility::getRecord($table, $uid);
if ($referencedRecord['CType'] == 'shortcut') {
return $this->checkShortcut($referencedRecord, $field, $values, $allowed);
}
if (!$this->isAllowedValue($referencedRecord, $field, $values, $allowed)) {
return false;
}
}
return true;
}
}