Commit 88833644 authored by Stefan Busemann's avatar Stefan Busemann

[FEATURE] Reactivate user process

After an unsucessful login attempt, a hook checks if the user exists in
old_users. If the user is found, a local fe_user and a ldap is created.
The old_user will be removed.
The visitor will be redirected to a page on which the process is
described and the user should start the password recovery.
parent 0b421444
Pipeline #4751 passed with stages
in 3 minutes and 2 seconds
<?php
declare(strict_types=1);
namespace T3o\T3omy\Controller;
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 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;
use T3o\T3omy\Hooks\FeManagerHooks;
/**
* Class UserController
*/
class OldUserController extends ActionController
{
/**
* @var OldUserRepository
*/
public $oldUserRepository;
/**
* @var userRepository
*/
public $userRepository;
/**
* @var persistenceManager
*/
public $persistenceManager;
/**
* @param OldUserRepository $oldUserRepository
*/
public function injectOldUsersRepository(OldUserRepository $oldUserRepository)
{
$this->oldUserRepository = $oldUserRepository;
}
/**
* @param PersistenceManager $persistenceManager
*/
public function injectPersistenceManager(PersistenceManager $persistenceManager)
{
$this->persistenceManager = $persistenceManager;
}
function reactivateOldUser($funcRef, $params)
{
$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'));
if ($oldUser) {
if ($this->recoverUser($oldUser)) {
$this->oldUserRepository->remove($oldUser);
$this->persistenceManager->persistAll();
}
// @fixme ?id must be configurable
header('Location:' . $_SERVER['HTTP_HOST']. '?id=37');
}
}
/**
* @param OldUser $oldUser
* @return bool
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
*/
public function recoverUser(OldUser $oldUser): bool
{
$userRepository = $this->objectManager->get(UserRepository::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)));
$userRepository->add($frontendUser);
$this->persistenceManager->persistAll();
$LdapOperation = $this->objectManager->get(FeManagerHooks::class);
$LdapOperation->createUser($frontendUser);
return true;
}
function siteURL()
{
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'].'/';
return $protocol.$domainName;
}
}
<?php
declare(strict_types=1);
namespace T3o\T3omy\Domain\Model;
/**
* Class UserProfileRepository
*/
class OldUser extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* @var string
*/
public $username = '';
/**
* @var string
*/
public $email = '';
/**
* @return string
*/
public function getUsername(): string
{
return $this->username;
}
/**
* @param string $username
*/
public function setUsername(string $username)
{
$this->username = $username;
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail(string $email)
{
$this->email = $email;
}
}
<?php
declare(strict_types=1);
namespace T3o\T3omy\Domain\Repository;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* Class UserProfileRepository
*
* @method findByUid(int $uid) \T3o\T3omy\Domain\Model\OldUser
*/
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();
}
}
......@@ -6,7 +6,11 @@ return [
'label' => 'username',
'default_sortby' => 'ORDER BY username ASC',
'searchFields' => 'title, uid, email',
'iconfile' => 'EXT:t3omy/Resources/Public/Icons/old_user.png'
'iconfile' => 'EXT:t3omy/Resources/Public/Icons/old_user.png',
'delete' => 'deleted',
'label' => 'username',
'default_sortby' => 'ORDER BY username',
'searchFields' => 'username, email'
],
'interface' => [
'showRecordFieldList' => 'uid, username, email',
......
......@@ -67,4 +67,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\Hooks\FeManagerHooks::class . '->reactivateOldUser';
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_error'][] = \T3o\T3omy\Controller\OldUserController::class . '->reactivateOldUser';
......@@ -11,6 +11,7 @@ CREATE TABLE fe_users (
CREATE TABLE old_users (
uid int(11) NOT NULL auto_increment,
pid int (11) unsigned DEFAULT '0',
deleted tinyint(4) unsigned DEFAULT '0' NOT NULL,
username varchar(255) DEFAULT '',
email varchar(255) DEFAULT '',
PRIMARY KEY (uid)
......
......@@ -22,6 +22,11 @@ config.tx_extbase {
tableName = fe_users
}
}
T3o\T3omy\Domain\Model\OldUser{
mapping {
tableName = old_users
}
}
}
}
}
......@@ -33,4 +38,4 @@ config.additionalHeaders {
30.header = X-Content-Security-Policy: default-src 'self' *.typo3.org; script-src 'unsafe-inline' *.typo3.org; font-src 'self' fonts.googleapis.com fonts.gstatic.com; style-src 'self' 'unsafe-inline' fonts.googleapis.com; img-src 'self' data: *.typo3.org www.gravatar.com
40.header = X-Webkit-CSP: default-src 'self' *.typo3.org; script-src 'unsafe-inline' *.typo3.org; font-src 'self' fonts.googleapis.com fonts.gstatic.com; style-src 'self' 'unsafe-inline' fonts.googleapis.com; img-src 'self' data: *.typo3.org www.gravatar.com
}
[end]
\ No newline at end of file
[end]
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