diff options
author | Anna Larch <anna@nextcloud.com> | 2021-04-15 14:27:03 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-05-28 10:17:14 +0300 |
commit | 537a766a12b74a3118e8993a5370ae250b96645d (patch) | |
tree | 4f32a74cde113fea1b2097690c62528504df8c3b /lib/Migration | |
parent | 8c8d71e37ad87a3303c0dbe09ab39098893c50c1 (diff) |
Add option to use multiple configs for mail provisioning
Signed-off-by: Anna Larch <anna@nextcloud.com>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Migration')
-rw-r--r-- | lib/Migration/AddSieveToProvisioningConfig.php | 85 | ||||
-rw-r--r-- | lib/Migration/MigrateProvisioningConfig.php | 65 | ||||
-rw-r--r-- | lib/Migration/ProvisionAccounts.php | 12 | ||||
-rw-r--r-- | lib/Migration/Version1100Date20210419080523.php | 196 | ||||
-rw-r--r-- | lib/Migration/Version1100Date20210419121734.php | 29 |
5 files changed, 226 insertions, 161 deletions
diff --git a/lib/Migration/AddSieveToProvisioningConfig.php b/lib/Migration/AddSieveToProvisioningConfig.php deleted file mode 100644 index e871ff1cc..000000000 --- a/lib/Migration/AddSieveToProvisioningConfig.php +++ /dev/null @@ -1,85 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * - * Mail - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Mail\Migration; - -use OCA\Mail\Service\Provisioning\Config as ProvisioningConfig; -use OCA\Mail\Service\Provisioning\ConfigMapper as ProvisioningConfigMapper; -use OCP\IConfig; -use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; - -class AddSieveToProvisioningConfig implements IRepairStep { - - /** @var IConfig */ - private $config; - - /** @var ProvisioningConfigMapper */ - private $configMapper; - - public function __construct(IConfig $config, ProvisioningConfigMapper $configMapper) { - $this->config = $config; - $this->configMapper = $configMapper; - } - - public function getName(): string { - return 'Add sieve defaults to provisioning config'; - } - - public function run(IOutput $output) { - if (!$this->shouldRun()) { - return; - } - - $config = $this->configMapper->load(); - if ($config === null) { - return; - } - - $reflectionClass = new \ReflectionClass(ProvisioningConfig::class); - $reflectionProperty = $reflectionClass->getProperty('data'); - - $reflectionProperty->setAccessible(true); - $data = $reflectionProperty->getValue($config); - - if (!isset($data['sieveEnabled'])) { - $data = array_merge($data, [ - 'sieveEnabled' => false, - 'sieveHost' => '', - 'sievePort' => 4190, - 'sieveUser' => '', - 'sieveSslMode' => 'tls', - ]); - } - - $reflectionProperty->setValue($config, $data); - $this->configMapper->save($config); - - $output->info('added sieve defaults to provisioning config'); - } - - protected function shouldRun(): bool { - $appVersion = $this->config->getAppValue('mail', 'installed_version', '0.0.0'); - return version_compare($appVersion, '1.9.0', '<'); - } -} diff --git a/lib/Migration/MigrateProvisioningConfig.php b/lib/Migration/MigrateProvisioningConfig.php deleted file mode 100644 index 12b90c8f8..000000000 --- a/lib/Migration/MigrateProvisioningConfig.php +++ /dev/null @@ -1,65 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * - * Mail - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Mail\Migration; - -use OCA\Mail\Service\Provisioning\Manager as ProvisioningManager; -use OCP\IConfig; -use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; - -class MigrateProvisioningConfig implements IRepairStep { - - /** @var ProvisioningManager */ - private $provisioningManager; - - /** @var IConfig */ - private $config; - - public function __construct(ProvisioningManager $provisioningManager, - IConfig $config) { - $this->provisioningManager = $provisioningManager; - $this->config = $config; - } - - public function getName(): string { - return 'Migrate Mail provisioning config from config.php to the database'; - } - - public function run(IOutput $output) { - $fromConfigRaw = $this->config->getSystemValue('app.mail.accounts.default'); - if ($fromConfigRaw === '') { - $output->info("No old config found"); - return; - } - - if ($this->provisioningManager->getConfig() !== null) { - $output->info("Mail provisioning config already set, ignoring old config"); - return; - } - - $this->provisioningManager->importConfig($fromConfigRaw); - $this->config->deleteSystemValue('app.mail.accounts.default'); - $output->info("Config migrated. Accounts not updated yet"); - } -} diff --git a/lib/Migration/ProvisionAccounts.php b/lib/Migration/ProvisionAccounts.php index 8487492ca..268e1a99d 100644 --- a/lib/Migration/ProvisionAccounts.php +++ b/lib/Migration/ProvisionAccounts.php @@ -43,17 +43,7 @@ class ProvisionAccounts implements IRepairStep { } public function run(IOutput $output) { - $config = $this->provisioningManager->getConfig(); - if ($config === null) { - $output->info("No Mail provisioning config set"); - return; - } - if (!$config->isActive()) { - $output->info("Mail provisioning is disabled"); - return; - } - - $cnt = $this->provisioningManager->provision($config); + $cnt = $this->provisioningManager->provision(); $output->info("$cnt accounts provisioned"); } } diff --git a/lib/Migration/Version1100Date20210419080523.php b/lib/Migration/Version1100Date20210419080523.php new file mode 100644 index 000000000..5c84fee7b --- /dev/null +++ b/lib/Migration/Version1100Date20210419080523.php @@ -0,0 +1,196 @@ +<?php + +declare(strict_types=1); + +namespace OCA\Mail\Migration; + +use Closure; +use JsonException; +use OCA\Mail\AppInfo\Application; +use OCP\DB\ISchemaWrapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; +use Psr\Log\LoggerInterface; + +class Version1100Date20210419080523 extends SimpleMigrationStep { + + /** @var IConfig */ + protected $config; + + /** @var IDBConnection */ + protected $connection; + + /** @var LoggerInterface */ + protected $logger; + + public function __construct(IConfig $config, IDBConnection $connection, LoggerInterface $logger) { + $this->config = $config; + $this->connection = $connection; + $this->logger = $logger; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + $schema = $schemaClosure(); + + if (!$schema->hasTable('mail_provisionings')) { + $provisioningTable = $schema->createTable('mail_provisionings'); + $provisioningTable->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $provisioningTable->addColumn('provisioning_domain', 'string', [ + 'notnull' => true, + 'length' => 63, + 'default' => '', + ]); + $provisioningTable->addColumn('email_template', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $provisioningTable->addColumn('imap_user', 'string', [ + 'notnull' => true, + 'length' => 128, + 'default' => '', + ]); + $provisioningTable->addColumn('imap_host', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $provisioningTable->addColumn('imap_port', 'smallint', [ + 'notnull' => true, + 'unsigned' => true, + ]); + $provisioningTable->addColumn('imap_ssl_mode', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $provisioningTable->addColumn('smtp_user', 'string', [ + 'notnull' => true, + 'length' => 128, + 'default' => '', + ]); + $provisioningTable->addColumn('smtp_host', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $provisioningTable->addColumn('smtp_port', 'smallint', [ + 'notnull' => true, + 'unsigned' => true, + ]); + $provisioningTable->addColumn('smtp_ssl_mode', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $provisioningTable->addColumn('sieve_enabled', 'boolean', [ + 'notnull' => false, + 'default' => false, + ]); + $provisioningTable->addColumn('sieve_user', 'string', [ + 'notnull' => false, + 'length' => 128, + ]); + $provisioningTable->addColumn('sieve_host', 'string', [ + 'notnull' => false, + 'length' => 128, + ]); + $provisioningTable->addColumn('sieve_port', 'smallint', [ + 'notnull' => false, + 'unsigned' => true, + ]); + $provisioningTable->addColumn('sieve_ssl_mode', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $provisioningTable->setPrimaryKey(['id']); + $provisioningTable->addUniqueIndex( + [ + 'provisioning_domain', + ], + 'mail_provsng_dm_idx' + ); + } + + $accountsTable = $schema->getTable('mail_accounts'); + $accountsTable->addColumn('provisioning_id', 'integer', [ + 'length' => 4, + 'notnull' => false, + ]); + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + // Fetch old config + $raw = $this->config->getAppValue( + Application::APP_ID, + 'provisioning_settings' + ); + if ($raw === '') { + // Not config set yet + return; + } + + try { + $conf = json_decode($raw, true, 10, JSON_THROW_ON_ERROR); + } catch (JsonException $e) { + $this->logger->error('Json decode for old provisioning config failed: ' . $e->getMessage() . ' - building manual config', [ + 'exception' => $e, + ]); + // build config manually + $conf = []; + } + + // create first entry + $insertQb = $this->connection->getQueryBuilder(); + $insertQb->insert('mail_provisionings'); + $insertQb->setValue('provisioning_domain', $insertQb->createNamedParameter('*')); // wildcard domain for this provisioning + $insertQb->setValue('email_template', $insertQb->createNamedParameter($conf['email'] ?? '%USERID%@domain.com')); + $insertQb->setValue('imap_user', $insertQb->createNamedParameter($conf['imapUser'] ?? '%USERID%@domain.com')); + $insertQb->setValue('imap_host', $insertQb->createNamedParameter($conf['imapHost'] ?? 'imap.domain.com')); + $insertQb->setValue('imap_port', $insertQb->createNamedParameter($conf['imapPort'] ?? 993, IQueryBuilder::PARAM_INT)); + $insertQb->setValue('imap_ssl_mode', $insertQb->createNamedParameter($conf['imapSslMode'] ?? 'ssl')); + $insertQb->setValue('smtp_user', $insertQb->createNamedParameter($conf['smtpUser'] ?? '%USERID%@domain.com')); + $insertQb->setValue('smtp_host', $insertQb->createNamedParameter($conf['smtpHost'] ?? 'smtp.domain.com')); + $insertQb->setValue('smtp_port', $insertQb->createNamedParameter($conf['smtpPort'] ?? 587, IQueryBuilder::PARAM_INT)); + $insertQb->setValue('smtp_ssl_mode', $insertQb->createNamedParameter($conf['smtpSslMode'] ?? 'tls')); + $insertQb->setValue('sieve_enabled', $insertQb->createNamedParameter((bool)($conf['sieveEnabled'] ?? false),IQueryBuilder::PARAM_BOOL)); + $insertQb->setValue('sieve_user', $insertQb->createNamedParameter($conf['sieveUser'] ?? '')); + $insertQb->setValue('sieve_host', $insertQb->createNamedParameter($conf['sieveHost'] ?? '')); + $insertQb->setValue('sieve_port', $insertQb->createNamedParameter($conf['sievePort'] ?? 4190, IQueryBuilder::PARAM_INT)); + $insertQb->setValue('sieve_ssl_mode', $insertQb->createNamedParameter($conf['sieveSslMode'] ?? '')); + $insertQb->execute(); + $id = $insertQb->getLastInsertId(); + + // set wildcard provisioning config for all provisioned accounts so we don't use state + $updateQb = $this->connection->getQueryBuilder(); + $updateQb = $updateQb->update('mail_accounts') + ->set('provisioning_id', $updateQb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) + ->where($updateQb->expr()->eq('provisioned', $updateQb->createNamedParameter('true', IQueryBuilder::PARAM_BOOL))); + $updateQb->execute(); + + $this->config->deleteAppValue( + Application::APP_ID, + 'provisioning_settings' + ); + } +} diff --git a/lib/Migration/Version1100Date20210419121734.php b/lib/Migration/Version1100Date20210419121734.php new file mode 100644 index 000000000..80bf9509d --- /dev/null +++ b/lib/Migration/Version1100Date20210419121734.php @@ -0,0 +1,29 @@ +<?php + +declare(strict_types=1); + +namespace OCA\Mail\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1100Date20210419121734 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $accounts = $schema->getTable('mail_accounts'); + $accounts->dropColumn('provisioned'); + + return $schema; + } +} |