Skip to content

Commit 214ad04

Browse files
Persist provisioned accounts
Signed-off-by: Christoph Wurst <[email protected]>
1 parent 26c13bc commit 214ad04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2236
-398
lines changed

appinfo/info.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- **🙈 We’re not reinventing the wheel!** Based on the great [Horde](http://horde.org) libraries.
1313
- **📬 Want to host your own mail server?** We don’t have to reimplement this as you could set up [Mail-in-a-Box](https://mailinabox.email)!
1414
]]></description>
15-
<version>0.19.1</version>
15+
<version>0.20.0</version>
1616
<licence>agpl</licence>
1717
<author>Christoph Wurst</author>
1818
<author>Jan-Christoph Borchardt</author>
@@ -34,10 +34,15 @@
3434
<repair-steps>
3535
<post-migration>
3636
<step>OCA\Mail\Migration\FixCollectedAddresses</step>
37+
<step>OCA\Mail\Migration\MigrateProvisioningConfig</step>
38+
<step>OCA\Mail\Migration\ProvisionAccounts</step>
3739
</post-migration>
3840
</repair-steps>
3941
<commands>
4042
<command>OCA\Mail\Command\CreateAccount</command>
4143
<command>OCA\Mail\Command\ExportAccount</command>
4244
</commands>
45+
<settings>
46+
<admin>OCA\Mail\Settings\AdminSettings</admin>
47+
</settings>
4348
</info>

appinfo/routes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@
113113
'url' => '/proxy',
114114
'verb' => 'GET'
115115
],
116+
[
117+
'name' => 'settings#provisioning',
118+
'url' => '/api/settings/provisioning',
119+
'verb' => 'POST'
120+
],
121+
[
122+
'name' => 'settings#deprovision',
123+
'url' => '/api/settings/provisioning',
124+
'verb' => 'DELETE'
125+
],
116126
],
117127
'resources' => [
118128
'accounts' => ['url' => '/api/accounts'],

doc/admin.md

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,6 @@ Then open the Mail app from the app menu. Put in your mail account credentials a
99

1010
Certain advanced or experimental features need to be specifically enabled in your `config.php`:
1111

12-
### Automatic account creation
13-
14-
In cases where an external user back-end is used for both your Nextcloud and your mail server you may want to have imap accounts set up automatically for your users.
15-
16-
### Available patterns
17-
18-
Two patterns are available to automatically construct credentials:
19-
* `%USERID%`, e.g. `jan`
20-
* `%EMAIL%`, e.g. `[email protected]`
21-
22-
### Minimal configuration
23-
24-
The following minimal configuration will add such an account as soon as the user logs in. The login password is used for the IMAP and SMTP authentication.
25-
26-
Note: Valid values for SSL are `'none'`, `'ssl'` and `'tls'`.
27-
28-
```
29-
'app.mail.accounts.default' => [
30-
'email' => '%USERID%@domain.tld',
31-
'imapHost' => 'imap.domain.tld',
32-
'imapPort' => 993,
33-
'imapSslMode' => 'ssl',
34-
'smtpHost' => 'smtp.domain.tld',
35-
'smtpPort' => 486,
36-
'smtpSslMode' => 'tls',
37-
],
38-
```
39-
40-
### Advanced configuration
41-
42-
In case you have to tweak IMAP and SMTP username, you can do that too.
43-
44-
```
45-
'app.mail.accounts.default' => [
46-
'email' => '%USERID%@domain.tld',
47-
'imapHost' => 'imap.domain.tld',
48-
'imapPort' => 993,
49-
'imapUser' => '%USERID%@domain.tld',
50-
'imapSslMode' => 'ssl',
51-
'smtpHost' => 'smtp.domain.tld',
52-
'smtpPort' => 486,
53-
'smtpUser' => '%USERID%@domain.tld',
54-
'smtpSslMode' => 'tls',
55-
],
56-
```
57-
5812
### Timeouts
5913
Depending on your mail host, it may be necessary to increase your IMAP and/or SMTP timeout threshold. Currently IMAP defaults to 20 seconds and SMTP defaults to 2 seconds. They can be changed as follows:
6014

lib/AppInfo/BootstrapSingleton.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace OCA\Mail\AppInfo;
2525

26+
use OC\Hooks\PublicEmitter;
2627
use OCA\Mail\Contracts\IAttachmentService;
2728
use OCA\Mail\Contracts\IAvatarService;
2829
use OCA\Mail\Contracts\IMailManager;
@@ -34,6 +35,7 @@
3435
use OCA\Mail\Events\MessageSentEvent;
3536
use OCA\Mail\Events\SaveDraftEvent;
3637
use OCA\Mail\Http\Middleware\ErrorMiddleware;
38+
use OCA\Mail\Http\Middleware\ProvisioningMiddleware;
3739
use OCA\Mail\Listener\AddressCollectionListener;
3840
use OCA\Mail\Listener\DeleteDraftListener;
3941
use OCA\Mail\Listener\DraftMailboxCreatorListener;
@@ -50,7 +52,8 @@
5052
use OCA\Mail\Service\UserPreferenceSevice;
5153
use OCP\AppFramework\IAppContainer;
5254
use OCP\EventDispatcher\IEventDispatcher;
53-
use OCP\IContainer;
55+
use OCP\IUser;
56+
use OCP\IUserManager;
5457
use OCP\Util;
5558

5659
class BootstrapSingleton {
@@ -108,6 +111,7 @@ private function initializeAppContainer(IAppContainer $container) {
108111

109112
$container->registerAlias('ErrorMiddleware', ErrorMiddleware::class);
110113
$container->registerMiddleWare('ErrorMiddleware');
114+
$container->registerMiddleWare(ProvisioningMiddleware::class);
111115

112116
$container->registerAlias(IGroupService::class, NextcloudGroupService::class);
113117
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @copyright 2019 Christoph Wurst <[email protected]>
5+
*
6+
* @author 2019 Christoph Wurst <[email protected]>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
24+
namespace OCA\Mail\Controller;
25+
26+
use OCA\Mail\AppInfo\Application;
27+
use OCA\Mail\Service\Provisioning\Manager as ProvisioningManager;
28+
use OCP\AppFramework\Controller;
29+
use OCP\AppFramework\Http\JSONResponse;
30+
use OCP\IRequest;
31+
32+
class SettingsController extends Controller {
33+
34+
/** @var ProvisioningManager */
35+
private $provisioningManager;
36+
37+
public function __construct(IRequest $request,
38+
ProvisioningManager $provisioningManager) {
39+
parent::__construct(Application::APP_ID, $request);
40+
$this->provisioningManager = $provisioningManager;
41+
}
42+
43+
public function provisioning(string $emailTemplate,
44+
string $imapUser,
45+
string $imapHost,
46+
int $imapPort,
47+
string $imapSslMode,
48+
string $smtpUser,
49+
string $smtpHost,
50+
int $smtpPort,
51+
string $smtpSslMode): JSONResponse {
52+
$this->provisioningManager->newProvisioning(
53+
$emailTemplate,
54+
$imapUser,
55+
$imapHost,
56+
$imapPort,
57+
$imapSslMode,
58+
$smtpUser,
59+
$smtpHost,
60+
$smtpPort,
61+
$smtpSslMode
62+
);
63+
64+
return new JSONResponse(null);
65+
}
66+
67+
public function deprovision(): JSONResponse {
68+
$this->provisioningManager->deprovision();
69+
70+
return new JSONResponse(null);
71+
}
72+
73+
}

lib/Db/MailAccount.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @method void setInboundSslMode(string $inboundSslMode)
4646
* @method string getInboundUser()
4747
* @method void setInboundUser(string $inboundUser)
48-
* @method string getInboundPassword()
48+
* @method string|null getInboundPassword()
4949
* @method void setInboundPassword(string $inboundPassword)
5050
* @method string getOutboundHost()
5151
* @method void setOutboundHost(string $outboundHost)
@@ -55,14 +55,16 @@
5555
* @method void setOutboundSslMode(string $outboundSslMode)
5656
* @method string getOutboundUser()
5757
* @method void setOutboundUser(string $outboundUser)
58-
* @method string getOutboundPassword()
58+
* @method string|null getOutboundPassword()
5959
* @method void setOutboundPassword(string $outboundPassword)
6060
* @method string|null getSignature()
6161
* @method void setSignature(string|null $signature)
6262
* @method int getLastMailboxSync()
6363
* @method void setLastMailboxSync(int $time)
6464
* @method string getEditorMode()
6565
* @method void setEditorMode(string $editorMode)
66+
* @method bool getProvisioned()
67+
* @method void setProvisioned(bool $provisioned)
6668
*/
6769
class MailAccount extends Entity {
6870

@@ -82,12 +84,12 @@ class MailAccount extends Entity {
8284
protected $signature;
8385
protected $lastMailboxSync;
8486
protected $editorMode;
87+
protected $provisioned;
8588

8689
/**
8790
* @param array $params
8891
*/
8992
public function __construct(array $params=[]) {
90-
9193
if (isset($params['accountId'])) {
9294
$this->setId($params['accountId']);
9395
}
@@ -131,6 +133,7 @@ public function __construct(array $params=[]) {
131133
}
132134

133135
$this->addType('lastMailboxSync', 'integer');
136+
$this->addType('provisioned', 'bool');
134137
}
135138

136139
/**
@@ -147,6 +150,7 @@ public function toJson() {
147150
'imapSslMode' => $this->getInboundSslMode(),
148151
'signature' => $this->getSignature(),
149152
'editorMode' => $this->getEditorMode(),
153+
'provisioned' => $this->getProvisioned(),
150154
];
151155

152156
if (!is_null($this->getOutboundHost())) {

lib/Db/MailAccountMapper.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525

2626
namespace OCA\Mail\Db;
2727

28+
use OCP\AppFramework\Db\DoesNotExistException;
29+
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
2830
use OCP\AppFramework\Db\QBMapper;
31+
use OCP\DB\QueryBuilder\IQueryBuilder;
2932
use OCP\IDBConnection;
33+
use OCP\IUser;
3034

3135
class MailAccountMapper extends QBMapper {
3236

@@ -72,6 +76,24 @@ public function findByUserId(string $userId): array {
7276
return $this->findEntities($query);
7377
}
7478

79+
/**
80+
* @throws DoesNotExistException
81+
* @throws MultipleObjectsReturnedException
82+
*/
83+
public function findProvisionedAccount(IUser $user): MailAccount {
84+
$qb = $this->db->getQueryBuilder();
85+
86+
$query = $qb
87+
->select('*')
88+
->from($this->getTableName())
89+
->where(
90+
$qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())),
91+
$qb->expr()->eq('provisioned', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
92+
);
93+
94+
return $this->findEntity($query);
95+
}
96+
7597
/**
7698
* Saves an User Account into the database
7799
*
@@ -80,12 +102,20 @@ public function findByUserId(string $userId): array {
80102
* @return MailAccount
81103
*/
82104
public function save(MailAccount $account): MailAccount {
83-
if (is_null($account->getId())) {
105+
if ($account->getId() === null) {
84106
return $this->insert($account);
85-
} else {
86-
$this->update($account);
87-
return $account;
88107
}
108+
109+
return $this->update($account);
110+
}
111+
112+
public function deleteProvisionedAccounts(): void {
113+
$qb = $this->db->getQueryBuilder();
114+
115+
$delete = $qb->delete($this->getTableName())
116+
->where($qb->expr()->eq('provisioned', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)));
117+
118+
$delete->execute();
89119
}
90120

91121
}

0 commit comments

Comments
 (0)