Commit a98b4273 authored by Thomas Löffler's avatar Thomas Löffler

Improve code, use core methods, use Hook instead of Controller

parent 24e60beb
Pipeline #4906 failed with stages
in 14 seconds
......@@ -11,17 +11,4 @@ use TYPO3\CMS\Extbase\Persistence\Repository;
*/
class OldUserRepository extends Repository
{
/**
* @param string $username
* @return \T3o\T3omy\Domain\Model\MyProfile|object
*/
public function findByUsername($username)
{
$query = $this->createQuery();
$query->getQuerySettings()
->setRespectStoragePage(false);
return $query->matching($query->equals('username', $username))->execute()->getFirst();
}
}
<?php
declare(strict_types = 1);
namespace T3o\T3omy\Controller;
namespace T3o\T3omy\Hooks;
use In2code\Femanager\Domain\Model\User;
use In2code\Femanager\Domain\Repository\UserRepository;
use T3o\T3omy\Domain\Model\OldUser;
use T3o\T3omy\Domain\Repository\OldUserRepository;
use T3o\T3omy\Hooks\FeManagerHooks;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
/**
* Class UserController
* Class ReactivateUserHook
*/
class OldUserController extends ActionController
class ReactivateUserHook
{
/**
* @var OldUserRepository
* @var \T3o\T3omy\Domain\Repository\OldUserRepository
*/
public $oldUserRepository;
protected $oldUserRepository;
/**
* @var userRepository
* @var \In2code\Femanager\Domain\Repository\UserRepository
*/
public $userRepository;
protected $userRepository;
/**
* @var persistenceManager
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
*/
public $persistenceManager;
protected $persistenceManager;
/**
* @param OldUserRepository $oldUserRepository
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
public function injectOldUsersRepository(OldUserRepository $oldUserRepository)
{
$this->oldUserRepository = $oldUserRepository;
}
protected $objectManager;
/**
* @param PersistenceManager $persistenceManager
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @return void
*/
public function injectPersistenceManager(PersistenceManager $persistenceManager)
{
$this->persistenceManager = $persistenceManager;
}
public function reactivateOldUser($funcRef, $params)
public function reactivateOldUser()
{
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$this->persistenceManager = $this->objectManager->get(PersistenceManager::class);
$this->oldUserRepository = $this->objectManager->get(OldUserRepository::class);
// check if there is a fitting user in old user table
/** @var /T3o/T3omy/Domain/Model/OldUser $oldUser * */
$oldUser = $this->oldUserRepository->findByUsername(GeneralUtility::_POST('user'));
/** @var /T3o/T3omy/Domain/Model/OldUser $oldUser */
$oldUser = $this->oldUserRepository->findOneByUsername(GeneralUtility::_POST('user'));
if ($oldUser) {
if ($this->recoverUser($oldUser)) {
$this->oldUserRepository->remove($oldUser);
$this->persistenceManager->persistAll();
$uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class);
$uri = $uriBuilder->reset()
->setTargetPageUid(37)
->setCreateAbsoluteUri(true)
->buildFrontendUri();
\TYPO3\CMS\Core\Utility\HttpUtility::redirect($uri, \TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_303);
}
// @fixme ?id must be configurable
header('Location:' . $_SERVER['HTTP_HOST'] . '?id=37');
}
}
/**
* @param OldUser $oldUser
* @return bool
* @param \T3o\T3omy\Domain\Model\OldUser $oldUser
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @return bool
*/
public function recoverUser(OldUser $oldUser): bool
{
$userRepository = $this->objectManager->get(UserRepository::class);
$userRepository = $this->objectManager->get(UserRepository::class);
$random = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Crypto\Random::class);
/** @var User $frontendUser */
$frontendUser = $this->objectManager->get(User::class);
$frontendUser->setUsername($oldUser->getUsername());
$frontendUser->setEmail($oldUser->getEmail());
$frontendUser->setName($oldUser->getUsername());
$frontendUser->setPassword(md5($oldUser->getEmail() . rand(459, 9999999)));
$frontendUser->setPassword($random->generateRandomBytes(20));
$userRepository->add($frontendUser);
$this->persistenceManager->persistAll();
$LdapOperation = $this->objectManager->get(FeManagerHooks::class);
$ldapOperation = $this->objectManager->get(FeManagerHooks::class);
$LdapOperation->createUser($frontendUser);
$ldapOperation->createUser($frontendUser);
return true;
}
public function siteURL()
{
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
$domainName = $_SERVER['HTTP_HOST'] . '/';
return $protocol . $domainName;
}
}
......@@ -61,4 +61,4 @@ $dispatcher->connect(
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['password_changed'][] = \T3o\T3omy\Hooks\FeManagerHooks::class . '->writePassword';
// Hook to look, if a user does exist in "old_users" table
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_error'][] = \T3o\T3omy\Controller\OldUserController::class . '->reactivateOldUser';
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_error'][] = \T3o\T3omy\Hooks\ReactivateUserHook::class . '->reactivateOldUser';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment