Commit b0cc1775 authored by Andreas Wolf's avatar Andreas Wolf

[CLEANUP] Restructure CheckForExpiredExtensions task

parent 4b737fd8
Pipeline #2740 passed with stages
in 4 minutes and 21 seconds
......@@ -44,10 +44,31 @@ class CheckForExpiredExtensions extends Task
'typo3v4',
'docteam'
];
$extensionTableConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension');
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$expiredExtensionsByOwner = $this->getExpiredExtensionsByOwner();
foreach ($expiredExtensionsByOwner as $username => $extensions) {
if (in_array($username, $this->blacklistUsers, true)) {
continue;
}
$this->notifyUser($username, $extensions);
}
// remove expired extensions
$this->removeExpiredExtensions();
return true;
}
/**
* @return array
*/
private function getExpiredExtensionsByOwner(): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension')
->createQueryBuilder();
$queryBuilder->select('uid', 'ext_key', 'frontend_user')
->from('tx_terfe2_domain_model_extension')
->where(
......@@ -67,60 +88,81 @@ class CheckForExpiredExtensions extends Task
$expiredExtensionsByOwner[$expiringExtension['frontend_user']][] = $expiringExtension;
}
}
return $expiredExtensionsByOwner;
}
/**
* @param $username
* @param $extensions
* @return mixed
*/
private function notifyUser($username, $extensions)
{
$frontendUserConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('fe_users');
foreach ($expiredExtensionsByOwner as $username => $extensions) {
if (in_array($username, $this->blacklistUsers, true)) {
continue;
}
$queryBuilder = $frontendUserConnection->createQueryBuilder();
$queryBuilder->select('uid', 'username', 'email')
->from('fe_users')
->where($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($username)));
$statement = $queryBuilder->execute();
$statement->execute();
$queryBuilder = $frontendUserConnection->createQueryBuilder();
$queryBuilder->select('uid', 'username', 'email')
->from('fe_users')
->where($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($username)));
$statement = $queryBuilder->execute();
$statement->execute();
$frontendUser = $statement->fetch(\PDO::FETCH_ASSOC);
if (!empty($frontendUser) && GeneralUtility::validEmail($frontendUser['email'])) {
$to = $frontendUser['email'];
$subject = 'Your extension keys are going to expire!';
/** @var StandaloneView $body */
$body = GeneralUtility::makeInstance(StandaloneView::class);
$body->setTemplatePathAndFilename(
GeneralUtility::getFileAbsFileName(
'EXT:ter_fe2/Resources/Private/Templates/Mail/ExpiredExtensions.html'
)
);
$body->assign('extensions', $extensions);
$body->assign('user', $frontendUser);
/** @var MailMessage $mail */
$mail = GeneralUtility::makeInstance(MailMessage::class);
$mail->addFrom('maintenance@typo3.org');
$mail->setTo($to);
$mail->setSubject($subject);
$mail->setBody($body->render());
if ($mail->send()) {
// set every extension of the owner to expire in 30 days
foreach ($extensions as $extension) {
$extensionTableConnection->update(
'tx_terfe2_domain_model_extension',
[
'expire' => strtotime('+30 days')
],
[
'uid' => (int)$extension['uid']
]
);
}
}
$frontendUser = $statement->fetch(\PDO::FETCH_ASSOC);
if (!empty($frontendUser) && GeneralUtility::validEmail($frontendUser['email'])) {
$to = $frontendUser['email'];
$subject = 'Your extension keys are going to expire!';
/** @var StandaloneView $body */
$body = GeneralUtility::makeInstance(StandaloneView::class);
$body->setTemplatePathAndFilename(
GeneralUtility::getFileAbsFileName(
'EXT:ter_fe2/Resources/Private/Templates/Mail/ExpiredExtensions.html'
)
);
$body->assign('extensions', $extensions);
$body->assign('user', $frontendUser);
/** @var MailMessage $mail */
$mail = GeneralUtility::makeInstance(MailMessage::class);
$mail->addFrom('maintenance@typo3.org');
$mail->setTo($to);
$mail->setSubject($subject);
$mail->setBody($body->render());
if ($mail->send()) {
// set every extension of the owner to expire in 30 days
$this->updateExpirationTimeOfExtensions($extensions);
}
}
return $queryBuilder;
}
/**
* @param $extensions
* @param $extensionTableConnection
*/
private function updateExpirationTimeOfExtensions($extensions)
{
$extensionTableConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension');
foreach ($extensions as $extension) {
$extensionTableConnection->update(
'tx_terfe2_domain_model_extension',
[
'expire' => strtotime('+30 days')
],
[
'uid' => (int)$extension['uid']
]
);
}
}
private function removeExpiredExtensions()
{
$extensionTableConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension');
// remove expired extensions
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$queryBuilder->select('uid', 'ext_key', 'frontend_user')
->from('tx_terfe2_domain_model_extension')
......
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