Passed
Pull Request — master (#205)
by Ralf
09:03
created

DocumentForm::hasValidCsrfToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace EWW\Dpf\Domain\Model;
4
5
use Exception;
6
use TypeError;
7
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
8
use TYPO3\CMS\Core\FormProtection\FrontendFormProtection;
9
10
/*
11
 * This file is part of the TYPO3 CMS project.
12
 *
13
 * It is free software; you can redistribute it and/or modify it under
14
 * the terms of the GNU General Public License, either version 2
15
 * of the License, or any later version.
16
 *
17
 * For the full copyright and license information, please read the
18
 * LICENSE.txt file that was distributed with this source code.
19
 *
20
 * The TYPO3 project - inspiring people to share!
21
 */
22
23
class DocumentForm extends AbstractFormElement
24
{
25
26
    /**
27
     * @var string CSRF token for this form
28
     */
29
    private $csrfToken;
30
31
    /**
32
     *
33
     * @var integer
34
     */
35
    protected $documentUid;
36
37
    /**
38
     *
39
     * @var boolean
40
     */
41
    protected $primaryFileMandatory;
42
43
    /**
44
     *
45
     * @var string
46
     */
47
    protected $fedoraPid;
48
49
    /**
50
     *
51
     * @var \EWW\Dpf\Domain\Model\File
52
     */
53
    protected $primaryFile;
54
55
    /**
56
     *
57
     * @var array
58
     */
59
    protected $secondaryFiles;
60
61
    /**
62
     *
63
     * @var array
64
     */
65
    protected $deletedFiles;
66
67
    /**
68
     *
69
     * @var array
70
     */
71
    protected $newFiles;
72
73
    /**
74
     *
75
     * @var string
76
     */
77
    protected $objectState;
78
79
    /**
80
     *
81
     * @var boolean
82
     */
83
    protected $valid = false;
84
85
    /**
86
     *
87
     * @var string
88
     */
89
    protected $processNumber;
90
91
    /**
92
     * @var bool
93
     */
94
    protected $temporary;
95
96
    /**
97
     * @var string
98
     */
99
    protected $comment = '';
100
101
    /**
102
     * Assign and persist CSRF token for later form validation.
103
     *
104
     * @param string $csrfToken
105
     */
106
    public function generateCsrfToken()
107
    {
108
        $formProtection = FormProtectionFactory::get();
109
        $this->csrfToken = $formProtection->generateToken('DocumentForm', 'construct', 'DocumentForm');
110
        $formProtection->persistSessionToken();
111
    }
112
113
    /**
114
     * Set the CSRF token for this form
115
     *
116
     * Used when creating a new instance from request form data.
117
     *
118
     * @param string $csrfToken CSRF token to set
119
     * @throws Exception if the given string is empty.
120
     * @throws TypeError if the given string is null
121
     */
122
    public function setCsrfToken(string $csrfToken)
123
    {
124
        if ($csrfToken === "")
125
        {
126
            throw new Exception("A forms CSRF token cannot be empty");
127
        }
128
        $this->csrfToken = $csrfToken;
129
    }
130
131
132
    /**
133
     * Returns the CSRF token of this form
134
     *
135
     * @return string CSRF token for this form
136
     */
137
    public function getCsrfToken()
138
    {
139
        return $this->csrfToken;
140
    }
141
142
143
    /**
144
     * Validates this forms assigned CSRF token with token stored in the TYPO3 session.
145
     *
146
     * @return bool True, is CSRF token is considered valid. False if the token is invalid or missing.
147
     */
148
    public function hasValidCsrfToken()
149
    {
150
        $formProtection = FormProtectionFactory::get();
151
        return $formProtection->validateToken($this->csrfToken, 'DocumentForm', 'construct', 'DocumentForm');
152
    }
153
154
    /**
155
     *
156
     * @return integer
157
     */
158
    public function getDocumentUid()
159
    {
160
        return $this->documentUid;
161
    }
162
163
    /**
164
     *
165
     * @param integer $documentUid
166
     */
167
    public function setDocumentUid($documentUid)
168
    {
169
        $this->documentUid = $documentUid;
170
    }
171
172
    /**
173
     *
174
     * @return boolean
175
     */
176
    public function getPrimaryFileMandatory()
177
    {
178
        return $this->primaryFileMandatory;
179
    }
180
181
    /**
182
     *
183
     * @param boolean $primaryFileMandatory
184
     */
185
    public function setPrimaryFileMandatory($primaryFileMandatory)
186
    {
187
        $this->primaryFileMandatory = boolval($primaryFileMandatory);
188
    }
189
190
    /**
191
     *
192
     * @return string
193
     */
194
    public function getFedoraPid()
195
    {
196
        return $this->fedoraPid;
197
    }
198
199
    /**
200
     *
201
     * @param string $fedoraPid
202
     */
203
    public function setFedoraPid($fedoraPid)
204
    {
205
        $this->fedoraPid = $fedoraPid;
206
    }
207
208
    /**
209
     *
210
     * @param type \EWW\Dpf\Domain\Model\File $primaryFile
211
     */
212
    public function setPrimaryFile($primaryFile)
213
    {
214
        $this->primaryFile = $primaryFile;
215
    }
216
217
    /**
218
     *
219
     * @return \EWW\Dpf\Domain\Model\File
220
     */
221
    public function getPrimaryFile()
222
    {
223
        return $this->primaryFile;
224
    }
225
226
    public function setSecondaryFiles($secondaryFiles)
227
    {
228
        $this->secondaryFiles = $secondaryFiles;
229
    }
230
231
    public function getSecondaryFiles()
232
    {
233
        return $this->secondaryFiles;
234
    }
235
236
    public function getDeletedFiles()
237
    {
238
        return $this->deletedFiles;
239
    }
240
241
    public function setDeletedFiles($deletedFiles)
242
    {
243
        $this->deletedFiles = $deletedFiles;
244
    }
245
246
    public function getNewFiles()
247
    {
248
        return $this->newFiles;
249
    }
250
251
    public function setNewFiles($newFiles)
252
    {
253
        $this->newFiles = $newFiles;
254
    }
255
256
    /**
257
     * @return bool
258
     */
259
    public function getValid()
260
    {
261
        return $this->valid;
262
    }
263
264
    /**
265
     * @param bool $valid
266
     */
267
    public function setValid($valid)
268
    {
269
        $this->valid = boolval($valid);
270
    }
271
272
    public function getNewFileNames()
273
    {
274
        $fileNames = array();
275
        foreach ($this->getNewFiles() as $file) {
276
            $fileNames[] = $file->getTitle();
277
        }
278
        return $fileNames;
279
    }
280
281
    /**
282
     * Sets the process number
283
     *
284
     * @return string
285
     */
286
    public function getProcessNumber()
287
    {
288
        return $this->processNumber;
289
    }
290
291
    /**
292
     * Gets the process number
293
     *
294
     * @param string $processNumber
295
     */
296
    public function setProcessNumber($processNumber)
297
    {
298
        $this->processNumber = $processNumber;
299
    }
300
301
    /**
302
     * Returns if a document is a temporary document.
303
     *
304
     * @return bool
305
     */
306
    public function isTemporary()
307
    {
308
        return $this->temporary;
309
    }
310
311
    /**
312
     * Sets if a document is a temporary document or not.
313
     * @param bool $temporary
314
     */
315
    public function setTemporary($temporary)
316
    {
317
        $this->temporary = boolval($temporary);
318
    }
319
320
    /**
321
     * @return string
322
     */
323
    public function getComment()
324
    {
325
        return $this->comment;
326
    }
327
328
    /**
329
     * @param string $comment
330
     */
331
    public function setComment($comment)
332
    {
333
        $this->comment = $comment;
334
    }
335
}
336