first commit
This commit is contained in:
commit
e81864e683
2984 files changed
+267797
No files matched your search
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use Exception;
|
||||
use In2code\Femanager\Domain\Repository\UserGroupRepository;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
|
||||
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
|
||||
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
||||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
|
||||
|
||||
/**
|
||||
* Class AbstractUtility
|
||||
*/
|
||||
abstract class AbstractUtility
|
||||
{
|
||||
/**
|
||||
* Get table configuration array for a defined table
|
||||
*
|
||||
* @param string $table
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
protected static function getTcaFromTable($table = 'fe_users'): array
|
||||
{
|
||||
$tca = [];
|
||||
if (!empty($GLOBALS['TCA'][$table])) {
|
||||
$tca = $GLOBALS['TCA'][$table];
|
||||
}
|
||||
return $tca;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ConnectionPool
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function getConnectionPool(): ConnectionPool
|
||||
{
|
||||
return GeneralUtility::makeInstance(ConnectionPool::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
protected static function getFilesArray(): array
|
||||
{
|
||||
return (array)$_FILES;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserGroupRepository
|
||||
*/
|
||||
protected static function getUserGroupRepository(): UserGroupRepository
|
||||
{
|
||||
return GeneralUtility::makeInstance(UserGroupRepository::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TypoScriptFrontendController|null
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
protected static function getTypoScriptFrontendController()
|
||||
{
|
||||
return $GLOBALS['TSFE'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get TYPO3 encryption key
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
protected static function getEncryptionKey(): string
|
||||
{
|
||||
if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
|
||||
throw new \UnexpectedValueException('No encryption key found in this TYPO3 installation', 1516373945265);
|
||||
}
|
||||
return $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extension configuration from LocalConfiguration.php
|
||||
*
|
||||
* @return array
|
||||
* @throws ExtensionConfigurationExtensionNotConfiguredException
|
||||
* @throws ExtensionConfigurationPathDoesNotExistException
|
||||
*/
|
||||
protected static function getExtensionConfiguration(): array
|
||||
{
|
||||
return (array)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('femanager');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ContentObjectRenderer
|
||||
* @throws Exception
|
||||
*/
|
||||
protected static function getContentObject(): ContentObjectRenderer
|
||||
{
|
||||
return GeneralUtility::makeInstance(ContentObjectRenderer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ConfigurationManagerInterface
|
||||
* @codeCoverageIgnore
|
||||
* @throws Exception
|
||||
*/
|
||||
protected static function getConfigurationManager(): ConfigurationManagerInterface
|
||||
{
|
||||
return GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BackendUserAuthentication
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
protected static function getBackendUserAuthentication()
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
|
||||
/**
|
||||
* Class BackendUserUtility
|
||||
*/
|
||||
class BackendUserUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function isAdminAuthentication()
|
||||
{
|
||||
$userAuthentication = self::getBackendUserAuthentication();
|
||||
|
||||
if ($userAuthentication->user['admin'] === 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$tsConfigEnableLoginAs = (int)($userAuthentication->getTSConfig()['tx_femanager.']['UserBackend.']['enableLoginAs'] ?? 0);
|
||||
|
||||
return $tsConfigEnableLoginAs === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BackendUserAuthentication
|
||||
*/
|
||||
public static function getBackendUserAuthentication()
|
||||
{
|
||||
return parent::getBackendUserAuthentication();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\MathUtility;
|
||||
use TYPO3\CMS\Core\Utility\RootlineUtility;
|
||||
|
||||
/**
|
||||
* Class BackendUtility
|
||||
*/
|
||||
class BackendUtility
|
||||
{
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public static function getPageIdentifier(): int
|
||||
{
|
||||
return (int)GeneralUtility::_GET('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URI to edit a record in backend
|
||||
*
|
||||
* @param string $tableName like "fe_users"
|
||||
* @param int $identifier record identifier to edit
|
||||
* @param bool $addReturnUrl add current URI as returnUrl
|
||||
* @return string
|
||||
*/
|
||||
public static function getBackendEditUri(string $tableName, int $identifier, bool $addReturnUrl = true): string
|
||||
{
|
||||
$uriParameters = [
|
||||
'edit' => [
|
||||
$tableName => [
|
||||
$identifier => 'edit'
|
||||
]
|
||||
]
|
||||
];
|
||||
if ($addReturnUrl) {
|
||||
$uriParameters['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
|
||||
}
|
||||
return (string)GeneralUtility::makeInstance(UriBuilder::class)
|
||||
->buildUriFromRoute('record_edit', $uriParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URI to create a new record in backend
|
||||
*
|
||||
* @param string $tableName like "fe_users"
|
||||
* @param int $pageIdentifier page identifier to store the new record in
|
||||
* @param bool $addReturnUrl add current URI as returnUrl
|
||||
* @return string
|
||||
*/
|
||||
public static function getBackendNewUri(string $tableName, int $pageIdentifier, bool $addReturnUrl = true): string
|
||||
{
|
||||
$uriParameters = [
|
||||
'edit' => [
|
||||
$tableName => [
|
||||
$pageIdentifier => 'new'
|
||||
]
|
||||
]
|
||||
];
|
||||
if ($addReturnUrl) {
|
||||
// @codeCoverageIgnoreStart
|
||||
$uriParameters['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
return (string)GeneralUtility::makeInstance(UriBuilder::class)
|
||||
->buildUriFromRoute('record_edit', $uriParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string "plugin" or "module"
|
||||
*/
|
||||
public static function getPluginOrModuleString(): string
|
||||
{
|
||||
$string = 'plugin';
|
||||
if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
|
||||
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()
|
||||
) {
|
||||
$string = 'module';
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all GET/POST params without module name and token
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected static function getCurrentParameters(): array
|
||||
{
|
||||
$parameters = [];
|
||||
$ignoreKeys = [
|
||||
'M',
|
||||
'moduleToken'
|
||||
];
|
||||
foreach ((array)GeneralUtility::_GET() as $key => $value) {
|
||||
if (in_array($key, $ignoreKeys)) {
|
||||
continue;
|
||||
}
|
||||
$parameters[$key] = $value;
|
||||
}
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $pageUid [optional] the current pageuid
|
||||
* @return array
|
||||
*/
|
||||
public static function loadTS($pageUid = null)
|
||||
{
|
||||
$pageUid = ($pageUid && MathUtility::canBeInterpretedAsInteger($pageUid)) ? $pageUid : GeneralUtility::_GP('id');
|
||||
$TSObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
|
||||
$TSObj->tt_track = 0;
|
||||
$TSObj->runThroughTemplates(GeneralUtility::makeInstance(RootlineUtility::class, $pageUid, '')->get());
|
||||
$TSObj->generateConfig();
|
||||
|
||||
return $TSObj->setup;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
||||
|
||||
/**
|
||||
* Class ConfigurationUtility
|
||||
*/
|
||||
class ConfigurationUtility extends AbstractUtility
|
||||
{
|
||||
const DEFAULT_CONFIGURATION = [
|
||||
'_TypoScriptIncluded' => 0,
|
||||
'_enable' => '',
|
||||
'_enable.' => [],
|
||||
'dataProcessors' => [],
|
||||
'edit./forceValues./beforeAnyConfirmation.' => [],
|
||||
'edit./redirect' => 'TEXT',
|
||||
'edit./redirect.' => [],
|
||||
'edit./requestRedirect' => 'TEXT',
|
||||
'edit./requestRedirect.' => [],
|
||||
'edit/fillEmailWithUsername' => '0',
|
||||
'edit/notifyAdmin' => '',
|
||||
'edit/email/notifyAdmin/receiver/email/value' => '',
|
||||
'edit/misc/passwordSave' => '',
|
||||
'invitation/misc/passwordSave' => '',
|
||||
'invitation./email./invitationAdminNotify.' => [],
|
||||
'invitation./email./invitationAdminNotifyStep1.' => [],
|
||||
'invitation./email./invitationRefused.' => [],
|
||||
'invitation./forceValues./beforeAnyConfirmation.' => [],
|
||||
'invitation./redirect' => 'TEXT',
|
||||
'invitation./redirect.' => [],
|
||||
'invitation./redirectDelete' => 'TEXT',
|
||||
'invitation./redirectDelete.' => [],
|
||||
'invitation./redirectStep1' => 'TEXT',
|
||||
'invitation./redirectStep1.' => [],
|
||||
'invitation./redirectPasswordChanged' => 'TEXT',
|
||||
'invitation./redirectPasswordChanged.' => [],
|
||||
'invitation./requestRedirect' => 'TEXT',
|
||||
'invitation./requestRedirect.' => [],
|
||||
'invitation/fillEmailWithUsername' => '0',
|
||||
'invitation/notifyAdmin' => 0,
|
||||
'invitation/email/invitationAdminNotify/receiver/name/value' => 'femanager',
|
||||
'invitation/email/invitationRefused/receiver/name/value' => 'femanager',
|
||||
'invitation/notifyAdminStep1' => 0,
|
||||
'invitation/email/invitationAdminNotifyStep1/receiver/name/value' => 'femanager',
|
||||
'new./adminConfirmationRedirect' => '',
|
||||
'new./email./createAdminConfirmation.' => [],
|
||||
'new./email./createAdminNotify.' => [],
|
||||
'new./email./createAdminNotify./receiver./email./value' => '',
|
||||
'new./email./createAdminNotify./subject' => 'TEXT',
|
||||
'new./email./createAdminNotify./subject.' => [],
|
||||
'new./email./createUserConfirmation.' => [],
|
||||
'new./email./createUserConfirmation./sender./email./value' => '',
|
||||
'new./email./createUserConfirmation./sender./name./value' => '',
|
||||
'new./email./createUserConfirmation./subject' => 'TEXT',
|
||||
'new./email./createUserConfirmation./subject.' => [],
|
||||
'new./email./createUserConfirmation./confirmUserConfirmation' => '0',
|
||||
'new./email./createUserConfirmation./confirmUserConfirmationRefused' => '0',
|
||||
'new./email./createUserConfirmation./confirmAdminConfirmation' => '0',
|
||||
'new./email./createUserNotify.' => [],
|
||||
'new./email./createUserNotify./sender./email./value' => '9999',
|
||||
'new./email./createUserNotify./sender./name./value' => '9999',
|
||||
'new./email./createUserNotify./subject' => 'TEXT',
|
||||
'new./email./createUserNotify./subject.' => [],
|
||||
'new./email./createUserNotifyRefused.' => [],
|
||||
'new./fillEmailWithUsername' => 0,
|
||||
'new./forceValues./beforeAnyConfirmation.' => [],
|
||||
'new./forceValues./onAdminConfirmation.' => [],
|
||||
'new./forceValues./onUserConfirmation.' => [],
|
||||
'new./login' => '0',
|
||||
'new./misc./passwordSave' => 0,
|
||||
'new./notifyAdmin' => '',
|
||||
'new./redirect' => 'TEXT',
|
||||
'new./redirect.' => [],
|
||||
'new./requestRedirect' => 'TEXT',
|
||||
'new./requestRedirect.' => [],
|
||||
'new./userConfirmationRedirect' => 'TEXT',
|
||||
'new./userConfirmationRedirect.' => [],
|
||||
'new/misc/passwordSave' => 0,
|
||||
'persistence./storagePid' => '0',
|
||||
'receiver./email' => 'TEXT',
|
||||
'receiver./email.' => [],
|
||||
'receiver./name' => 'TEXT',
|
||||
'receiver./name.' => [],
|
||||
'sender./email' => 'TEXT',
|
||||
'sender./email.' => [],
|
||||
'sender./name' => 'TEXT',
|
||||
'sender./name.' => [],
|
||||
'priority' => 'TEXT',
|
||||
'priority.' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function isDisableModuleActive(): bool
|
||||
{
|
||||
$configuration = self::getExtensionConfiguration();
|
||||
|
||||
return $configuration['disableModule'] === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function isConfirmationModuleActive(): bool
|
||||
{
|
||||
$configuration = self::getExtensionConfiguration();
|
||||
|
||||
return $configuration['enableConfirmationModule'] === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function isDisableLogActive(): bool
|
||||
{
|
||||
$configuration = self::getExtensionConfiguration();
|
||||
|
||||
return $configuration['disableLog'] === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get complete Typoscript or only a special value by a given path
|
||||
*
|
||||
* @param string $path "misc.uploadFolder" or empty for complete TypoScript array
|
||||
* @return string
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function getConfiguration(string $path = '')
|
||||
{
|
||||
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
|
||||
$typoscript = $configurationManager->getConfiguration(
|
||||
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
|
||||
'Femanager',
|
||||
'Pi1'
|
||||
);
|
||||
if (!empty($path)) {
|
||||
$typoscript = ArrayUtility::getValueByPath($typoscript, $path, '.');
|
||||
}
|
||||
|
||||
return $typoscript;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function isBackendModuleFilterUserConfirmation(): bool
|
||||
{
|
||||
$config = BackendUserUtility::getBackendUserAuthentication()->getTSConfig(
|
||||
)['tx_femanager.']['UserBackend.']['confirmation.']['filter.']['userConfirmation'] ?? false;
|
||||
|
||||
return (bool)$config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function IsResendUserConfirmationRequestActive(): bool
|
||||
{
|
||||
$config = BackendUserUtility::getBackendUserAuthentication()->getTSConfig(
|
||||
)['tx_femanager.']['UserBackend.']['confirmation.']['ResendUserConfirmationRequest'] ?? false;
|
||||
return (bool)$config;
|
||||
}
|
||||
|
||||
public static function IsCreateUserNotifyActive($config): bool
|
||||
{
|
||||
if (ConfigurationUtility::getValue(
|
||||
'new/email/createUserNotify/sender/email/value',
|
||||
$config
|
||||
) && ConfigurationUtility::getValue(
|
||||
'new./email./createUserNotify./sender./name./value',
|
||||
$config
|
||||
)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getDefaultConfiguration($key)
|
||||
{
|
||||
return self::DEFAULT_CONFIGURATION[$key] ?? null;
|
||||
}
|
||||
|
||||
public static function getValue($key, $config)
|
||||
{
|
||||
try {
|
||||
return ArrayUtility::getValueByPath($config, $key);
|
||||
} catch (MissingArrayPathException $ex) {
|
||||
return self::getDefaultConfiguration($key);
|
||||
}
|
||||
}
|
||||
|
||||
public static function notifyAdminAboutEdits($settings)
|
||||
{
|
||||
return self::getValue('edit/email/notifyAdmin/_enable/value', $settings)
|
||||
&& (
|
||||
self::getValue('edit/email/notifyAdmin/receiver/email/value', $settings)
|
||||
|| self::getValue('edit/notifyAdmin', $settings)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
|
||||
/**
|
||||
* Class FileUtility
|
||||
*/
|
||||
class FileUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* @param string $path Absolute path
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function createFolderIfNotExists(string $path)
|
||||
{
|
||||
if (!is_dir($path) && !GeneralUtility::mkdir($path)) {
|
||||
throw new \UnexpectedValueException(
|
||||
'Folder ' . self::getRelativeFolderFromAbsolutePath($path) . ' does not exists and can not be created!',
|
||||
1516373962125
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get relative path from absolute path, but don't touch if it's already a relative path
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function getRelativeFolderFromAbsolutePath(string $path): string
|
||||
{
|
||||
if (PathUtility::isAbsolutePath($path)) {
|
||||
$path = PathUtility::getRelativePathTo($path);
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use In2code\Femanager\Domain\Model\User;
|
||||
use In2code\Femanager\Domain\Model\UserGroup;
|
||||
use TYPO3\CMS\Core\Context\Context;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Class FrontendUtility
|
||||
*/
|
||||
class FrontendUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $pluginNames = [
|
||||
'tx_femanager_pi1',
|
||||
'tx_femanager_pi2'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get current pid
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getCurrentPid(): int
|
||||
{
|
||||
return (int)self::getTypoScriptFrontendController()->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get frontend language uid
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getFrontendLanguageUid(): int
|
||||
{
|
||||
$languageUid = 0;
|
||||
$languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');
|
||||
$languageUid = $languageAspect->getId();
|
||||
return $languageUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getCharset(): string
|
||||
{
|
||||
return self::getTypoScriptFrontendController()->metaCharset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function getUriToCurrentPage(): string
|
||||
{
|
||||
$contentObject = ObjectUtility::getContentObject();
|
||||
$configuration = [
|
||||
'parameter' => self::getCurrentPid()
|
||||
];
|
||||
return $contentObject->typoLink_URL($configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object properties from forceValues in TypoScript
|
||||
*
|
||||
* @param User $user
|
||||
* @param array $settings
|
||||
* @return User $object
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function forceValues(User $user, array $settings)
|
||||
{
|
||||
foreach ((array)$settings as $field => $config) {
|
||||
$config = null;
|
||||
if (stristr($field, '.')) {
|
||||
continue;
|
||||
}
|
||||
// value to set
|
||||
$value = self::getContentObject()->cObjGetSingle($settings[$field], $settings[$field . '.']);
|
||||
self::forceValue($user, $field, $value);
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set single object property from forceValues in TypoScript
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $field
|
||||
* @param any $value
|
||||
*/
|
||||
public static function forceValue(User $user, string $field, $value): void
|
||||
{
|
||||
if ($field === 'usergroup') {
|
||||
// need objectstorage for usergroup field
|
||||
$user->removeAllUsergroups();
|
||||
$values = GeneralUtility::trimExplode(',', $value, true);
|
||||
$userGroupRepository = self::getUserGroupRepository();
|
||||
|
||||
foreach ($values as $usergroupUid) {
|
||||
/** @var UserGroup $usergroup */
|
||||
$usergroup = $userGroupRepository->findByUid($usergroupUid);
|
||||
$user->addUsergroup($usergroup);
|
||||
}
|
||||
} else {
|
||||
// set value
|
||||
$setterMethod = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
|
||||
if (method_exists($user, $setterMethod)) {
|
||||
$user->{$setterMethod}($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getControllerName(): string
|
||||
{
|
||||
$controllerName = '';
|
||||
foreach (self::$pluginNames as $pluginName) {
|
||||
$variables = GeneralUtility::_GPmerged($pluginName);
|
||||
if (!empty($variables['controller'])) {
|
||||
$controllerName = $variables['controller'];
|
||||
}
|
||||
}
|
||||
return $controllerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getActionName(): string
|
||||
{
|
||||
$actionName = '';
|
||||
foreach (self::$pluginNames as $pluginName) {
|
||||
$variables = GeneralUtility::_GPmerged($pluginName);
|
||||
if (!empty($variables['action'])) {
|
||||
$actionName = $variables['action'];
|
||||
}
|
||||
}
|
||||
return $actionName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use In2code\Femanager\Domain\Model\User;
|
||||
|
||||
/**
|
||||
* Class HashUtility
|
||||
*/
|
||||
class HashUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* Check if given hash is correct
|
||||
*
|
||||
* @param string $hash
|
||||
* @param User $user
|
||||
* @return bool
|
||||
*/
|
||||
public static function validHash(string $hash, User $user, string $suffix = '')
|
||||
{
|
||||
return self::createHashForUser($user, $suffix) === $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create hash for a user
|
||||
*
|
||||
* @param User $user
|
||||
* @return string
|
||||
*/
|
||||
public static function createHashForUser(User $user, string $suffix = '')
|
||||
{
|
||||
return self::hashString($user->getUsername() . $suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Hash from String and TYPO3 Encryption Key (if available)
|
||||
*
|
||||
* @param string $string Any String to hash
|
||||
* @param int $length Hash Length
|
||||
* @return string $hash Hashed String
|
||||
*/
|
||||
protected static function hashString($string, $length = 16)
|
||||
{
|
||||
return substr(md5($string . self::getEncryptionKey()), 0, $length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Extbase\Utility\LocalizationUtility as ExtbaseLocalizationUtility;
|
||||
|
||||
/**
|
||||
* Class LocalizationUtility
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class LocalizationUtility
|
||||
{
|
||||
/**
|
||||
* Returns the localized label of the LOCAL_LANG key, but prefill extensionName
|
||||
*
|
||||
* @param string $key The key from the LOCAL_LANG array for which to return the value.
|
||||
* @param string $extensionName The name of the extension
|
||||
* @param array $arguments the arguments of the extension, being passed over to vsprintf
|
||||
* @return string|null
|
||||
*/
|
||||
public static function translate($key, $extensionName = 'femanager', $arguments = null)
|
||||
{
|
||||
return ExtbaseLocalizationUtility::translate($key, $extensionName, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get locallang translation with key prefix tx_femanager_domain_model_log.state.
|
||||
*
|
||||
* @param int $state
|
||||
* @return string|null
|
||||
*/
|
||||
public static function translateByState($state)
|
||||
{
|
||||
return self::translate('tx_femanager_domain_model_log.state.' . $state);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use In2code\Femanager\Domain\Model\Log;
|
||||
use In2code\Femanager\Domain\Model\User;
|
||||
use In2code\Femanager\Domain\Repository\LogRepository;
|
||||
use In2code\Femanager\Event\UserLogEvent;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Class LogUtility
|
||||
*/
|
||||
class LogUtility
|
||||
{
|
||||
/**
|
||||
* @var LogRepository
|
||||
*/
|
||||
private $logRepository;
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
public function __construct(LogRepository $logRepository, EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->logRepository = $logRepository;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $state State to log
|
||||
* @param User $user Related User
|
||||
* @param array $additionalProperties for individual logging
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function log($state, User $user, array $additionalProperties = [])
|
||||
{
|
||||
if (!ConfigurationUtility::isDisableLogActive()) {
|
||||
$log = GeneralUtility::makeInstance(Log::class);
|
||||
$log->setTitle(LocalizationUtility::translateByState($state));
|
||||
$log->setState($state);
|
||||
$log->setUser($user);
|
||||
$this->logRepository->add($log);
|
||||
}
|
||||
|
||||
$this->eventDispatcher->dispatch(new UserLogEvent($user, $state, $additionalProperties));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||
use TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException;
|
||||
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
|
||||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
||||
|
||||
/**
|
||||
* Class ObjectUtility
|
||||
*/
|
||||
class ObjectUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* @param string $tableName
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public static function getQueryBuilder(string $tableName): QueryBuilder
|
||||
{
|
||||
return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ContentObjectRenderer
|
||||
*/
|
||||
public static function getContentObject(): ContentObjectRenderer
|
||||
{
|
||||
return GeneralUtility::makeInstance(ContentObjectRenderer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if object was changed or not
|
||||
*
|
||||
* @param object $object
|
||||
* @return bool
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function isDirtyObject($object): bool
|
||||
{
|
||||
foreach (array_keys($object->_getProperties()) as $propertyName) {
|
||||
try {
|
||||
$property = ObjectAccess::getProperty($object, $propertyName);
|
||||
} catch (PropertyNotAccessibleException $e) {
|
||||
// if property can not be accessed
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* std::Property (string, int, etc..),
|
||||
* PHP-Objects (DateTime, RecursiveIterator, etc...),
|
||||
* TYPO3-Objects (user, page, etc...)
|
||||
*/
|
||||
if (!$property instanceof ObjectStorage) {
|
||||
if ($object->_isDirty($propertyName)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* ObjectStorage
|
||||
*/
|
||||
if ($property->_isDirty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implode subjobjects on a property (example for usergroups: "ug1, ug2, ug3")
|
||||
*
|
||||
* @param ObjectStorage $objectStorage
|
||||
* @param string $property
|
||||
* @param string $glue
|
||||
* @return string
|
||||
*/
|
||||
public static function implodeObjectStorageOnProperty(
|
||||
ObjectStorage $objectStorage,
|
||||
string $property = 'uid',
|
||||
string $glue = ', '
|
||||
): string {
|
||||
$values = [];
|
||||
if (!empty($objectStorage)) {
|
||||
foreach ($objectStorage as $object) {
|
||||
try {
|
||||
$values[] = ObjectAccess::getProperty($object, $property);
|
||||
} catch (\Exception $exception) {
|
||||
unset($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
return implode($glue, $values);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Class StringUtility
|
||||
*/
|
||||
class StringUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* Only allowed a-z, A-Z, 0-9, -, .
|
||||
* Others will be replaced
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $replace
|
||||
* @return string
|
||||
*/
|
||||
public static function cleanString($filename, $replace = '_')
|
||||
{
|
||||
return preg_replace('/[^a-zA-Z0-9-\.]/', $replace, trim($filename));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read values between brackets
|
||||
*
|
||||
* test(1,2,3) => 1,2,3
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function getValuesInBrackets($value)
|
||||
{
|
||||
preg_match_all('/\(.*?\)/i', $value, $result);
|
||||
return str_replace(['(', ')'], '', $result[0][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read values before brackets
|
||||
*
|
||||
* test(1,2,3) => test
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function getValuesBeforeBrackets($value)
|
||||
{
|
||||
$valueParts = GeneralUtility::trimExplode('(', $value, true);
|
||||
return $valueParts[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string starts with another string
|
||||
*
|
||||
* @param string $haystack
|
||||
* @param string $needle
|
||||
* @return bool
|
||||
*/
|
||||
public static function startsWith($haystack, $needle)
|
||||
{
|
||||
return stristr($haystack, $needle) && strrpos($haystack, $needle, -strlen($haystack)) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string ends with another string
|
||||
*
|
||||
* @param string $haystack
|
||||
* @param string $needle
|
||||
* @return string
|
||||
*/
|
||||
public static function endsWith($haystack, $needle)
|
||||
{
|
||||
return stristr($haystack, $needle) && strlen($haystack) - strlen($needle) === strpos($haystack, $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create array for swiftmailer
|
||||
* sender and receiver mail/name combination with fallback
|
||||
*
|
||||
* @param string $emailString String with separated emails (splitted by \n)
|
||||
* @param string $name Name for every email name combination
|
||||
* @return array $mailArray
|
||||
*/
|
||||
public static function makeEmailArray($emailString, $name = 'femanager')
|
||||
{
|
||||
$emails = GeneralUtility::trimExplode(PHP_EOL, $emailString, true);
|
||||
$mailArray = [];
|
||||
foreach ($emails as $email) {
|
||||
if (GeneralUtility::validEmail($email)) {
|
||||
$mailArray[$email] = $name;
|
||||
}
|
||||
}
|
||||
return $mailArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* createRandomFileName
|
||||
*
|
||||
* @param int $length
|
||||
* @param bool $addUpperCase
|
||||
* @param bool $addSpecialCharacters
|
||||
* @return string
|
||||
*/
|
||||
public static function getRandomString($length = 32, $addUpperCase = true, $addSpecialCharacters = true)
|
||||
{
|
||||
$characters = self::getNumbersString() . self::getCharactersString();
|
||||
if ($addUpperCase) {
|
||||
$characters .= self::getUpperCharactersString();
|
||||
}
|
||||
if ($addSpecialCharacters) {
|
||||
$characters .= '#+*&%$§()[]{}!.:-_,;';
|
||||
}
|
||||
$string = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$key = mt_rand(0, strlen($characters) - 1);
|
||||
$string .= $characters[$key];
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string "0123456789"
|
||||
*/
|
||||
public static function getNumbersString()
|
||||
{
|
||||
return implode('', range(0, 9));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string "abcdefghijklmnopqrstuvwxyz"
|
||||
*/
|
||||
public static function getCharactersString()
|
||||
{
|
||||
return implode('', range('a', 'z'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
*/
|
||||
public static function getUpperCharactersString()
|
||||
{
|
||||
return implode('', range('A', 'Z'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove double slashes from URI but don't touch the protocol (http:// e.g.)
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public static function removeDoubleSlashesFromUri($string)
|
||||
{
|
||||
return preg_replace('~([^:]|^)(/{2,})~', '$1/', $string);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Fluid\View\StandaloneView;
|
||||
|
||||
/**
|
||||
* Class TemplateUtility
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class TemplateUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* Get absolute path for templates with fallback
|
||||
* In case of multiple paths this will just return the first one.
|
||||
* See getTemplateFolders() for an array of paths.
|
||||
*
|
||||
* @param string $part "template", "partial", "layout"
|
||||
* @return string
|
||||
* @see getTemplateFolders()
|
||||
*/
|
||||
public static function getTemplateFolder($part = 'template')
|
||||
{
|
||||
$matches = self::getTemplateFolders($part);
|
||||
return !empty($matches) ? $matches[0] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get absolute paths for templates with fallback
|
||||
* Returns paths from *RootPaths and *RootPath and "hardcoded"
|
||||
* paths pointing to the EXT:femanager-resources.
|
||||
*
|
||||
* @param string $part "template", "partial", "layout"
|
||||
* @param bool $returnAllPaths Default: FALSE, If FALSE only paths
|
||||
* for the first configuration (Paths, Path, hardcoded)
|
||||
* will be returned. If TRUE all (possible) paths will be returned.
|
||||
* @return array
|
||||
*/
|
||||
public static function getTemplateFolders($part = 'template', $returnAllPaths = false)
|
||||
{
|
||||
$templatePaths = [];
|
||||
$configuration = self::getConfigurationManager()
|
||||
->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'femanager');
|
||||
if (!empty($configuration['view'][$part . 'RootPaths'] ?? '')) {
|
||||
$templatePaths = $configuration['view'][$part . 'RootPaths'] ?? '';
|
||||
$templatePaths = array_values($templatePaths);
|
||||
}
|
||||
if ($returnAllPaths || empty($templatePaths)) {
|
||||
$path = $configuration['view'][$part . 'RootPath'] ?? '';
|
||||
if (!empty($path)) {
|
||||
$templatePaths[] = $path;
|
||||
}
|
||||
}
|
||||
if ($returnAllPaths || empty($templatePaths)) {
|
||||
$templatePaths[] = 'EXT:femanager/Resources/Private/' . ucfirst($part) . 's/';
|
||||
}
|
||||
$templatePaths = array_unique($templatePaths);
|
||||
$absolutePaths = [];
|
||||
foreach ($templatePaths as $templatePath) {
|
||||
$absolutePaths[] = GeneralUtility::getFileAbsFileName($templatePath);
|
||||
}
|
||||
return $absolutePaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return path and filename for a file or path.
|
||||
* Only the first existing file/path will be returned.
|
||||
* respect *RootPaths and *RootPath
|
||||
*
|
||||
* @param string $pathAndFilename e.g. Email/Name.html
|
||||
* @param string $part "template", "partial", "layout"
|
||||
* @return string Filename/path
|
||||
*/
|
||||
public static function getTemplatePath($pathAndFilename, $part = 'template')
|
||||
{
|
||||
$matches = self::getTemplatePaths($pathAndFilename, $part);
|
||||
return !empty($matches) ? end($matches) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return path and filename for one or many files/paths.
|
||||
* Only existing files/paths will be returned.
|
||||
* respect *RootPaths and *RootPath
|
||||
*
|
||||
* @param string $pathAndFilename Path/filename (Email/Name.html) or path
|
||||
* @param string $part "template", "partial", "layout"
|
||||
* @return array All existing matches found
|
||||
*/
|
||||
public static function getTemplatePaths($pathAndFilename, $part = 'template')
|
||||
{
|
||||
$pathAndFilenames = [];
|
||||
$absolutePaths = self::getTemplateFolders($part, true);
|
||||
foreach ($absolutePaths as $absolutePath) {
|
||||
if (file_exists($absolutePath . $pathAndFilename)) {
|
||||
$pathAndFilenames[] = $absolutePath . $pathAndFilename;
|
||||
}
|
||||
}
|
||||
return $pathAndFilenames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get standaloneview with default properties
|
||||
*
|
||||
* @param string $controllerName
|
||||
* @param string $extensionName
|
||||
* @param string $pluginName
|
||||
* @param string $format
|
||||
* @return StandaloneView
|
||||
*/
|
||||
public static function getDefaultStandAloneView(
|
||||
$controllerName = 'New',
|
||||
$extensionName = 'Femanager',
|
||||
$pluginName = 'Pi1',
|
||||
$format = 'html'
|
||||
) {
|
||||
/** @var StandaloneView $standAloneView */
|
||||
$standAloneView = GeneralUtility::makeInstance(StandaloneView::class);
|
||||
$standAloneView->getRequest()->setControllerExtensionName($extensionName);
|
||||
$standAloneView->getRequest()->setPluginName($pluginName);
|
||||
$standAloneView->getRequest()->setControllerName($controllerName);
|
||||
$standAloneView->setFormat($format);
|
||||
$standAloneView->setLayoutRootPaths(TemplateUtility::getTemplateFolders('layout'));
|
||||
$standAloneView->setPartialRootPaths(TemplateUtility::getTemplateFolders('partial'));
|
||||
return $standAloneView;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace In2code\Femanager\Utility;
|
||||
|
||||
use In2code\Femanager\Domain\Model\User;
|
||||
use In2code\Femanager\Domain\Model\UserGroup;
|
||||
use In2code\Femanager\Domain\Repository\UserRepository;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\Argon2iPasswordHash;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\BlowfishPasswordHash;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\Md5PasswordHash;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\Pbkdf2PasswordHash;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\PhpassPasswordHash;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
|
||||
|
||||
/**
|
||||
* Class UserUtility
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class UserUtility extends AbstractUtility
|
||||
{
|
||||
/**
|
||||
* Return current logged in fe_user
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
public static function getCurrentUser()
|
||||
{
|
||||
if (self::getPropertyFromUser() !== null) {
|
||||
$userRepository = GeneralUtility::makeInstance(UserRepository::class);
|
||||
|
||||
return $userRepository->findByUid((int)self::getPropertyFromUser());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get property from current logged in Frontend User
|
||||
*
|
||||
* @param string $propertyName
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getPropertyFromUser($propertyName = 'uid')
|
||||
{
|
||||
if (!empty(self::getTypoScriptFrontendController()->fe_user->user[$propertyName])) {
|
||||
return self::getTypoScriptFrontendController()->fe_user->user[$propertyName];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Usergroups from current logged in user
|
||||
*
|
||||
* array(
|
||||
* 1,
|
||||
* 5,
|
||||
* 7
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getCurrentUsergroupUids()
|
||||
{
|
||||
$currentLoggedInUser = self::getCurrentUser();
|
||||
$usergroupUids = [];
|
||||
if ($currentLoggedInUser !== null) {
|
||||
foreach ($currentLoggedInUser->getUsergroup() as $usergroup) {
|
||||
$usergroupUids[] = $usergroup->getUid();
|
||||
}
|
||||
}
|
||||
|
||||
return $usergroupUids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Autogenerate username and password if it's empty
|
||||
*
|
||||
* @param User $user
|
||||
* @return User $user
|
||||
*/
|
||||
public static function fallbackUsernameAndPassword(User $user)
|
||||
{
|
||||
$settings = self::getConfigurationManager()->getConfiguration(
|
||||
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
|
||||
'Femanager',
|
||||
'Pi1'
|
||||
);
|
||||
|
||||
if (isset($settings['new']['misc']['autogenerate'])) {
|
||||
$autogenerateSettings = $settings['new']['misc']['autogenerate'];
|
||||
|
||||
if (!$user->getUsername()) {
|
||||
$user->setUsername(
|
||||
StringUtility::getRandomString(
|
||||
$autogenerateSettings['username']['length'],
|
||||
$autogenerateSettings['username']['addUpperCase'],
|
||||
$autogenerateSettings['username']['addSpecialCharacters']
|
||||
)
|
||||
);
|
||||
if ($user->getEmail()) {
|
||||
$user->setUsername($user->getEmail());
|
||||
}
|
||||
}
|
||||
if (!$user->getPassword()) {
|
||||
$password = StringUtility::getRandomString(
|
||||
$autogenerateSettings['password']['length'],
|
||||
$autogenerateSettings['password']['addUpperCase'],
|
||||
$autogenerateSettings['password']['addSpecialCharacters']
|
||||
);
|
||||
$user->setPassword($password);
|
||||
$user->setPasswordAutoGenerated($password);
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param array $settings
|
||||
* @return User
|
||||
*/
|
||||
public static function takeEmailAsUsername(User $user, array $settings)
|
||||
{
|
||||
if (ConfigurationUtility::getValue('new/fillEmailWithUsername', $settings) === '1') {
|
||||
$user->setEmail($user->getUsername());
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite usergroups from user by flexform settings
|
||||
*
|
||||
* @param User $user
|
||||
* @param array $settings
|
||||
* @param string $controllerName
|
||||
* @return User $object
|
||||
*/
|
||||
public static function overrideUserGroup(User $user, $settings, $controllerName = 'new')
|
||||
{
|
||||
if (!empty($settings[$controllerName]['overrideUserGroup'])) {
|
||||
$user->removeAllUsergroups();
|
||||
$usergroupUids = GeneralUtility::trimExplode(',', $settings[$controllerName]['overrideUserGroup'], true);
|
||||
foreach ($usergroupUids as $usergroupUid) {
|
||||
/** @var UserGroup $usergroup */
|
||||
$usergroup = self::getUserGroupRepository()->findByUid($usergroupUid);
|
||||
$user->addUsergroup($usergroup);
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert password to Argon2i, Bcrypt, Pbkdf2, Phpass, Blowfish or Md5 hash
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $method
|
||||
* @throws InvalidPasswordHashException
|
||||
*/
|
||||
public static function convertPassword(User $user, $method)
|
||||
{
|
||||
if (array_key_exists('password', UserUtility::getDirtyPropertiesFromUser($user))) {
|
||||
self::hashPassword($user, $method);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash a password from $user->getPassword()
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $method "Argon2i", "Bcrypt", "Pbkdf2", "Phpass", "Blowfish", "md5" or "none" ("sha1" for TYPO3 V8)
|
||||
* @throws InvalidPasswordHashException
|
||||
*/
|
||||
public static function hashPassword(User &$user, $method)
|
||||
{
|
||||
$hashInstance = false;
|
||||
$saltedHashPassword = $user->getPassword();
|
||||
/** @var PasswordHashFactory $passwordHashFactory */
|
||||
$passwordHashFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
|
||||
switch ($method) {
|
||||
case 'Argon2i':
|
||||
$hashInstance = GeneralUtility::makeInstance(Argon2iPasswordHash::class);
|
||||
break;
|
||||
|
||||
case 'Bcrypt':
|
||||
$hashInstance = GeneralUtility::makeInstance(BcryptPasswordHash::class);
|
||||
break;
|
||||
|
||||
case 'Pbkdf2':
|
||||
$hashInstance = GeneralUtility::makeInstance(Pbkdf2PasswordHash::class);
|
||||
break;
|
||||
|
||||
case 'Phpass':
|
||||
$hashInstance = GeneralUtility::makeInstance(PhpassPasswordHash::class);
|
||||
break;
|
||||
|
||||
case 'Blowfish':
|
||||
$hashInstance = GeneralUtility::makeInstance(BlowfishPasswordHash::class);
|
||||
break;
|
||||
|
||||
case 'md5':
|
||||
$hashInstance = GeneralUtility::makeInstance(Md5PasswordHash::class);
|
||||
break;
|
||||
case 'none':
|
||||
break;
|
||||
|
||||
default:
|
||||
$hashInstance = $passwordHashFactory->getDefaultHashInstance(TYPO3_MODE);
|
||||
}
|
||||
|
||||
if ($hashInstance === false) {
|
||||
$user->setPassword($saltedHashPassword);
|
||||
} else {
|
||||
$user->setPassword($hashInstance->getHashedPassword($saltedHashPassword));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get changed properties (compare two objects with same getter methods)
|
||||
*
|
||||
* @param User $changedObject
|
||||
* @return array
|
||||
* [firstName][old] = Alex
|
||||
* [firstName][new] = Alexander
|
||||
*/
|
||||
public static function getDirtyPropertiesFromUser(User $changedObject)
|
||||
{
|
||||
$dirtyProperties = [];
|
||||
$ignoreProperties = [
|
||||
'txFemanagerChangerequest',
|
||||
'ignoreDirty',
|
||||
'isOnline',
|
||||
'lastlogin'
|
||||
];
|
||||
|
||||
foreach ($changedObject->_getCleanProperties() as $propertyName => $oldPropertyValue) {
|
||||
if (method_exists($changedObject, 'get' . ucfirst($propertyName))
|
||||
&& !in_array($propertyName, $ignoreProperties)
|
||||
) {
|
||||
$newPropertyValue = $changedObject->{'get' . ucfirst($propertyName)}();
|
||||
if (!is_object($oldPropertyValue) || !is_object($newPropertyValue)) {
|
||||
if ($oldPropertyValue !== $newPropertyValue) {
|
||||
$dirtyProperties[$propertyName]['old'] = $oldPropertyValue;
|
||||
$dirtyProperties[$propertyName]['new'] = $newPropertyValue;
|
||||
}
|
||||
} else {
|
||||
if (get_class($oldPropertyValue) === 'DateTime') {
|
||||
/** @var $oldPropertyValue \DateTime */
|
||||
/** @var $newPropertyValue \DateTime */
|
||||
if ($oldPropertyValue->getTimestamp() !== $newPropertyValue->getTimestamp()) {
|
||||
$dirtyProperties[$propertyName]['old'] = $oldPropertyValue->getTimestamp();
|
||||
$dirtyProperties[$propertyName]['new'] = $newPropertyValue->getTimestamp();
|
||||
}
|
||||
} elseif (get_class($oldPropertyValue) === ObjectStorage::class) {
|
||||
$titlesOld = ObjectUtility::implodeObjectStorageOnProperty($oldPropertyValue);
|
||||
$titlesNew = ObjectUtility::implodeObjectStorageOnProperty($newPropertyValue);
|
||||
if ($titlesOld !== $titlesNew) {
|
||||
$dirtyProperties[$propertyName]['old'] = $titlesOld;
|
||||
$dirtyProperties[$propertyName]['new'] = $titlesNew;
|
||||
}
|
||||
} else {
|
||||
$uidOld = ObjectAccess::getProperty($oldPropertyValue, 'uid');
|
||||
$uidNew = ObjectAccess::getProperty($newPropertyValue, 'uid');
|
||||
if ($uidOld !== $uidNew) {
|
||||
$dirtyProperties[$propertyName]['old'] = $uidOld;
|
||||
$dirtyProperties[$propertyName]['new'] = $uidNew;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $dirtyProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* overwrite user with old values and xml with new values
|
||||
*
|
||||
* @param User $user
|
||||
* @param array $dirtyProperties
|
||||
* @return User $user
|
||||
*/
|
||||
public static function rollbackUserWithChangeRequest($user, $dirtyProperties)
|
||||
{
|
||||
$existingProperties = $user->_getCleanProperties();
|
||||
|
||||
// reset old values
|
||||
$user->setUserGroup($existingProperties['usergroup']);
|
||||
foreach ($dirtyProperties as $propertyName => $propertyValue) {
|
||||
$propertyValue = null;
|
||||
$user->{'set' . ucfirst($propertyName)}($existingProperties[$propertyName]);
|
||||
}
|
||||
|
||||
// store changes as xml in field fe_users.tx_femanager_changerequest
|
||||
$user->setTxFemanagerChangerequest(GeneralUtility::array2xml($dirtyProperties, '', 0, 'changes'));
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove FE Session to a given user
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public static function removeFrontendSessionToUser(User $user)
|
||||
{
|
||||
self::getConnectionPool()->getConnectionForTable('fe_sessions')->delete(
|
||||
'fe_sessions',
|
||||
['ses_userid' => (int)$user->getUid()]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if FE Session exists
|
||||
*
|
||||
* @param User $user
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkFrontendSessionToUser(User $user)
|
||||
{
|
||||
$queryBuilder = self::getConnectionPool()->getQueryBuilderForTable('fe_sessions');
|
||||
|
||||
$row = $queryBuilder->select('ses_id')
|
||||
->from('fe_sessions')
|
||||
->where(
|
||||
$queryBuilder->expr()->eq('ses_userid', (int)$user->getUid())
|
||||
)
|
||||
->execute()
|
||||
->fetch();
|
||||
|
||||
return !empty($row['ses_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Login FE-User
|
||||
*
|
||||
* @param User $user
|
||||
* @param string|null $storagePids
|
||||
*/
|
||||
public static function login(User $user, $storagePids = null)
|
||||
{
|
||||
// ensure a session cookie is set (in case there is no session yet)
|
||||
$GLOBALS['TSFE']->fe_user->setAndSaveSessionData('dummy', true);
|
||||
// create the session (destroys all existing session data in the session backend!)
|
||||
$GLOBALS['TSFE']->fe_user->createUserSession(['uid' => (int)$user->getUid()]);
|
||||
// re-fetch the session from the database so that the internal 'userSession' property of TSFE gets updated
|
||||
$GLOBALS['TSFE']->fe_user->fetchUserSession();
|
||||
// write the session data again to the session backend; preserves what was there before!!
|
||||
$GLOBALS['TSFE']->fe_user->setAndSaveSessionData('dummy', true);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user