RegisterkeyController.php 14 KB
Newer Older
1
<?php
2
namespace T3o\TerFe2\Controller;
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

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

29 30 31 32
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

33 34 35
/**
 * Controller for the extension key registration
 */
36
class RegisterkeyController extends \T3o\TerFe2\Controller\AbstractTerBasedController
37 38 39
{

    /**
40
     * @var \T3o\TerFe2\Domain\Repository\ExtensionRepository
41 42 43 44
     */
    protected $extensionRepository;

    /**
45
     * @var \T3o\TerFe2\Domain\Repository\VersionRepository
46 47 48 49
     */
    protected $versionRepository;

    /**
50
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
51 52 53 54 55 56 57 58 59 60
     */
    protected $userRepository;

    /**
     * Initializes the controller
     *
     * @return void
     */
    protected function initializeController()
    {
61 62
        $this->extensionRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\ExtensionRepository::class);
        $this->versionRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\VersionRepository::class);
63
        $this->userRepository = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository::class);
64 65 66
        $querySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
        $querySettings->setRespectStoragePage(false);
        $this->userRepository->setDefaultQuerySettings($querySettings);
67 68 69 70 71 72 73 74
    }

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

        // 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
111
                $extension = $this->objectManager->get(\T3o\TerFe2\Domain\Model\Extension::class);
112
                $extension->setExtKey($extensionKey);
113
                $extension->setFrontendUser($this->frontendUser['username']);
114 115

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

Markus Sommer's avatar
Markus Sommer committed
155
        $this->redirect('index', 'Registerkey', null, array());
156 157 158 159 160 161
    }

    /**
     * Transfer an extension key to another user
     *
     * @param string $newUser Username of the assignee
162
     * @param \T3o\TerFe2\Domain\Model\Extension $extension Extension to transfer
163 164
     * @return void
     */
165
    public function transferAction($newUser, \T3o\TerFe2\Domain\Model\Extension $extension)
166 167 168 169
    {

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

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

    /**
     * Delete an extension key from ter server
     *
206
     * @param \T3o\TerFe2\Domain\Model\Extension $extension Extension to delete
207 208
     * @return void
     */
209
    public function deleteAction(\T3o\TerFe2\Domain\Model\Extension $extension)
210 211 212
    {

        if ($extension->getVersionCount() > 0) {
213
            $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
214 215 216
                $this->translate('registerkey.deleting_prohibited', array($extension->getExtKey())),
                '',
                \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
217 218 219 220 221
            );
        } 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);
222
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
223 224 225
                    '',
                    $this->translate('registerkey.deleted', array($extension->getExtKey())),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::OK
226 227
                );
            } else {
228
                $this->addFlashMessage(
Markus Sommer's avatar
Markus Sommer committed
229 230 231
                    $this->resolveWSErrorMessage('cannotbedeleted.message', array($extension->getExtKey())),
                    $this->resolveWSErrorMessage('cannotbedeleted.title', array($extension->getExtKey())),
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
232 233 234
                );
            }
        } else {
235
            $this->addFlashMessage($this->translate('registerkey.notyourextension'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
236 237 238 239 240 241 242 243 244
        }

        $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
     *
245
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
246
     * @ignorevalidation $extension
247
     * @return void
248
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
249
     */
250
    public function keepAction(\T3o\TerFe2\Domain\Model\Extension $extension)
251 252 253 254 255 256 257 258 259
    {
        $extension->setExpire(0);
        $this->extensionRepository->update($extension);
        $this->redirect('index', 'Registerkey');
    }

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

Markus Sommer's avatar
Markus Sommer committed
275
        $redirectToIndexAction = false;
276 277 278 279 280

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

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

        $this->extensionRepository->setDefaultOrderings(
325
            array('extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        );
        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);
    }
345
}