@@ -41,7 +41,7 @@ public function __construct(IDBConnection $db) {
4141 */
4242 public function find (int $ aliasId , string $ currentUserId ): Alias {
4343 $ qb = $ this ->db ->getQueryBuilder ();
44- $ qb ->select ('aliases.* ' )
44+ $ qb ->select ('aliases.* ' , ' accounts.provisioning_id ' )
4545 ->from ($ this ->getTableName (), 'aliases ' )
4646 ->join ('aliases ' , 'mail_accounts ' , 'accounts ' , $ qb ->expr ()->eq ('aliases.account_id ' , 'accounts.id ' ))
4747 ->where (
@@ -54,6 +54,24 @@ public function find(int $aliasId, string $currentUserId): Alias {
5454 return $ this ->findEntity ($ qb );
5555 }
5656
57+ /**
58+ * @throws DoesNotExistException
59+ */
60+ public function findByAlias (string $ alias , string $ currentUserId ): Alias {
61+ $ qb = $ this ->db ->getQueryBuilder ();
62+ $ qb ->select ('aliases.* ' , 'accounts.provisioning_id ' )
63+ ->from ($ this ->getTableName (), 'aliases ' )
64+ ->join ('aliases ' , 'mail_accounts ' , 'accounts ' , $ qb ->expr ()->eq ('aliases.account_id ' , 'accounts.id ' ))
65+ ->where (
66+ $ qb ->expr ()->andX (
67+ $ qb ->expr ()->eq ('accounts.user_id ' , $ qb ->createNamedParameter ($ currentUserId )),
68+ $ qb ->expr ()->eq ('aliases.alias ' , $ qb ->createNamedParameter ($ alias ))
69+ )
70+ );
71+
72+ return $ this ->findEntity ($ qb );
73+ }
74+
5775 /**
5876 * @param int $accountId
5977 * @param string $currentUserId
@@ -62,7 +80,7 @@ public function find(int $aliasId, string $currentUserId): Alias {
6280 */
6381 public function findAll (int $ accountId , string $ currentUserId ): array {
6482 $ qb = $ this ->db ->getQueryBuilder ();
65- $ qb ->select ('aliases.* ' )
83+ $ qb ->select ('aliases.* ' , ' accounts.provisioning_id ' )
6684 ->from ($ this ->getTableName (), 'aliases ' )
6785 ->join ('aliases ' , 'mail_accounts ' , 'accounts ' , $ qb ->expr ()->eq ('aliases.account_id ' , 'accounts.id ' ))
6886 ->where (
@@ -89,6 +107,29 @@ public function deleteAll($accountId) {
89107 $ query ->execute ();
90108 }
91109
110+ /**
111+ * Delete all provisioned aliases for the given uid
112+ *
113+ * Exception for Nextcloud 20: \Doctrine\DBAL\DBALException
114+ * Exception for Nextcloud 21 and newer: \OCP\DB\Exception
115+ *
116+ * @TODO: Change throws to \OCP\DB\Exception once Mail does not support Nextcloud 20.
117+ *
118+ * @throws \Exception
119+ */
120+ public function deleteProvisionedAliasesByUid (string $ uid ): void {
121+ $ qb = $ this ->db ->getQueryBuilder ();
122+
123+ $ qb ->delete ($ this ->getTableName (), 'aliases ' )
124+ ->join ('aliases ' , 'mail_accounts ' , 'accounts ' , 'accounts.id = aliases.account_id ' )
125+ ->where (
126+ $ qb ->expr ()->eq ('accounts.user_id ' , $ qb ->createNamedParameter ($ uid )),
127+ $ qb ->expr ()->isNotNull ('provisioning_id ' )
128+ );
129+
130+ $ qb ->execute ();
131+ }
132+
92133 public function deleteOrphans (): void {
93134 $ qb1 = $ this ->db ->getQueryBuilder ();
94135 $ idsQuery = $ qb1 ->select ('a.id ' )
0 commit comments