Skip to content

Commit

Permalink
[!!!][TASK] Migrate plugin to CType content element
Browse files Browse the repository at this point in the history
Refs #84
  • Loading branch information
derhansen committed Jul 18, 2024
1 parent 67579b3 commit 19a9bfe
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 2 deletions.
107 changes: 107 additions & 0 deletions Classes/Updates/PluginPermissionUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace Derhansen\FeChangePwd\Updates;

use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Attribute\UpgradeWizard;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

#[UpgradeWizard('feChangePwdPluginPermissionUpdater')]
class PluginPermissionUpdater implements UpgradeWizardInterface
{
public function getIdentifier(): string
{
return 'feChangePwdPluginPermissionUpdater';
}

public function getTitle(): string
{
return 'ext:fe_change_pwd: Migrates plugin permissions for the new CTypes content element';
}

public function getDescription(): string
{
return 'Migrates plugin permissions in ext:fe_change_pwd for the migrated CTypes content element';
}

public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class,
];
}

public function updateNecessary(): bool
{
return $this->checkIfWizardIsRequired();
}

public function executeUpdate(): bool
{
return $this->performMigration();
}

public function checkIfWizardIsRequired(): bool
{
return count($this->getMigrationRecords()) > 0;
}

public function performMigration(): bool
{
$records = $this->getMigrationRecords();

foreach ($records as $record) {
$this->updateExplicitAllowdeny(
$record['uid'],
str_replace('list_type:fechangepwd_pi1', 'CType:fechangepwd_pi1', $record['explicit_allowdeny'])
);
}

return true;
}

/**
* Updates the explicit_allowdeny for the given be_groups record uid with the given value
*/
protected function updateExplicitAllowdeny(int $uid, string $explicitAllowdeny): void
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_groups');
$queryBuilder->update('be_groups')
->set('explicit_allowdeny', $explicitAllowdeny)
->where(
$queryBuilder->expr()->in(
'uid',
$queryBuilder->createNamedParameter($uid, Connection::PARAM_INT)
)
)
->executeStatement();
}

/**
* Returns all record for the migration
*/
protected function getMigrationRecords(): array
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('be_groups');
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

return $queryBuilder
->select('uid', 'explicit_allowdeny')
->from('be_groups')
->where(
$queryBuilder->expr()->like(
'explicit_allowdeny',
$queryBuilder->createNamedParameter('%list_type:fechangepwd_pi1%')
)
)
->executeQuery()
->fetchAllAssociative();
}
}
109 changes: 109 additions & 0 deletions Classes/Updates/PluginToContentTypeUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Derhansen\FeChangePwd\Updates;

use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Attribute\UpgradeWizard;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

#[UpgradeWizard('feChangePwdPluginToContentTypeUpdater')]
class PluginToContentTypeUpdater implements UpgradeWizardInterface
{
public function getIdentifier(): string
{
return 'feChangePwdPluginToContentTypeUpdater';
}

public function getTitle(): string
{
return 'ext:fe_change_pwd: Migrates list_type plugin to CType content element';
}

public function getDescription(): string
{
return 'Migrates the plugin in ext:fe_change_pwd from list_type to CType';
}

public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class,
];
}

public function updateNecessary(): bool
{
return $this->checkIfWizardIsRequired();
}

public function executeUpdate(): bool
{
return $this->performMigration();
}

public function checkIfWizardIsRequired(): bool
{
return count($this->getMigrationRecords()) > 0;
}

public function performMigration(): bool
{
$records = $this->getMigrationRecords();

foreach ($records as $record) {
$this->updateContentElement($record['uid'], $record['list_type']);
}

return true;
}

/**
* Updates the CType and sets `list_type` to an empty string for the given tt_content record
*/
protected function updateContentElement(int $uid, string $newCtype): void
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$queryBuilder->update('tt_content')
->set('CType', $newCtype)
->set('list_type', '')
->where(
$queryBuilder->expr()->in(
'uid',
$queryBuilder->createNamedParameter($uid, Connection::PARAM_INT)
)
)
->executeStatement();
}

/**
* Returns all record for the migration
*/
protected function getMigrationRecords(): array
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tt_content');
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

return $queryBuilder
->select('uid', 'pid', 'CType', 'list_type', 'pi_flexform')
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'CType',
$queryBuilder->createNamedParameter('list')
),
$queryBuilder->expr()->eq(
'list_type',
$queryBuilder->createNamedParameter('fechangepwd_pi1')
)
)
->executeQuery()
->fetchAllAssociative();
}
}
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'author' => 'Torben Hansen',
'author_email' => 'torben@derhansen.com',
'state' => 'stable',
'version' => '5.0.0',
'version' => '5.1.0',
'constraints' => [
'depends' => [
'typo3' => '13.2.0-13.4.99'
Expand Down
3 changes: 2 additions & 1 deletion ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// non-cacheable actions
[
PasswordController::class => 'edit,update,sendChangePasswordCode',
]
],
ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT
);

// Define template override for Fluid email
Expand Down

0 comments on commit 19a9bfe

Please sign in to comment.