RegisterkeyController.php 14.6 KB
Newer Older
1
<?php
2
namespace T3o\TerFe2\Controller;
3

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!
 */
16

17
use ApacheSolrForTypo3\Solr\GarbageCollector;
18 19 20 21
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

22 23 24
/**
 * Controller for the extension key registration
 */
25
class RegisterkeyController extends \T3o\TerFe2\Controller\AbstractTerBasedController
26 27 28
{

    /**
29
     * @var \T3o\TerFe2\Domain\Repository\ExtensionRepository
30 31 32 33
     */
    protected $extensionRepository;

    /**
34
     * @var \T3o\TerFe2\Domain\Repository\VersionRepository
35 36 37 38
     */
    protected $versionRepository;

    /**
39
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
40 41 42 43
     */
    protected $userRepository;

    /**
44
     * inject extensionRepository
45
     *
46
     * @param \T3o\TerFe2\Domain\Repository\ExtensionRepository $extensionRepository
47
     */
48
    public function injectExtensionRepository(\T3o\TerFe2\Domain\Repository\ExtensionRepository $extensionRepository)
49
    {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        $this->extensionRepository = $extensionRepository;
    }

    /**
     * inject versionRepository
     *
     * @param \T3o\TerFe2\Domain\Repository\VersionRepository $versionRepository
     */
    public function injectVersionRepository(\T3o\TerFe2\Domain\Repository\VersionRepository $versionRepository)
    {
        $this->versionRepository = $versionRepository;
    }

    /**
     * inject userRepository
     *
     * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $userRepository
     */
    public function injectUserRepository(\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
71 72 73 74 75
    }

    /**
     * Initialize all actions
     *
76
     * @param bool $uploaded TRUE if an extension version was successfully uploaded
77
     */
Markus Sommer's avatar
Markus Sommer committed
78
    public function indexAction($uploaded = false)
79 80
    {
        // get extensions by user if a user is logged in
81 82
        if ($GLOBALS['TSFE']->fe_user->user['uid']) {
            $this->frontendUser = $this->userRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
83 84
            $extensions = $this->extensionRepository->findByFrontendUser($this->frontendUser->getUsername());
            $expiringExtensions = $this->extensionRepository->findByFrontendUserAndExpiring($this->frontendUser->getUsername());
85 86 87 88 89 90 91 92 93 94 95
            $this->view->assign('extensions', $extensions);
            $this->view->assign('expiringExtensions', $expiringExtensions);
            $this->view->assign('uploaded', $uploaded);
        }
    }

    /**
     * Register a new extension
     *
     * @param string $extensionKey Extension key
     */
96
    public function createAction($extensionKey)
97 98 99 100 101 102 103
    {

        // 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)) {
104
            $extensionData = [
105 106 107
                'extensionKey' => $extensionKey,
                'title' => $extensionKey,
                'description' => '',
108
            ];
109 110 111 112

            // Register the extension key at ter server, if successfull, add it to the extension table
            if ($this->terConnection->registerExtension($extensionData)) {
                // Create extension model
113
                $extension = $this->objectManager->get(\T3o\TerFe2\Domain\Model\Extension::class);
114
                $extension->setExtKey($extensionKey);
115
                $extension->setFrontendUser($this->frontendUser['username']);
116 117

                $this->extensionRepository->add($extension);
118
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
119 120 121
                    '',
                    $this->translate('registerkey.key_registered'),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::OK
122 123 124
                );
                $this->redirect('index', 'Registerkey');
            } else {
125
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
126 127 128
                    $this->resolveWSErrorMessage('not_register.message'),
                    $this->resolveWSErrorMessage('not_register.title'),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
129 130 131
                );
            }
        } else {
132
            /** @var \T3o\TerFe2\Domain\Model\Extension $extension */
133 134
            $extension = $this->extensionRepository->findOneByExtKey($extensionKey);
            $message = '';
135 136 137
            if ($error === 'key_invalid') {
                $message = $this->resolveWSErrorMessage($error . '.message');
            }
138
            if ($extension instanceof \T3o\TerFe2\Domain\Model\Extension && $extension->getFrontendUser()) {
139
                /** @var \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser */
140
                $frontendUser = $this->userRepository->findOneByUsername($extension->getFrontendUser());
141
                if ($frontendUser instanceof \TYPO3\CMS\Extbase\Domain\Model\FrontendUser) {
142 143
                    $message = $this->resolveWSErrorMessage(
                        $error . '.message',
144
                        [
145
                            '<a href="mailto:' . htmlspecialchars($frontendUser->getEmail()) . '">' . htmlspecialchars($frontendUser->getName()) . '</a>'
146
                        ]
147 148 149
                    );
                }
            }
150
            $this->addFlashMessage(
151 152
                $message,
                $this->resolveWSErrorMessage($error . '.title'),
153
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
154 155 156
            );
        }

157
        $this->redirect('index', 'Registerkey', null, []);
158 159 160 161 162 163
    }

    /**
     * Transfer an extension key to another user
     *
     * @param string $newUser Username of the assignee
164
     * @param \T3o\TerFe2\Domain\Model\Extension $extension Extension to transfer
165
     * @ignorevalidation $extension
166
     */
167
    public function transferAction($newUser, \T3o\TerFe2\Domain\Model\Extension $extension)
168 169 170
    {
        $newUser = trim($newUser);
        if ($newUser == '') {
171
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
172 173 174
                '',
                $this->translate('registerkey.newuserempty'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
175 176 177 178 179 180 181 182 183 184
            );
        } 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);
185
                $this->addFlashMessage($this->translate('registerkey.keyTransfered', [$extension->getExtKey(), $newUser]), '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK);
186
            } else {
187
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
188
                    $this->resolveWSErrorMessage($error),
189
                    $this->translate('registerkey.transferError.title', [$extension->getExtKey()]),
Markus Sommer's avatar
Markus Sommer committed
190
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
191 192 193
                );
            }
        } else {
194
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
195 196 197
                '',
                $this->translate('registerkey.notyourextension'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
198 199 200 201 202 203 204 205 206
            );
        }

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

    /**
     * Delete an extension key from ter server
     *
207
     * @param \T3o\TerFe2\Domain\Model\Extension $extension Extension to delete
208
     */
209
    public function deleteAction(\T3o\TerFe2\Domain\Model\Extension $extension)
210 211
    {
        if ($extension->getVersionCount() > 0) {
212
            $this->addFlashMessage(
213
                $this->translate('registerkey.deleting_prohibited', [$extension->getExtKey()]),
Markus Sommer's avatar
Markus Sommer committed
214 215
                '',
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
216 217 218 219
            );
        } 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())) {
220
                $extensionUid = $extension->getUid();
221
                $this->extensionRepository->remove($extension);
222 223

                // ext:solr garbage collector
224
                if (ExtensionManagementUtility::isLoaded('solr')) {
225 226
                    $garbageCollector = GeneralUtility::makeInstance(GarbageCollector::class);
                    $garbageCollector->collectGarbage('tx_terfe2_domain_model_extension', $extensionUid);
227
                }
228
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
229
                    '',
230
                    $this->translate('registerkey.deleted', [$extension->getExtKey()]),
Markus Sommer's avatar
Markus Sommer committed
231
                    \TYPO3\CMS\Core\Messaging\FlashMessage::OK
232 233
                );
            } else {
234
                $this->addFlashMessage(
235 236
                    $this->resolveWSErrorMessage('cannotbedeleted.message', [$extension->getExtKey()]),
                    $this->resolveWSErrorMessage('cannotbedeleted.title', [$extension->getExtKey()]),
Markus Sommer's avatar
Markus Sommer committed
237
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
238 239 240
                );
            }
        } else {
241
            $this->addFlashMessage($this->translate('registerkey.notyourextension'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
242 243 244 245 246 247 248 249 250
        }

        $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
     *
251
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
252
     * @ignorevalidation $extension
253
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
254
     */
255
    public function keepAction(\T3o\TerFe2\Domain\Model\Extension $extension)
256
    {
257
        $extension->setExpire(null);
258 259 260 261 262 263 264
        $this->extensionRepository->update($extension);
        $this->redirect('index', 'Registerkey');
    }

    /**
     * Delete an extension version from ter server
     *
265
     * @param \T3o\TerFe2\Domain\Model\Version $version Extension to delete
266
     * @ignorevalidation $version
267
     */
268
    public function deleteExtensionVersionAction(\T3o\TerFe2\Domain\Model\Version $version)
269 270
    {
        if (!$this->securityRole->isAdmin()) {
271
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
272 273 274
                $this->resolveWSErrorMessage('not_admin.message'),
                $this->resolveWSErrorMessage('not_admin.title'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
275 276 277 278
            );
            $this->redirect('index');
        }

Markus Sommer's avatar
Markus Sommer committed
279
        $redirectToIndexAction = false;
280 281 282 283 284

        // 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);
285
            if (empty($version->getExtension()->getVersions())) {
Markus Sommer's avatar
Markus Sommer committed
286
                $redirectToIndexAction = true;
287 288 289 290 291
                // remove from index queue
                if (ExtensionManagementUtility::isLoaded('solr')) {
                    $indexQueue = GeneralUtility::makeInstance(Queue::class);
                    $indexQueue->deleteItem('tx_terfe2_domain_model_extension', $version->getExtension()->getUid());
                }
292
            }
293
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
294
                '',
295
                $this->translate('registerkey.version_deleted', [$version->getVersionString(), $version->getExtension()->getExtKey()]),
Markus Sommer's avatar
Markus Sommer committed
296
                \TYPO3\CMS\Core\Messaging\FlashMessage::OK
297 298
            );
        } else {
299
            $this->addFlashMessage(
300 301
                $this->resolveWSErrorMessage('extensioncannotbedeleted.message', [$version->getExtension()->getExtKey()]),
                $this->resolveWSErrorMessage('extensioncannotbedeleted.title', [$version->getExtension()->getExtKey()]),
Markus Sommer's avatar
Markus Sommer committed
302
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
303 304 305
            );
        }
        if ($redirectToIndexAction) {
Markus Sommer's avatar
Markus Sommer committed
306
            $this->redirect('index', 'Registerkey', null);
307
        } else {
308
            $this->redirect('admin', 'Registerkey', null, ['extensionKey' => $version->getExtension()->getExtKey()]);
309 310 311 312 313 314 315 316 317 318 319
        }
    }

    /**
     * Show all extensions for ter admins
     *
     * @param string $extensionKey
     */
    public function adminAction($extensionKey = '')
    {
        if (!$this->securityRole->isAdmin()) {
320
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
321 322 323
                $this->resolveWSErrorMessage('no_admin.message'),
                $this->resolveWSErrorMessage('no_admin.title'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
324 325 326 327 328
            );
            $this->redirect('index');
        }

        $this->extensionRepository->setDefaultOrderings(
329
            ['extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING]
330 331 332 333 334 335 336 337
        );
        if (!$extensionKey) {
            $this->view->assign('adminExtensions', $this->extensionRepository->findAllAdmin());
        } else {
            $this->view->assign('adminExtensions', $this->extensionRepository->findByExtKey($extensionKey));
        }
    }

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    /**
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
     * @param \T3o\TerFe2\Domain\Model\Version $version
     */
    public function setAsLastVersionAction(\T3o\TerFe2\Domain\Model\Extension $extension, \T3o\TerFe2\Domain\Model\Version $version)
    {
        $extension->setLastVersion($version);
        $this->extensionRepository->update($extension);
        $this->redirectWithMessage(
            'Version ' . $version->getVersionString() . ' set as latest version!',
            'admin',
            'New last version',
            \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
            null,
            null,
            ['extensionKey' => $extension->getExtKey()]
        );
    }

357 358 359 360 361 362 363
    /**
     * resolve the error key and get the corresponding translation
     *
     * @param string $error
     * @param array $arguments
     * @return string $message already translated
     */
364
    protected function resolveWSErrorMessage($error, $arguments = [])
365 366 367
    {
        return $this->translate('registerkey.error.' . $error, $arguments);
    }
368
}