diff options
author | GretaD <gretadoci@gmail.com> | 2021-02-11 20:32:08 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-02-26 19:59:39 +0300 |
commit | 5677ea6d1a234060466c34b181bf86f9fd8b8200 (patch) | |
tree | 3edc34eaecfb04ff4917076c34d4ed8a8f57f779 /lib | |
parent | 06842c1ad4fc1b73218d7a49b72343ce6ac92b48 (diff) |
Add trusted domain
Signed-off-by: GretaD <gretadoci@gmail.com>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Contracts/ITrustedSenderService.php | 2 | ||||
-rw-r--r-- | lib/Controller/TrustedSendersController.php | 12 | ||||
-rw-r--r-- | lib/Db/TrustedSender.php | 6 | ||||
-rw-r--r-- | lib/Db/TrustedSenderMapper.php | 23 | ||||
-rw-r--r-- | lib/Migration/Version1090Date20210216154409.php | 34 | ||||
-rw-r--r-- | lib/Service/TrustedSenderService.php | 8 |
6 files changed, 71 insertions, 14 deletions
diff --git a/lib/Contracts/ITrustedSenderService.php b/lib/Contracts/ITrustedSenderService.php index 7f0de8aab..1f25e93d4 100644 --- a/lib/Contracts/ITrustedSenderService.php +++ b/lib/Contracts/ITrustedSenderService.php @@ -30,7 +30,7 @@ use OCA\Mail\Db\TrustedSender; interface ITrustedSenderService { public function isTrusted(string $uid, string $email): bool; - public function trust(string $uid, string $email, ?bool $trust = true); + public function trust(string $uid, string $email, string $type, ?bool $trust = true); /** * @param string $uid diff --git a/lib/Controller/TrustedSendersController.php b/lib/Controller/TrustedSendersController.php index 552101d8d..54c5d930c 100644 --- a/lib/Controller/TrustedSendersController.php +++ b/lib/Controller/TrustedSendersController.php @@ -54,13 +54,14 @@ class TrustedSendersController extends Controller { * @TrapError * * @param string $email - * + * @param string $type * @return JsonResponse */ - public function setTrusted(string $email): JsonResponse { + public function setTrusted(string $email, string $type): JsonResponse { $this->trustedSenderService->trust( $this->uid, - $email + $email, + $type ); return JsonResponse::success(null, Http::STATUS_CREATED); @@ -71,13 +72,14 @@ class TrustedSendersController extends Controller { * @TrapError * * @param string $email - * + * @param string $type * @return JsonResponse */ - public function removeTrust(string $email): JsonResponse { + public function removeTrust(string $email, string $type): JsonResponse { $this->trustedSenderService->trust( $this->uid, $email, + $type, false ); diff --git a/lib/Db/TrustedSender.php b/lib/Db/TrustedSender.php index d02313f7c..3425c056a 100644 --- a/lib/Db/TrustedSender.php +++ b/lib/Db/TrustedSender.php @@ -33,6 +33,8 @@ use OCP\AppFramework\Db\Entity; * @method getEmail(): string * @method setUserId(string $userId): void * @method getUserId(): string + * @method setType(string $type): void + * @method getType(): string */ class TrustedSender extends Entity implements JsonSerializable { @@ -42,11 +44,15 @@ class TrustedSender extends Entity implements JsonSerializable { /** @var string */ protected $userId; + /** @var string */ + protected $type; + public function jsonSerialize() { return [ 'id' => $this->id, 'email' => $this->email, 'uid' => $this->userId, + 'type' => $this->type, ]; } } diff --git a/lib/Db/TrustedSenderMapper.php b/lib/Db/TrustedSenderMapper.php index 5d29f224a..b8b8eeae3 100644 --- a/lib/Db/TrustedSenderMapper.php +++ b/lib/Db/TrustedSenderMapper.php @@ -34,13 +34,24 @@ class TrustedSenderMapper extends QBMapper { } public function exists(string $uid, string $email): bool { + $emailObject = new \Horde_Mail_Rfc822_Address($email); + $host = $emailObject->host; $qb = $this->db->getQueryBuilder(); $select = $qb->select('*') ->from($this->getTableName()) ->where( - $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)), - $qb->expr()->eq('email', $qb->createNamedParameter($email)) + $qb->expr()->orX( + $qb->expr()->andX( + $qb->expr()->eq('email', $qb->createNamedParameter($email)), + $qb->expr()->eq('type', $qb->createNamedParameter('individual')) + ), + $qb->expr()->andX( + $qb->expr()->eq('email', $qb->createNamedParameter($host)), + $qb->expr()->eq('type', $qb->createNamedParameter('domain')) + ) + ), + $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)) ); /** @var TrustedSender[] $rows */ @@ -49,25 +60,27 @@ class TrustedSenderMapper extends QBMapper { return !empty($rows); } - public function create(string $uid, string $email): void { + public function create(string $uid, string $email, string $type): void { $qb = $this->db->getQueryBuilder(); $insert = $qb->insert($this->getTableName()) ->values([ 'user_id' => $qb->createNamedParameter($uid), 'email' => $qb->createNamedParameter($email), + 'type' => $qb->createNamedParameter($type), ]); $insert->execute(); } - public function remove(string $uid, string $email): void { + public function remove(string $uid, string $email, string $type): void { $qb = $this->db->getQueryBuilder(); $delete = $qb->delete($this->getTableName()) ->where( $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)), - $qb->expr()->eq('email', $qb->createNamedParameter($email)) + $qb->expr()->eq('email', $qb->createNamedParameter($email)), + $qb->expr()->eq('type', $qb->createNamedParameter($type)) ); $delete->execute(); diff --git a/lib/Migration/Version1090Date20210216154409.php b/lib/Migration/Version1090Date20210216154409.php new file mode 100644 index 000000000..4a793a142 --- /dev/null +++ b/lib/Migration/Version1090Date20210216154409.php @@ -0,0 +1,34 @@ +<?php + +declare(strict_types=1); + +namespace OCA\Mail\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1090Date20210216154409 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(); + + $table = $schema->getTable('mail_trusted_senders'); + $table->addColumn('type', 'string', [ + 'notnull' => true, + 'default' => 'individual', + ]); + $table->addIndex(['type'], 'mail_trusted_senders_type'); + + return $schema; + } +} diff --git a/lib/Service/TrustedSenderService.php b/lib/Service/TrustedSenderService.php index 8210be686..57cebbfa6 100644 --- a/lib/Service/TrustedSenderService.php +++ b/lib/Service/TrustedSenderService.php @@ -44,7 +44,7 @@ class TrustedSenderService implements ITrustedSenderService { ); } - public function trust(string $uid, string $email, ?bool $trust = true): void { + public function trust(string $uid, string $email, string $type, ?bool $trust = true): void { if ($trust && $this->isTrusted($uid, $email)) { // Nothing to do return; @@ -53,12 +53,14 @@ class TrustedSenderService implements ITrustedSenderService { if ($trust) { $this->mapper->create( $uid, - $email + $email, + $type ); } else { $this->mapper->remove( $uid, - $email + $email, + $type ); } } |