RegisterkeyController.php 14.9 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

Jens Jacobsen's avatar
Jens Jacobsen committed
17
use ApacheSolrForTypo3\Solr\GarbageCollector;
18 19 20
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Stefan Froemken's avatar
Stefan Froemken committed
21
use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
22

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

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

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

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

    /**
Stefan Froemken's avatar
Stefan Froemken committed
45
     * inject extensionRepository
46
     *
Stefan Froemken's avatar
Stefan Froemken committed
47
     * @param \T3o\TerFe2\Domain\Repository\ExtensionRepository $extensionRepository
48 49
     * @return void
     */
Stefan Froemken's avatar
Stefan Froemken committed
50
    public function injectExtensionRepository(\T3o\TerFe2\Domain\Repository\ExtensionRepository $extensionRepository)
51
    {
Stefan Froemken's avatar
Stefan Froemken committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        $this->extensionRepository = $extensionRepository;
    }

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

    /**
     * inject userRepository
     *
     * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $userRepository
     * @return void
     */
    public function injectUserRepository(\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
75 76 77 78 79 80 81 82
    }

    /**
     * Initialize all actions
     *
     * @param boolean $uploaded TRUE if an extension version was successfully uploaded
     * @return void
     */
Markus Sommer's avatar
Markus Sommer committed
83
    public function indexAction($uploaded = false)
84 85
    {
        // get extensions by user if a user is logged in
Thomas Löffler's avatar
Thomas Löffler committed
86 87
        if ($GLOBALS['TSFE']->fe_user->user['uid']) {
            $this->frontendUser = $this->userRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
88 89
            $extensions = $this->extensionRepository->findByFrontendUser($this->frontendUser->getUsername());
            $expiringExtensions = $this->extensionRepository->findByFrontendUserAndExpiring($this->frontendUser->getUsername());
90 91 92 93 94 95 96 97 98 99 100 101
            $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
     */
102
    public function createAction($extensionKey)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    {

        // 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
119
                $extension = $this->objectManager->get(\T3o\TerFe2\Domain\Model\Extension::class);
120
                $extension->setExtKey($extensionKey);
121
                $extension->setFrontendUser($this->frontendUser['username']);
122 123

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

Markus Sommer's avatar
Markus Sommer committed
163
        $this->redirect('index', 'Registerkey', null, array());
164 165 166 167 168 169
    }

    /**
     * Transfer an extension key to another user
     *
     * @param string $newUser Username of the assignee
170
     * @param \T3o\TerFe2\Domain\Model\Extension $extension Extension to transfer
171 172
     * @return void
     */
173
    public function transferAction($newUser, \T3o\TerFe2\Domain\Model\Extension $extension)
174 175 176 177
    {

        $newUser = trim($newUser);
        if ($newUser == '') {
178
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
179 180 181
                '',
                $this->translate('registerkey.newuserempty'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
182 183 184 185 186 187 188 189 190 191
            );
        } 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);
192
                $this->addFlashMessage($this->translate('registerkey.keyTransfered', array($extension->getExtKey(), $newUser)), '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK);
193
            } else {
194
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
195 196 197
                    $this->resolveWSErrorMessage($error),
                    $this->translate('registerkey.transferError.title', array($extension->getExtKey())),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
198 199 200
                );
            }
        } else {
201
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
202 203 204
                '',
                $this->translate('registerkey.notyourextension'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
205 206 207 208 209 210 211 212 213
            );
        }

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

    /**
     * Delete an extension key from ter server
     *
214
     * @param \T3o\TerFe2\Domain\Model\Extension $extension Extension to delete
215 216
     * @return void
     */
217
    public function deleteAction(\T3o\TerFe2\Domain\Model\Extension $extension)
218 219 220
    {

        if ($extension->getVersionCount() > 0) {
221
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
222 223 224
                $this->translate('registerkey.deleting_prohibited', array($extension->getExtKey())),
                '',
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
225 226 227 228
            );
        } 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())) {
Jens Jacobsen's avatar
Jens Jacobsen committed
229
                $extensionUid = $extension->getUid();
230
                $this->extensionRepository->remove($extension);
Jens Jacobsen's avatar
Jens Jacobsen committed
231 232

                // ext:solr garbage collector
233
                if (ExtensionManagementUtility::isLoaded('solr')) {
Jens Jacobsen's avatar
Jens Jacobsen committed
234 235
                    $garbageCollector = GeneralUtility::makeInstance(GarbageCollector::class);
                    $garbageCollector->collectGarbage('tx_terfe2_domain_model_extension', $extensionUid);
236
                }
237
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
238 239 240
                    '',
                    $this->translate('registerkey.deleted', array($extension->getExtKey())),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::OK
241 242
                );
            } else {
243
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
244 245 246
                    $this->resolveWSErrorMessage('cannotbedeleted.message', array($extension->getExtKey())),
                    $this->resolveWSErrorMessage('cannotbedeleted.title', array($extension->getExtKey())),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
247 248 249
                );
            }
        } else {
250
            $this->addFlashMessage($this->translate('registerkey.notyourextension'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
251 252 253 254 255 256 257 258 259
        }

        $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
     *
260
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
261
     * @ignorevalidation $extension
262
     * @return void
263
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
264
     */
265
    public function keepAction(\T3o\TerFe2\Domain\Model\Extension $extension)
266
    {
267
        $extension->setExpire(null);
268 269 270 271 272 273 274
        $this->extensionRepository->update($extension);
        $this->redirect('index', 'Registerkey');
    }

    /**
     * Delete an extension version from ter server
     *
275
     * @param \T3o\TerFe2\Domain\Model\Version $version Extension to delete
276
     * @ignorevalidation $version
277 278
     * @return void
     */
279
    public function deleteExtensionVersionAction(\T3o\TerFe2\Domain\Model\Version $version)
280 281
    {
        if (!$this->securityRole->isAdmin()) {
282
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
283 284 285
                $this->resolveWSErrorMessage('not_admin.message'),
                $this->resolveWSErrorMessage('not_admin.title'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
286 287 288 289
            );
            $this->redirect('index');
        }

Markus Sommer's avatar
Markus Sommer committed
290
        $redirectToIndexAction = false;
291 292 293 294 295

        // 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);
296
            if (empty($version->getExtension()->getVersions())) {
Markus Sommer's avatar
Markus Sommer committed
297
                $redirectToIndexAction = true;
298 299 300 301 302
                // remove from index queue
                if (ExtensionManagementUtility::isLoaded('solr')) {
                    $indexQueue = GeneralUtility::makeInstance(Queue::class);
                    $indexQueue->deleteItem('tx_terfe2_domain_model_extension', $version->getExtension()->getUid());
                }
303
            }
304
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
305 306 307
                '',
                $this->translate('registerkey.version_deleted', array($version->getVersionString(), $version->getExtension()->getExtKey())),
                \TYPO3\CMS\Core\Messaging\FlashMessage::OK
308 309
            );
        } else {
310
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
311 312 313
                $this->resolveWSErrorMessage('extensioncannotbedeleted.message', array($version->getExtension()->getExtKey())),
                $this->resolveWSErrorMessage('extensioncannotbedeleted.title', array($version->getExtension()->getExtKey())),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
314 315 316
            );
        }
        if ($redirectToIndexAction) {
Markus Sommer's avatar
Markus Sommer committed
317
            $this->redirect('index', 'Registerkey', null);
318
        } else {
Markus Sommer's avatar
Markus Sommer committed
319
            $this->redirect('admin', 'Registerkey', null, array('extensionKey' => $version->getExtension()->getExtKey()));
320 321 322 323 324 325 326 327 328 329 330
        }
    }

    /**
     * Show all extensions for ter admins
     *
     * @param string $extensionKey
     */
    public function adminAction($extensionKey = '')
    {
        if (!$this->securityRole->isAdmin()) {
331
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
332 333 334
                $this->resolveWSErrorMessage('no_admin.message'),
                $this->resolveWSErrorMessage('no_admin.title'),
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
335 336 337 338 339
            );
            $this->redirect('index');
        }

        $this->extensionRepository->setDefaultOrderings(
340
            array('extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
341 342 343 344 345 346 347 348
        );
        if (!$extensionKey) {
            $this->view->assign('adminExtensions', $this->extensionRepository->findAllAdmin());
        } else {
            $this->view->assign('adminExtensions', $this->extensionRepository->findByExtKey($extensionKey));
        }
    }

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    /**
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
     * @param \T3o\TerFe2\Domain\Model\Version $version
     * @return void
     */
    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()]
        );
    }

369 370 371 372 373 374 375 376 377 378 379
    /**
     * 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);
    }
380
}