1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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'
);
}
}
|