Completed
Push — master ( 57f196...fb64ee )
by
unknown
15:57
created

hasOverWriteProtection()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
namespace Aoe\AoeDbSequenzer\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH ([email protected])
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use Aoe\AoeDbSequenzer\Domain\Model\OverwriteProtection;
28
use Aoe\AoeDbSequenzer\Domain\Repository\OverwriteProtectionRepository;
29
use TYPO3\CMS\Core\DataHandling\DataHandler;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Object\ObjectManager;
32
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
33
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
34
35
/**
36
 * @package Aoe\AoeDbSequenzer
37
 */
38
class OverwriteProtectionService
39
{
40
    /**
41
     * @var string
42
     */
43
    const OVERWRITE_PROTECTION_TABLE = 'tx_aoedbsequenzer_domain_model_overwriteprotection';
44
45
    /**
46
     * @var string
47
     */
48
    const OVERWRITE_PROTECTION_TILL = 'tx_aoe_dbsquenzer_protectoverwrite_till';
49
50
    /**
51
     * @var string
52
     */
53
    const OVERWRITE_PROTECTION_MODE = 'tx_aoe_dbsquenzer_protectoverwrite_mode';
54
55
    /**
56
     * array of configured tables that should call the sequenzer
57
     *
58
     * @var array
59
     */
60
    private $supportedTables;
61
62
    /**
63
     * @var OverwriteProtectionRepository
64
     */
65
    private $overwriteProtectionRepository;
66
67
    /**
68
     * @var PersistenceManager
69
     */
70
    private $persistenceManager;
71
72
    /**
73
     * @var ObjectManagerInterface
74
     */
75
    private $objectManager;
76
77
    public function __construct()
78
    {
79
        $extConf = unserialize($GLOBALS ['TYPO3_CONF_VARS'] ['EXT'] ['extConf'] ['aoe_dbsequenzer']);
80
        $explodedValues = explode(',', $extConf ['tables']);
81
        $this->supportedTables = array_map('trim', $explodedValues);
82
83
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
84
        $this->overwriteProtectionRepository = $this->objectManager->get(OverwriteProtectionRepository::class);
85
        $this->persistenceManager = $this->objectManager->get(PersistenceManager::class);
86
    }
87
88
    /**
89
     * Hook for deletes in Typo3 Backend. It also delete all overwrite protection
90
     * @param string $command
91
     * @param string $table
92
     * @param integer $id
93
     */
94
    public function processCmdmap_postProcess($command, $table, $id)
95
    {
96
        if (false === $this->needsOverWriteProtection($table)) {
97
            return;
98
        }
99
        if ($command !== 'delete') {
100
            return;
101
        }
102
        $this->removeOverwriteProtection($id, $table);
103
    }
104
105
    /**
106
     * Hook for updates in Typo3 backend
107
     * @param array $incomingFieldArray
108
     * @param string $table
109
     * @param integer $id
110
     * @param DataHandler $tcemain
111
     */
112
    public function processDatamap_preProcessFieldArray(&$incomingFieldArray, $table, $id, DataHandler &$tcemain)
113
    {
114
        if (false === $this->needsOverWriteProtection($table)) {
115
            return;
116
        }
117
118
        // check, if overwrite-protection-fields are set:
119
        // If they are NOT set, it means, that any other extension maybe called the process_datamap!
120
        if (false === array_key_exists(self::OVERWRITE_PROTECTION_TILL, $incomingFieldArray) ||
121
            false === array_key_exists(self::OVERWRITE_PROTECTION_MODE, $incomingFieldArray)
122
        ) {
123
            return;
124
        }
125
126
        if (false === $this->hasOverWriteProtection($incomingFieldArray)) {
127
            $this->removeOverwriteProtection($id, $table);
128
        } else {
129
            $protectionTime = $incomingFieldArray [self::OVERWRITE_PROTECTION_TILL];
130
            $mode = $incomingFieldArray [self::OVERWRITE_PROTECTION_MODE];
131
132
            $protectionTime = $this->convertClientTimestampToUTC($protectionTime, $table, $tcemain);
133
134
            $queryResult = $this->overwriteProtectionRepository->findByProtectedUidAndTableName($id, $table);
135
            if ($queryResult->count() === 0) {
136
                /* @var $overwriteProtection OverwriteProtection */
137
                $overwriteProtection = $this->objectManager->get(OverwriteProtection::class);
138
                $overwriteProtection->setProtectedMode($mode);
139
                $overwriteProtection->setPid($tcemain->getPID($table, $id));
0 ignored issues
show
Security Bug introduced by
It seems like $tcemain->getPID($table, $id) targeting TYPO3\CMS\Core\DataHandling\DataHandler::getPID() can also be of type false; however, TYPO3\CMS\Extbase\Domain...tDomainObject::setPid() does only seem to accept integer|null, did you maybe forget to handle an error condition?
Loading history...
140
                $overwriteProtection->setProtectedTablename($table);
141
                $overwriteProtection->setProtectedUid($id);
142
                $overwriteProtection->setProtectedTime($protectionTime);
143
                $this->overwriteProtectionRepository->add($overwriteProtection);
144
            } else {
145
                /* @var $overwriteProtection OverwriteProtection */
146
                $overwriteProtection = $queryResult->getFirst();
147
                $overwriteProtection->setProtectedMode($mode);
148
                $overwriteProtection->setProtectedTime($protectionTime);
149
                $this->overwriteProtectionRepository->update($overwriteProtection);
150
            }
151
            $this->persistenceManager->persistAll();
152
        }
153
        unset ($incomingFieldArray [self::OVERWRITE_PROTECTION_TILL]);
154
        unset ($incomingFieldArray [self::OVERWRITE_PROTECTION_MODE]);
155
    }
156
157
    /**
158
     * @param array $fields_values
159
     * @return boolean
160
     */
161
    private function hasOverWriteProtection(array $fields_values)
162
    {
163
        if (isset ($fields_values [self::OVERWRITE_PROTECTION_TILL])) {
164
            $value = trim($fields_values [self::OVERWRITE_PROTECTION_TILL]);
165
            if (false === empty ($value) && false !== is_numeric($value)) {
166
                return true;
167
            }
168
        }
169
        return false;
170
    }
171
172
    /**
173
     * If a table is configured to use the sequenzer
174
     *
175
     * @param string $tableName
176
     * @return boolean
177
     */
178
    private function needsOverWriteProtection($tableName)
179
    {
180
        if ($tableName !== self::OVERWRITE_PROTECTION_TABLE && in_array($tableName, $this->supportedTables)) {
181
            return true;
182
        }
183
        return false;
184
    }
185
186
    /**
187
     * remove overwriteProtection
188
     *
189
     * @param integer $id
190
     * @param string $table
191
     */
192
    private function removeOverwriteProtection($id, $table)
193
    {
194
        $queryResult = $this->overwriteProtectionRepository->findByProtectedUidAndTableName($id, $table);
195
        if ($queryResult->count() > 0) {
196
            $overwriteProtection = $queryResult->getFirst();
197
            $this->overwriteProtectionRepository->remove($overwriteProtection);
198
            $this->persistenceManager->persistAll();
199
        }
200
    }
201
202
    /**
203
     * @param string $dateTimeValue
204
     * @param string $table
205
     * @param DataHandler $dataHandler
206
     * @return string
207
     */
208
    private function convertClientTimestampToUTC($dateTimeValue, $table, DataHandler $dataHandler)
209
    {
210
        $evalArray = explode(',', $GLOBALS['TCA'][$table]['columns'][self::OVERWRITE_PROTECTION_TILL]['config']['eval']);
211
212
        $evalResult = $dataHandler->checkValue_input_Eval($dateTimeValue, $evalArray, null);
213
214
        if (isset($evalResult['value'])) {
215
            return $evalResult['value'];
216
        }
217
218
        return $dateTimeValue;
219
    }
220
}
221