RegisterkeyController.php 20.2 KB
Newer Older
1
<?php
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

/* * *****************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Thomas Layh <thomas@layh.com>
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 * **************************************************************** */

/**
 * Controller for the extension key registration
 */
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Tx_TerFe2_Controller_RegisterkeyController extends Tx_TerFe2_Controller_AbstractTerBasedController
{

    /**
     * @var Tx_TerFe2_Domain_Repository_ExtensionRepository
     */
    protected $extensionRepository;

    /**
     * @var Tx_TerFe2_Domain_Repository_VersionRepository
     */
    protected $versionRepository;

    /**
45
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
46
47
48
49
50
51
52
53
54
55
56
57
     */
    protected $userRepository;

    /**
     * Initializes the controller
     *
     * @return void
     */
    protected function initializeController()
    {
        $this->extensionRepository = $this->objectManager->get('Tx_TerFe2_Domain_Repository_ExtensionRepository');
        $this->versionRepository = $this->objectManager->get('Tx_TerFe2_Domain_Repository_VersionRepository');
58
        $this->userRepository = $this->objectManager->get(Tx_TerFe2_Domain_Repository_UserRepository::class);
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
    }

    /**
     * Initialize all actions
     *
     * @param boolean $uploaded TRUE if an extension version was successfully uploaded
     * @return void
     * @dontvalidate $uploaded
     */
    public function indexAction($uploaded = FALSE)
    {
        // get extensions by user if a user is logged in
        if (!empty($this->frontendUser)) {
            $extensions = $this->extensionRepository->findByFrontendUser($this->frontendUser['username']);
            $expiringExtensions = $this->extensionRepository->findByFrontendUserAndExpiring($this->frontendUser['username']);
            $this->view->assign('extensions', $extensions);
            $this->view->assign('expiringExtensions', $expiringExtensions);
            $this->view->assign('uploaded', $uploaded);
        }
    }

    /**
     * Register a new extension
     *
     * @param string $extensionKey Extension key
     * @return void
     */
86
    public function createAction($extensionKey)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    {

        // Remove spaces from extensionKey if there are some
        $extensionKey = trim($extensionKey);

        // Check if the extension exists in the ter
        if ($this->terConnection->checkExtensionKey($extensionKey, $error)) {
            $extensionData = array(
                'extensionKey' => $extensionKey,
                'title' => $extensionKey,
                'description' => '',
            );

            // Register the extension key at ter server, if successfull, add it to the extension table
            if ($this->terConnection->registerExtension($extensionData)) {
                // Create extension model
                $extension = $this->objectManager->create('Tx_TerFe2_Domain_Model_Extension');
                $extension->setExtKey($extensionKey);
                $extension->setFrontendUser($this->frontendUser['username']);

                $this->extensionRepository->add($extension);
108
                $this->addFlashMessage(
109
                    '', $this->translate('registerkey.key_registered'), \TYPO3\CMS\Core\Messaging\FlashMessage::OK
110
111
112
                );
                $this->redirect('index', 'Registerkey');
            } else {
113
                $this->addFlashMessage(
114
                    $this->resolveWSErrorMessage('not_register.message'), $this->resolveWSErrorMessage('not_register.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
115
116
117
118
119
120
121
                );
            }
        } else {
            /** @var Tx_TerFe2_Domain_Model_Extension $extension */
            $extension = $this->extensionRepository->findOneByExtKey($extensionKey);
            $message = '';
            if ($extension instanceof Tx_TerFe2_Domain_Model_Extension && $extension->getFrontendUser()) {
122
                /** @var \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser */
123
                $frontendUser = $this->userRepository->findOneByUsername($extension->getFrontendUser());
124
                if ($frontendUser instanceof \TYPO3\CMS\Extbase\Domain\Model\FrontendUser) {
125
126
127
128
129
130
131
132
                    $message = $this->resolveWSErrorMessage(
                        $error . '.message',
                        array(
                            '<a href="mailto:' . htmlspecialchars($frontendUser->getEmail()) . '">' . htmlspecialchars($frontendUser->getName()) . '</a>'
                        )
                    );
                }
            }
133
            $this->addFlashMessage(
134
135
                $message,
                $this->resolveWSErrorMessage($error . '.title'),
136
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
            );
        }

        $this->redirect('index', 'Registerkey', NULL, array());
    }

    /**
     * an action to salvage the keys that were registered on the old TYPO3.org, but never had uploads
     *
     * @author Christian Zenker <christian.zenker@599media.de>
     */
    public function salvageAction()
    {
        $error = null;
        $registeredExtensions = $this->terConnection->getExtensionKeysByUser($error);
        if ($error) {
153
            $this->addFlashMessage(
154
                $this->resolveWSErrorMessage($error), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
155
156
            );
        } elseif (!is_array($registeredExtensions)) {
157
            $this->addFlashMessage(
158
                $this->resolveWSErrorMessage('result_empty.message'), $this->resolveWSErrorMessage('result_empty.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
159
160
            );
        } elseif (empty($registeredExtensions)) {
161
            $this->addFlashMessage(
162
                $this->resolveWSErrorMessage('nothing_found'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
163
164
165
166
167
168
169
170
171
172
173
174
175
            );
        } else {

            $countSkipped = 0;
            $countSalvaged = 0;

            // get an array of the already existent extension keys
            $existingExtensionKeys = $this->getExistingExtensionsByList($registeredExtensions);

            foreach ($registeredExtensions as $extension) {
                $extensionKey = $extension['extensionkey'];
                if (in_array($extensionKey, $existingExtensionKeys)) {
                    // if: key already exists
176
//                        $this->addFlashMessage(
czenker's avatar
czenker committed
177
178
//                            sprintf('%s already exists.', $extensionKey),
//                            '',
179
//                            \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE
czenker's avatar
czenker committed
180
//                        );
181
182
183
184
185
                    $countSkipped++;
                } else {
                    $extensionModel = $this->objectManager->create('Tx_TerFe2_Domain_Model_Extension');
                    $extensionModel->setExtKey($extensionKey);
                    $extensionModel->setFrontendUser($extension['ownerusername']);
czenker's avatar
czenker committed
186

187
                    $this->extensionRepository->add($extensionModel);
188
//                        $this->addFlashMessage(
czenker's avatar
czenker committed
189
190
//                            '',
//                            sprintf('%s salvaged.', $extensionKey ),
191
//                            \TYPO3\CMS\Core\Messaging\FlashMessage::OK
czenker's avatar
czenker committed
192
193
//                        );

194
195
196
197
                    $countSalvaged++;
                }
            }
            if ($countSalvaged > 0) {
198
                $this->addFlashMessage(
199
                    $this->translate('registerkey.salvage.success', array($countSalvaged)), '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK
200
201
                );
            } elseif ($countSkipped == 1) {
202
                $this->addFlashMessage(
203
                    $this->translate('registerkey.salvage.pass1.message'), $this->translate('registerkey.salvage.pass.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
204
205
                );
            } elseif ($countSkipped > 1) {
206
                $this->addFlashMessage(
207
                    $this->translate('registerkey.salvage.pass.message', array($countSkipped)), $this->translate('registerkey.salvage.pass.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
208
209
                );
            } else {
210
                $this->addFlashMessage(
211
                    $this->resolveWSErrorMessage('nothing_found'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
                );
            }
        }


        $this->redirect('index', 'Registerkey');
    }

    protected function getExistingExtensionsByList($extensions)
    {
        /**
         * @var array that just holds the given extensionkeys and no meta data
         */
        $keys = array();
        foreach ($extensions as $extension) {
            $keys[] = $extension['extensionkey'];
        }

        if (empty($keys)) {
            return array();
        }


        // query database for existing keys
        $e = $this->extensionRepository->findByExtKeys($keys);
        // strip only the keys from the model
        $existingExtensionKeys = array();
        foreach ($e as $extension) {
            $existingExtensionKeys[] = $extension->getExtKey();
        }

        return $existingExtensionKeys;
    }

    /**
     * Manage registered extensions
     *
     * @obsolete
     * @return void
     */
    public function manageAction()
    {
        $extensions = $this->extensionRepository->findByFrontendUser($this->frontendUser['username']);
        $this->view->assign('extensions', $extensions);
    }

    /**
     * Display the edit form
     *
     * @param Tx_TerFe2_Domain_Model_Extension $extension Extension to modify
     * @dontvalidate $extension
     * @return void
     */
    public function editAction(Tx_TerFe2_Domain_Model_Extension $extension)
    {

        // check if the extension belongs to the current user
        if ($this->securityRole->isReviewer() or $extension->getFrontendUser() == $this->userRepository->findCurrent()) {

            $this->view->assign('extension', $extension);
            $this->view->assign('loggedIn', $this->userRepository->findCurrent());
        } else {
274
            $this->addFlashMessage($this->translate('registerkey.notyourextension'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
275
276
277
278
279
280
281
282
283
284
            $this->redirect('index', 'Registerkey');
        }
    }

    /**
     * Update an existing extension
     *
     * @param Tx_TerFe2_Domain_Model_Extension $extension Extension to modify
     * @return void
     */
285
    public function updateAction(Tx_TerFe2_Domain_Model_Extension $extension)
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
    {

        // check if the extension belongs to the current user
        if ($this->securityRole->isReviewer() or $extension->getFrontendUser() == $GLOBALS['TSFE']->fe_user->user['username']) {

            /**
             * TODO: Modification of the extension key is currently not allowed
             */
            if ($extension->_isDirty('extKey')) {
                $this->redirect('index', 'Registerkey');
            }

            // Check if the extension key has changed
            if ($extension->_isDirty('extKey')) {
                // If extension key has changed, check if the new one is in the ter
                if ($this->terConnection->checkExtensionKey($extension->getExtKey(), $error)) {
                    $error = '';
                    if ($this->terConnection->assignExtensionKey($extension->getExtKey(), $this->frontendUser['username'], $error)) {
                        $this->extensionRepository->update($extension);
305
                        $this->addFlashMessage($this->translate('registerkey.key_updated', '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK));
306
307
308
                        $this->redirect('index', 'Registerkey');
                    } else {
                        // TODO: Show different message by $error code
309
                        $this->addFlashMessage($this->translate('registerkey.key_update_failed', '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR));
310
311
                    }
                } else {
312
                    $this->addFlashMessage(
313
                        $this->resolveWSErrorMessage($error . '.message'), $this->resolveWSErrorMessage($error . '.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
314
315
316
317
318
319
                    );
                }
            }

            $this->redirect('edit', 'Registerkey', NULL, array('extension' => $extension));
        } else {
320
            $this->addFlashMessage($this->translate('registerkey.notyourextension', '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR));
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
            $this->redirect('index', 'Registerkey');
        }
    }


    /**
     * Transfer an extension key to another user
     *
     * @param string $newUser Username of the assignee
     * @param Tx_TerFe2_Domain_Model_Extension $extension Extension to transfer
     * @return void
     */
    public function transferAction($newUser, Tx_TerFe2_Domain_Model_Extension $extension)
    {

        $newUser = trim($newUser);
        if ($newUser == '') {
338
            $this->addFlashMessage(
339
                '', $this->translate('registerkey.newuserempty'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
340
341
342
343
344
345
346
347
348
349
350
            );
        } elseif ((strtolower($extension->getFrontendUser()) == strtolower($GLOBALS['TSFE']->fe_user->user['username'])) || $this->securityRole->isAdmin()) {

            // check if the extension belongs to the current user

            $error = '';

            // Is it possible to assign the key to a new user
            if ($this->terConnection->assignExtensionKey($extension->getExtKey(), $newUser, $error)) {
                $extension->setFrontendUser($newUser);
                $this->extensionRepository->update($extension);
351
                $this->addFlashMessage($this->translate('registerkey.keyTransfered', array($extension->getExtKey(), $newUser)), '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK);
352
            } else {
353
                $this->addFlashMessage(
354
                    $this->resolveWSErrorMessage($error), $this->translate('registerkey.transferError.title', array($extension->getExtKey())), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
355
356
357
                );
            }
        } else {
358
            $this->addFlashMessage(
359
                '', $this->translate('registerkey.notyourextension'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
            );
        }

        $this->redirect('index', 'Registerkey');
    }

    /**
     * Delete an extension key from ter server
     *
     * @param Tx_TerFe2_Domain_Model_Extension $extension Extension to delete
     * @return void
     */
    public function deleteAction(Tx_TerFe2_Domain_Model_Extension $extension)
    {

        if ($extension->getVersionCount() > 0) {
376
            $this->addFlashMessage(
377
                $this->translate('registerkey.deleting_prohibited', array($extension->getExtKey())), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
378
379
380
381
382
383
            );
        } elseif ((strtolower($extension->getFrontendUser()) == strtolower($GLOBALS['TSFE']->fe_user->user['username'])) || $this->securityRole->isAdmin()) {

            // Deleted in ter, then delete the key in the ter_fe2 extension table
            if ($this->terConnection->deleteExtensionKey($extension->getExtKey())) {
                $this->extensionRepository->remove($extension);
384
                $this->addFlashMessage(
385
                    '', $this->translate('registerkey.deleted', array($extension->getExtKey())), \TYPO3\CMS\Core\Messaging\FlashMessage::OK
386
387
                );
            } else {
388
                $this->addFlashMessage(
389
                    $this->resolveWSErrorMessage('cannotbedeleted.message', array($extension->getExtKey())), $this->resolveWSErrorMessage('cannotbedeleted.title', array($extension->getExtKey())), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
390
391
392
                );
            }
        } else {
393
            $this->addFlashMessage($this->translate('registerkey.notyourextension'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
394
395
396
397
398
399
400
401
402
403
404
405
        }

        $this->redirect('index', 'Registerkey');
    }

    /**
     * Sets the expire back to zero and touches the extension
     * The process for getting expiring extensions will be back in 1 year
     *
     * @param \Tx_TerFe2_Domain_Model_Extension $extension
     * @dontvalidate $extension
     * @return void
406
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
     */
    public function keepAction(Tx_TerFe2_Domain_Model_Extension $extension)
    {
        $extension->setExpire(0);
        $this->extensionRepository->update($extension);
        $this->redirect('index', 'Registerkey');
    }

    /**
     * Delete an extension version from ter server
     *
     * @param Tx_TerFe2_Domain_Model_Version $version Extension to delete
     * @dontvalidate $version
     * @return void
     */
    public function deleteExtensionVersionAction(Tx_TerFe2_Domain_Model_Version $version)
    {
        if (!$this->securityRole->isAdmin()) {
425
            $this->addFlashMessage(
426
                $this->resolveWSErrorMessage('not_admin.message'), $this->resolveWSErrorMessage('not_admin.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
427
428
429
430
431
432
433
434
435
436
437
438
439
440
            );
            $this->redirect('index');
        }

        $redirectToIndexAction = FALSE;

        // Deleted in ter, then delete the version (and probably the extension) in the ter_fe2 extension table
        if ($this->terConnection->deleteExtensionVersion($version->getExtension()->getExtKey(), $version->getVersionString())) {
            $version->getExtension()->removeVersion($version);
            $this->versionRepository->remove($version);
            if ($version->getExtension()->getLastVersion() === NULL) {
                $this->extensionRepository->remove($version->getExtension());
                $redirectToIndexAction = TRUE;
            }
441
            $this->addFlashMessage(
442
                '', $this->translate('registerkey.version_deleted', array($version->getVersionString(), $version->getExtension()->getExtKey())), \TYPO3\CMS\Core\Messaging\FlashMessage::OK
443
444
            );
        } else {
445
            $this->addFlashMessage(
446
                $this->resolveWSErrorMessage('extensioncannotbedeleted.message', array($version->getExtension()->getExtKey())), $this->resolveWSErrorMessage('extensioncannotbedeleted.title', array($version->getExtension()->getExtKey())), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
            );
        }
        if ($redirectToIndexAction) {
            $this->redirect('index', 'Registerkey', NULL);
        } else {
            $this->redirect('admin', 'Registerkey', NULL, array('extensionKey' => $version->getExtension()->getExtKey()));
        }
    }

    /**
     * Show all extensions for ter admins
     *
     * @param string $extensionKey
     */
    public function adminAction($extensionKey = '')
    {
        if (!$this->securityRole->isAdmin()) {
464
            $this->addFlashMessage(
465
                $this->resolveWSErrorMessage('no_admin.message'), $this->resolveWSErrorMessage('no_admin.title'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
466
467
468
469
470
            );
            $this->redirect('index');
        }

        $this->extensionRepository->setDefaultOrderings(
471
            array('extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
        );
        if (!$extensionKey) {
            $this->view->assign('adminExtensions', $this->extensionRepository->findAllAdmin());
        } else {
            $this->view->assign('adminExtensions', $this->extensionRepository->findByExtKey($extensionKey));
        }
    }

    /**
     * resolve the error key and get the corresponding translation
     *
     * @param string $error
     * @param array $arguments
     * @return string $message already translated
     */
    protected function resolveWSErrorMessage($error, $arguments = array())
    {
        return $this->translate('registerkey.error.' . $error, $arguments);
    }
491
492

}