From 0fc202ac85799ca101886a932707142eeaaaf286 Mon Sep 17 00:00:00 2001 From: Alexander Vassilevski Date: Sun, 21 Feb 2021 18:40:26 -0800 Subject: [PATCH 1/5] added hook for password update --- lib/Config.php | 6 ++-- lib/Hooks/UserHooks.php | 74 ++++++++++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/lib/Config.php b/lib/Config.php index cf19c9d..925fdbf 100644 --- a/lib/Config.php +++ b/lib/Config.php @@ -11,9 +11,11 @@ class Config { private $logger; private $appConfiguration; private $logContext = ['app' => 'ct_auto_mail_hooks']; - public function __construct(ILogger $logger, IConfig $nextCloudConfiguration) { + public function __construct( + ILogger $logger, IConfig $nextCloudConfiguration) { $this->logger = $logger; - $this->appConfiguration = $nextCloudConfiguration->getSystemValue(self::CONFIG_KEY); + $this->appConfiguration = + $nextCloudConfiguration->getSystemValue(self::CONFIG_KEY); } public function getGlesysKeys() { return $this->getConfValue(self::CONFIG_KEY_GLESYS_KEYS); diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php index 2823893..2f3b596 100644 --- a/lib/Hooks/UserHooks.php +++ b/lib/Hooks/UserHooks.php @@ -19,6 +19,8 @@ class UserHooks { '\OC\User', 'postCreateUser',$this->createUserCB()); $this->userManager->listen( '\OC\User', 'preDelete', $this->deleteUserCB()); + $this->userManager->listen( + '\OC\User', 'postSetPassword', $this->updateEmailPasswordCB()); } private function createUserCB() { return function (\OC\User\User $user, string $password) { @@ -33,7 +35,6 @@ class UserHooks { 'email' => $uid . $email_suffix, 'quota' => $quota // in MB ); - $this->logger->warning("newuser:". print_r($newuser,1)); if($this->createEmailAccount($newuser)) { $this->logger->warning( "Automatically created mail account for uid " . $uid @@ -54,17 +55,33 @@ class UserHooks { $email_suffix = $this->config->getEmailAddressSuffix(); $email = $uid . $email_suffix; $user_data = array('email' => $email); - $this->logger->warning("user_data:". print_r($user_data,1)); if($this->deleteEmailAccount($user_data)) $this->logger->warning( "Deleted mail account: " . $email, $this->logContext); else $this->logger->error("Error deleting mail account" . $uid); }; } - private function createImapFolders($newuser) { + private function updateEmailPasswordCB() { + return function ( + \OC\User\User $user, string $password, string $recoverPassword) { + $uid = $user->getUID(); + $email_suffix = $this->config->getEmailAddressSuffix(); + $email = $uid . $email_suffix; + $user_data = array( + 'email' => $email, + 'password' => $password + ); + if($this->updateEmailPassword($user_data)) + $this->logger->warning( + "Updated glesys password for account: " . $email, + $this->logContext); + else $this->logger->error("Error deleting mail account" . $uid); + }; + } + private function createImapFolders($user_data) { $server = 'mail.glesys.se'; - $email = $newuser['email']; - $passw = $newuser['password']; + $email = $user_data['email']; + $passw = $user_data['password']; $folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive'); $mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN); if(!$mbox){ @@ -84,15 +101,13 @@ class UserHooks { \imap_close($mbox); return true; } - private function createEmailAccount($newuser) { + private function createEmailAccount($user_data) { $keys = $this->config->getGlesysKeys(); - $this->logger->debug("glesysKeys:" . $keys); $fields = array( - 'emailaccount' => $newuser['email'], - 'password' => $newuser['password'], - 'quota' => $newuser['quota'] + 'emailaccount' => $user_data['email'], + 'password' => $user_dat['password'], + 'quota' => $user_data['quota'] ); - $this->logger->warning("fields:" . print_r($fields, 1)); $query = http_build_query($fields); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, @@ -110,26 +125,53 @@ class UserHooks { curl_close($ch); $xml = simplexml_load_string($result); $code = $xml->status->code; - $this->logger->warning("xml status text:" . $xml->status->text); if ($code == 200) return $this->createImapFolders($newuser); + else $this->logger->warning( + "Error: non-200 status: " . $xml->status->text); return false; } - private function deleteEmailAccount($user){ + private function deleteEmailAccount($user_data){ $ch = curl_init(); $keys = $this->config->getGlesysKeys(); - $this->logger->debug("glesysKeys:" . $keys); curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user["email"]); + curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_USERPWD, $keys); $result = curl_exec($ch); + if (curl_errno($ch)) { + $message = 'Error: ' . curl_error($ch); + $this->logger->error($message); + return false; + } + curl_close($ch); + return true; + } + private function updateEmailPassword($user_data) { + $keys = $this->config->getGlesysKeys(); + $fields = array( + 'emailaccount' => $user_data['email'], + 'password' => $user_data['password'] + ); + $query = http_build_query($fields); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, + 'https://api.glesys.com/email/editaccount/'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_USERPWD, $keys); + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + $result = curl_exec($ch); if (curl_errno($ch)) { $message = 'Error:' . curl_error($ch); $this->logger->error($message); return false; } curl_close($ch); - return true; + $xml = simplexml_load_string($result); + $code = $xml->status->code; + if ($code != 200) + $this->logger->warning( + "Error: non-200 status: " . $xml->status->text); } } From 2766f1e18b622562dff2020b498c8ef594293ef1 Mon Sep 17 00:00:00 2001 From: Alexander Vassilevski Date: Sun, 21 Feb 2021 18:49:07 -0800 Subject: [PATCH 2/5] fix typo --- lib/Hooks/UserHooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php index 2f3b596..5c250e1 100644 --- a/lib/Hooks/UserHooks.php +++ b/lib/Hooks/UserHooks.php @@ -105,7 +105,7 @@ class UserHooks { $keys = $this->config->getGlesysKeys(); $fields = array( 'emailaccount' => $user_data['email'], - 'password' => $user_dat['password'], + 'password' => $user_data['password'], 'quota' => $user_data['quota'] ); $query = http_build_query($fields); From e098b4e7a376848cfdb18666e37035df6febd8f4 Mon Sep 17 00:00:00 2001 From: Alexander Vassilevski Date: Sun, 21 Feb 2021 18:51:52 -0800 Subject: [PATCH 3/5] fixing variable rename error --- lib/Hooks/UserHooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php index 5c250e1..49bfd88 100644 --- a/lib/Hooks/UserHooks.php +++ b/lib/Hooks/UserHooks.php @@ -125,7 +125,7 @@ class UserHooks { curl_close($ch); $xml = simplexml_load_string($result); $code = $xml->status->code; - if ($code == 200) return $this->createImapFolders($newuser); + if ($code == 200) return $this->createImapFolders($user_data); else $this->logger->warning( "Error: non-200 status: " . $xml->status->text); return false; From 14a6f49d58c2a2b9617c5909bd5157ff07aa6640 Mon Sep 17 00:00:00 2001 From: Alexander Vassilevski Date: Sun, 21 Feb 2021 18:55:23 -0800 Subject: [PATCH 4/5] removing third param; --- lib/Hooks/UserHooks.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php index 49bfd88..8f15d4b 100644 --- a/lib/Hooks/UserHooks.php +++ b/lib/Hooks/UserHooks.php @@ -62,8 +62,7 @@ class UserHooks { }; } private function updateEmailPasswordCB() { - return function ( - \OC\User\User $user, string $password, string $recoverPassword) { + return function (\OC\User\User $user, string $password) { $uid = $user->getUID(); $email_suffix = $this->config->getEmailAddressSuffix(); $email = $uid . $email_suffix; From a7b542ead003be2e64b6ebe294bb032bd84195e7 Mon Sep 17 00:00:00 2001 From: Alexander Vassilevski Date: Sun, 21 Feb 2021 18:58:13 -0800 Subject: [PATCH 5/5] fixing error msgs and ensuring return status; --- lib/Hooks/UserHooks.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php index 8f15d4b..8cb4888 100644 --- a/lib/Hooks/UserHooks.php +++ b/lib/Hooks/UserHooks.php @@ -74,7 +74,7 @@ class UserHooks { $this->logger->warning( "Updated glesys password for account: " . $email, $this->logContext); - else $this->logger->error("Error deleting mail account" . $uid); + else $this->logger->error("Error updating mail password for account" . $uid); }; } private function createImapFolders($user_data) { @@ -169,8 +169,11 @@ class UserHooks { curl_close($ch); $xml = simplexml_load_string($result); $code = $xml->status->code; - if ($code != 200) + if ($code != 200){ $this->logger->warning( "Error: non-200 status: " . $xml->status->text); + return false; + } + return true; } }