From e306535edbb3d24e9cdcab1d2c43a8a57eedddfc Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:12:38 +0530 Subject: [PATCH 01/21] Managing emailrestriction on change of recovry email --- lib/Listeners/UserConfigChangedListener.php | 6 + lib/Service/CurlService.php | 143 ++++++++++++++++++++ lib/Service/RecoveryEmailService.php | 49 ++++++- 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 lib/Service/CurlService.php diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 10d845d..713620e 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -34,7 +34,13 @@ class UserConfigChangedListener implements IEventListener { if ($event->getKey() === 'recovery-email') { $user = $event->getUserId(); $newRecoveryEmail = $event->getValue(); + $userEmailAddress = $event->getEmail(); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); + if ($newRecoveryEmail === '') { + $this->recoveryEmailService->handleEmailRestiction($userEmailAddress, 'add'); + }else{ + $this->recoveryEmailService->handleEmailRestiction($userEmailAddress, 'remove'); + } } } } diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php new file mode 100644 index 0000000..79a2351 --- /dev/null +++ b/lib/Service/CurlService.php @@ -0,0 +1,143 @@ +request('GET', $url, $params, $headers, $userOptions); + } + + /** + * POST alis for request method + * + * @param $url + * @param array $params + * @param array $headers + * @param array $userOptions + * @return mixed + */ + public function post($url, $params = array(), $headers = array(), $userOptions = array()) { + return $this->request('POST', $url, $params, $headers, $userOptions); + } + + public function delete($url, $params = [], $headers = [], $userOptions = []) { + return $this->request('DELETE', $url, $params, $headers, $userOptions); + } + + public function put($url, $params = [], $headers = [], $userOptions = []) { + return $this->request('PUT', $url, $params, $headers, $userOptions); + } + + /** + * @return int + */ + + public function getLastStatusCode() : int { + return $this->lastStatusCode; + } + + private function buildPostData($params = [], $headers = []) { + $jsonContent = in_array('Content-Type: application/json', $headers); + if ($jsonContent) { + $params = json_encode($params); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception('JSON encoding failed: ' . json_last_error_msg()); + } + return $params; + } + + $formContent = in_array('Content-Type: application/x-www-form-urlencoded', $headers); + if ($formContent) { + $params = http_build_query($params); + return $params; + } + + return $params; + } + + /** + * Curl run request + * + * @param $method + * @param string $url + * @param array $params + * @param array $headers + * @param array $userOptions + * @return mixed + * @throws Exception + */ + private function request($method, $url, $params = array(), $headers = array(), $userOptions = array()) { + $ch = curl_init(); + $method = strtoupper($method); + $options = array( + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => $headers + ); + foreach ($userOptions as $key => $value) { + $options[$key] = $value; + } + switch ($method) { + case 'GET': + if ($params) { + $url = $url . '?' . http_build_query($params); + } + break; + case 'POST': + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = $this->buildPostData($params, $headers); + break; + case 'DELETE': + $options[CURLOPT_CUSTOMREQUEST] = "DELETE"; + if ($params) { + $url = $url . '?' . http_build_query($params); + } + break; + case 'PUT': + $options[CURLOPT_CUSTOMREQUEST] = "PUT"; + $options[CURLOPT_POSTFIELDS] = $this->buildPostData($params, $headers); + break; + default: + throw new Exception('Unsupported method.'); + break; + } + $options[CURLOPT_URL] = $url; + + curl_setopt_array($ch, $options); + + $response = curl_exec($ch); + + $this->lastStatusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + + + if ($errno = curl_errno($ch)) { + $errorMessage = curl_strerror($errno); + throw new Exception("Curl error $errno - $errorMessage"); + } + + curl_close($ch); + + return $response; + } +} diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 3ed2384..287e3d5 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -22,6 +22,7 @@ use OCP\L10N\IFactory; use OCP\IURLGenerator; use OCP\Defaults; use OCP\Security\VerificationToken\IVerificationToken; +use OCA\EmailRecovery\Service\CurlService; class RecoveryEmailService { private ILogger $logger; @@ -34,9 +35,11 @@ class RecoveryEmailService { private IURLGenerator $urlGenerator; private Defaults $themingDefaults; private IVerificationToken $verificationToken; + private CurlService $curl; + private $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes - public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken) { + public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; @@ -47,6 +50,21 @@ class RecoveryEmailService { $this->urlGenerator = $urlGenerator; $this->themingDefaults = $themingDefaults; $this->verificationToken = $verificationToken; + $this->curl = $curlService; + $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); + + if (!empty($commonServiceURL)) { + $commonServiceURL = rtrim($commonServiceURL, '/') . '/'; + } + $this->apiConfig = [ + 'mainDomain' => $this->config->getSystemValue('main_domain', ''), + 'commonServicesURL' => $commonServiceURL, + 'commonServicesToken' => $this->config->getSystemValue('common_services_token', ''), + 'aliasDomain' => $this->config->getSystemValue('alias_domain', ''), + 'commonApiVersion' => $this->config->getSystemValue('common_api_version', ''), + 'userClusterID' => $this->config->getSystemValue('user_cluster_id', ''), + 'objectClass' => $this->config->getSystemValue('ldap_object_class', []), + ]; } public function setRecoveryEmail(string $username, string $value = '') : void { $this->config->setUserValue($username, $this->appName, 'recovery-email', $value); @@ -235,4 +253,33 @@ class RecoveryEmailService { // Check if the email domain is in the blacklisted domains array return in_array($emailDomain, $blacklistedDomains); } + + public function handleEmailRestiction(string $email, string $actionType = 'add') { + $commonServicesURL = $this->apiConfig['commonServicesURL']; + $commonApiVersion = $this->apiConfig['commonApiVersion']; + + if (!isset($commonServicesURL) || empty($commonServicesURL)) { + return; + } + + $endpoint = $commonApiVersion . '/emails/restricted/' . $email; + $url = $commonServicesURL . $endpoint; // POST /v2/emails/restricted/@email + + $params = []; + + $token = $this->apiConfig['commonServicesToken']; + $headers = [ + "Authorization: Bearer $token" + ]; + if($actionType == 'add'){ + $this->curl->post($url, $params, $headers); + }else{ + $this->curl->delete($url, $params, $headers); + } + + if ($this->curl->getLastStatusCode() !== 200) { + throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); + } + } + } -- GitLab From 322ef1c06c5c99dfedbe054b5211159132c0220e Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:15:04 +0530 Subject: [PATCH 02/21] lint --- lib/Listeners/UserConfigChangedListener.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 713620e..aaf60b2 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -4,11 +4,11 @@ declare(strict_types=1); namespace OCA\EmailRecovery\Listeners; +use OCA\EmailRecovery\Service\RecoveryEmailService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\ILogger; use OCP\User\Events\UserConfigChangedEvent; -use OCA\EmailRecovery\Service\RecoveryEmailService; class UserConfigChangedListener implements IEventListener { private $logger; @@ -38,7 +38,7 @@ class UserConfigChangedListener implements IEventListener { $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); if ($newRecoveryEmail === '') { $this->recoveryEmailService->handleEmailRestiction($userEmailAddress, 'add'); - }else{ + } else { $this->recoveryEmailService->handleEmailRestiction($userEmailAddress, 'remove'); } } -- GitLab From 92073e93123b0943578489a0ad35ebb4401d7322 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:16:35 +0530 Subject: [PATCH 03/21] lint --- lib/Service/RecoveryEmailService.php | 31 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 287e3d5..5a30b89 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -5,24 +5,23 @@ declare(strict_types=1); namespace OCA\EmailRecovery\Service; use Exception; -use OCP\ILogger; -use OCP\IConfig; -use OCP\IUserManager; +use OCA\EcloudAccounts\Service\LDAPConnectionService; +use OCA\EmailRecovery\Exception\BlacklistedEmailException; use OCA\EmailRecovery\Exception\InvalidRecoveryEmailException; -use OCA\EmailRecovery\Exception\SameRecoveryEmailAsEmailException; -use OCA\EmailRecovery\Exception\RecoveryEmailAlreadyFoundException; use OCA\EmailRecovery\Exception\MurenaDomainDisallowedException; -use OCA\EmailRecovery\Exception\BlacklistedEmailException; -use OCA\EcloudAccounts\Service\LDAPConnectionService; -use OCP\Mail\IEMailTemplate; -use OCP\Mail\IMailer; -use OCP\Util; +use OCA\EmailRecovery\Exception\RecoveryEmailAlreadyFoundException; +use OCA\EmailRecovery\Exception\SameRecoveryEmailAsEmailException; +use OCP\Defaults; +use OCP\IConfig; +use OCP\ILogger; +use OCP\IURLGenerator; use OCP\IUser; +use OCP\IUserManager; use OCP\L10N\IFactory; -use OCP\IURLGenerator; -use OCP\Defaults; +use OCP\Mail\IEMailTemplate; +use OCP\Mail\IMailer; use OCP\Security\VerificationToken\IVerificationToken; -use OCA\EmailRecovery\Service\CurlService; +use OCP\Util; class RecoveryEmailService { private ILogger $logger; @@ -271,9 +270,9 @@ class RecoveryEmailService { $headers = [ "Authorization: Bearer $token" ]; - if($actionType == 'add'){ + if($actionType == 'add') { $this->curl->post($url, $params, $headers); - }else{ + } else { $this->curl->delete($url, $params, $headers); } @@ -281,5 +280,5 @@ class RecoveryEmailService { throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); } } - + } -- GitLab From ced89302da42f1b1c06a25b0dbf03919f44cd541 Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 14 Jun 2024 10:56:06 +0000 Subject: [PATCH 04/21] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Akhil --- lib/Service/RecoveryEmailService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 5a30b89..d8f7ff4 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -253,7 +253,7 @@ class RecoveryEmailService { return in_array($emailDomain, $blacklistedDomains); } - public function handleEmailRestiction(string $email, string $actionType = 'add') { + public function handleEmailRestiction(string $email, string $actionType = 'add') : void { $commonServicesURL = $this->apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; -- GitLab From 78426de6705a308bc542d2c68a30c9baf3472ca9 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:29:21 +0530 Subject: [PATCH 05/21] changes --- lib/Service/CurlService.php | 1 - lib/Service/RecoveryEmailService.php | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index 79a2351..8948568 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -14,7 +14,6 @@ namespace OCA\EmailRecovery\Service; use Exception; class CurlService { - private int $lastStatusCode = 0; /** * GET alias for request method diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index d8f7ff4..04973da 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -270,7 +270,7 @@ class RecoveryEmailService { $headers = [ "Authorization: Bearer $token" ]; - if($actionType == 'add') { + if ($actionType == 'add') { $this->curl->post($url, $params, $headers); } else { $this->curl->delete($url, $params, $headers); @@ -280,5 +280,4 @@ class RecoveryEmailService { throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); } } - } -- GitLab From 2f826bab46bc5559cd20d10b1f2d15c564434c86 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:31:51 +0530 Subject: [PATCH 06/21] create new function --- lib/Listeners/UserConfigChangedListener.php | 4 ++-- lib/Service/RecoveryEmailService.php | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index aaf60b2..4ed5be5 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -37,9 +37,9 @@ class UserConfigChangedListener implements IEventListener { $userEmailAddress = $event->getEmail(); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); if ($newRecoveryEmail === '') { - $this->recoveryEmailService->handleEmailRestiction($userEmailAddress, 'add'); + $this->recoveryEmailService->restrictEmail($userEmailAddress); } else { - $this->recoveryEmailService->handleEmailRestiction($userEmailAddress, 'remove'); + $this->recoveryEmailService->unrestrictEmail($userEmailAddress); } } } diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 04973da..6b04aa8 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -253,7 +253,7 @@ class RecoveryEmailService { return in_array($emailDomain, $blacklistedDomains); } - public function handleEmailRestiction(string $email, string $actionType = 'add') : void { + private function handleEmailRestiction(string $email, string $actionType = 'add') : void { $commonServicesURL = $this->apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; @@ -280,4 +280,10 @@ class RecoveryEmailService { throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); } } + public function restrictEmail(string $email) : void { + $this->handleEmailRestiction($email, 'add'); + } + public function unrestrictEmail(string $email) : void { + $this->handleEmailRestiction($email, 'add'); + } } -- GitLab From f60d0d13073b293d5b391760c3b0513781a02ecb Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:32:50 +0530 Subject: [PATCH 07/21] create new function --- lib/Service/RecoveryEmailService.php | 33 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 6b04aa8..f3b4c51 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -253,7 +253,7 @@ class RecoveryEmailService { return in_array($emailDomain, $blacklistedDomains); } - private function handleEmailRestiction(string $email, string $actionType = 'add') : void { + public function restrictEmail(string $email) : void { $commonServicesURL = $this->apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; @@ -270,20 +270,33 @@ class RecoveryEmailService { $headers = [ "Authorization: Bearer $token" ]; - if ($actionType == 'add') { - $this->curl->post($url, $params, $headers); - } else { - $this->curl->delete($url, $params, $headers); - } + $this->curl->post($url, $params, $headers); if ($this->curl->getLastStatusCode() !== 200) { throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); } } - public function restrictEmail(string $email) : void { - $this->handleEmailRestiction($email, 'add'); - } public function unrestrictEmail(string $email) : void { - $this->handleEmailRestiction($email, 'add'); + $commonServicesURL = $this->apiConfig['commonServicesURL']; + $commonApiVersion = $this->apiConfig['commonApiVersion']; + + if (!isset($commonServicesURL) || empty($commonServicesURL)) { + return; + } + + $endpoint = $commonApiVersion . '/emails/restricted/' . $email; + $url = $commonServicesURL . $endpoint; // POST /v2/emails/restricted/@email + + $params = []; + + $token = $this->apiConfig['commonServicesToken']; + $headers = [ + "Authorization: Bearer $token" + ]; + $this->curl->delete($url, $params, $headers); + + if ($this->curl->getLastStatusCode() !== 200) { + throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); + } } } -- GitLab From 669a405a64a390ac8f480717c735df9d333ce31c Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:33:04 +0530 Subject: [PATCH 08/21] create new function --- lib/Service/RecoveryEmailService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index f3b4c51..f0e13b8 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -285,7 +285,7 @@ class RecoveryEmailService { } $endpoint = $commonApiVersion . '/emails/restricted/' . $email; - $url = $commonServicesURL . $endpoint; // POST /v2/emails/restricted/@email + $url = $commonServicesURL . $endpoint; // DELETE /v2/emails/restricted/@email $params = []; -- GitLab From ce703ec63e89278a58e77d7ec15da979a7740513 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:35:31 +0530 Subject: [PATCH 09/21] empty added --- lib/Listeners/UserConfigChangedListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 4ed5be5..6e76a29 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -36,7 +36,7 @@ class UserConfigChangedListener implements IEventListener { $newRecoveryEmail = $event->getValue(); $userEmailAddress = $event->getEmail(); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); - if ($newRecoveryEmail === '') { + if (empty($newRecoveryEmail)) { $this->recoveryEmailService->restrictEmail($userEmailAddress); } else { $this->recoveryEmailService->unrestrictEmail($userEmailAddress); -- GitLab From 6bb4325b3cf2d11e1ac287ec0cad69410871fcaa Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:36:38 +0530 Subject: [PATCH 10/21] empty added --- lib/Service/RecoveryEmailService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index f0e13b8..619596e 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -273,7 +273,7 @@ class RecoveryEmailService { $this->curl->post($url, $params, $headers); if ($this->curl->getLastStatusCode() !== 200) { - throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); + throw new Exception('Error adding email ' . $email . ' into restricted list. Status Code: '.$this->curl->getLastStatusCode()); } } public function unrestrictEmail(string $email) : void { @@ -296,7 +296,7 @@ class RecoveryEmailService { $this->curl->delete($url, $params, $headers); if ($this->curl->getLastStatusCode() !== 200) { - throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode()); + throw new Exception('Error deleting email ' . $email . ' from restricted list. Status Code: '.$this->curl->getLastStatusCode()); } } } -- GitLab From 46bd9d84fb3fcf12f02f669867ef9ea6f7e48d00 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:49:18 +0530 Subject: [PATCH 11/21] changed function to get emailaddress --- lib/Listeners/UserConfigChangedListener.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 6e76a29..2352289 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -9,14 +9,17 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\ILogger; use OCP\User\Events\UserConfigChangedEvent; +use OCP\IUserManager; class UserConfigChangedListener implements IEventListener { private $logger; private $recoveryEmailService; + private $userManager; - public function __construct(ILogger $logger, RecoveryEmailService $recoveryEmailService) { + public function __construct(ILogger $logger, RecoveryEmailService $recoveryEmailService, IUserManager $userManager) { $this->logger = $logger; $this->recoveryEmailService = $recoveryEmailService; + $this->userManager = $userManager; } public function handle(Event $event): void { @@ -34,7 +37,9 @@ class UserConfigChangedListener implements IEventListener { if ($event->getKey() === 'recovery-email') { $user = $event->getUserId(); $newRecoveryEmail = $event->getValue(); - $userEmailAddress = $event->getEmail(); + $user = $this->userManager->get($user); + $userEmailAddress = $user->getEMailAddress(); + $this->logger->info("EmailAddress: ".$userEmailAddress); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); if (empty($newRecoveryEmail)) { $this->recoveryEmailService->restrictEmail($userEmailAddress); -- GitLab From 13527c525e0b48602a1bfbbbc462324343eb1aa8 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:54:40 +0530 Subject: [PATCH 12/21] changed function to get emailaddress --- lib/Listeners/UserConfigChangedListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 2352289..9c1e2f7 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -37,10 +37,10 @@ class UserConfigChangedListener implements IEventListener { if ($event->getKey() === 'recovery-email') { $user = $event->getUserId(); $newRecoveryEmail = $event->getValue(); + $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); $user = $this->userManager->get($user); $userEmailAddress = $user->getEMailAddress(); $this->logger->info("EmailAddress: ".$userEmailAddress); - $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); if (empty($newRecoveryEmail)) { $this->recoveryEmailService->restrictEmail($userEmailAddress); } else { -- GitLab From a98e949fbd0705e5e18f407973cf383ec63f6d7d Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:58:11 +0530 Subject: [PATCH 13/21] changed function to get emailaddress --- lib/Listeners/UserConfigChangedListener.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 9c1e2f7..58fdbad 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -38,9 +38,11 @@ class UserConfigChangedListener implements IEventListener { $user = $event->getUserId(); $newRecoveryEmail = $event->getValue(); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); - $user = $this->userManager->get($user); - $userEmailAddress = $user->getEMailAddress(); - $this->logger->info("EmailAddress: ".$userEmailAddress); + + $this->logger->error("userID: ".$user); + $userData = $this->userManager->get($user); + $userEmailAddress = $userData->getEMailAddress(); + $this->logger->error("EmailAddress: ".$userEmailAddress); if (empty($newRecoveryEmail)) { $this->recoveryEmailService->restrictEmail($userEmailAddress); } else { -- GitLab From 2f7b072bd9d9ac6d3ce316be422648b80cde4dc6 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 16:58:55 +0530 Subject: [PATCH 14/21] changed function to get emailaddress --- lib/Listeners/UserConfigChangedListener.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 58fdbad..c31ae81 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -39,10 +39,8 @@ class UserConfigChangedListener implements IEventListener { $newRecoveryEmail = $event->getValue(); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); - $this->logger->error("userID: ".$user); $userData = $this->userManager->get($user); $userEmailAddress = $userData->getEMailAddress(); - $this->logger->error("EmailAddress: ".$userEmailAddress); if (empty($newRecoveryEmail)) { $this->recoveryEmailService->restrictEmail($userEmailAddress); } else { -- GitLab From de8111dff1bdbd5df30bb69e08ab982fbc70d03a Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 14 Jun 2024 13:46:02 +0000 Subject: [PATCH 15/21] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Akhil --- lib/Service/RecoveryEmailService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 619596e..da22205 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -35,7 +35,7 @@ class RecoveryEmailService { private Defaults $themingDefaults; private IVerificationToken $verificationToken; private CurlService $curl; - private $apiConfig; + private array $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService) { -- GitLab From 3ddfcf3d590b2722e5881aef6cae112275926090 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 19:28:57 +0530 Subject: [PATCH 16/21] removed unncessary var --- lib/Service/RecoveryEmailService.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index da22205..7ffaadd 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -56,13 +56,9 @@ class RecoveryEmailService { $commonServiceURL = rtrim($commonServiceURL, '/') . '/'; } $this->apiConfig = [ - 'mainDomain' => $this->config->getSystemValue('main_domain', ''), 'commonServicesURL' => $commonServiceURL, 'commonServicesToken' => $this->config->getSystemValue('common_services_token', ''), - 'aliasDomain' => $this->config->getSystemValue('alias_domain', ''), - 'commonApiVersion' => $this->config->getSystemValue('common_api_version', ''), - 'userClusterID' => $this->config->getSystemValue('user_cluster_id', ''), - 'objectClass' => $this->config->getSystemValue('ldap_object_class', []), + 'commonApiVersion' => $this->config->getSystemValue('common_api_version', '') ]; } public function setRecoveryEmail(string $username, string $value = '') : void { -- GitLab From d2885ce98bd18c8eee4db4ed2be8bd94cd010d32 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 14 Jun 2024 19:44:24 +0530 Subject: [PATCH 17/21] Refactored code --- lib/Service/RecoveryEmailService.php | 56 ++++++++++++++-------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 7ffaadd..1b01bd6 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -249,50 +249,50 @@ class RecoveryEmailService { return in_array($emailDomain, $blacklistedDomains); } + private function manageEmailRestriction(string $email, string $method, string $url) : void { + $params = []; + + $token = $this->apiConfig['commonServicesToken']; + $headers = [ + "Authorization: Bearer $token" + ]; + + if ($method === 'POST') { + $this->curl->post($url, $params, $headers); + } elseif ($method === 'DELETE') { + $this->curl->delete($url, $params, $headers); + } + + if ($this->curl->getLastStatusCode() !== 200) { + throw new Exception('Error ' . strtolower($method) . 'ing email ' . $email . ' in restricted list. Status Code: ' . $this->curl->getLastStatusCode()); + } + } + public function restrictEmail(string $email) : void { $commonServicesURL = $this->apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; - + if (!isset($commonServicesURL) || empty($commonServicesURL)) { return; } - + $endpoint = $commonApiVersion . '/emails/restricted/' . $email; $url = $commonServicesURL . $endpoint; // POST /v2/emails/restricted/@email - - $params = []; - - $token = $this->apiConfig['commonServicesToken']; - $headers = [ - "Authorization: Bearer $token" - ]; - $this->curl->post($url, $params, $headers); - - if ($this->curl->getLastStatusCode() !== 200) { - throw new Exception('Error adding email ' . $email . ' into restricted list. Status Code: '.$this->curl->getLastStatusCode()); - } + + $this->manageEmailRestriction($email, 'POST', $url); } + public function unrestrictEmail(string $email) : void { $commonServicesURL = $this->apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; - + if (!isset($commonServicesURL) || empty($commonServicesURL)) { return; } - + $endpoint = $commonApiVersion . '/emails/restricted/' . $email; $url = $commonServicesURL . $endpoint; // DELETE /v2/emails/restricted/@email - - $params = []; - - $token = $this->apiConfig['commonServicesToken']; - $headers = [ - "Authorization: Bearer $token" - ]; - $this->curl->delete($url, $params, $headers); - - if ($this->curl->getLastStatusCode() !== 200) { - throw new Exception('Error deleting email ' . $email . ' from restricted list. Status Code: '.$this->curl->getLastStatusCode()); - } + + $this->manageEmailRestriction($email, 'DELETE', $url); } } -- GitLab From 50811b65b0a7b9a4f6b88348ba48805921c41c40 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 17 Jun 2024 17:09:33 +0530 Subject: [PATCH 18/21] added logger --- lib/Listeners/UserConfigChangedListener.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index c31ae81..9d4a092 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -39,13 +39,18 @@ class UserConfigChangedListener implements IEventListener { $newRecoveryEmail = $event->getValue(); $this->recoveryEmailService->updateRecoveryEmailAtLDAPServer($user, $newRecoveryEmail); - $userData = $this->userManager->get($user); - $userEmailAddress = $userData->getEMailAddress(); - if (empty($newRecoveryEmail)) { - $this->recoveryEmailService->restrictEmail($userEmailAddress); - } else { - $this->recoveryEmailService->unrestrictEmail($userEmailAddress); - } + try { + $userData = $this->userManager->get($user); + $userEmailAddress = $userData->getEMailAddress(); + + if (empty($newRecoveryEmail)) { + $this->recoveryEmailService->restrictEmail($userEmailAddress); + } else { + $this->recoveryEmailService->unrestrictEmail($userEmailAddress); + } + } catch (\Exception $e) { + $this->logger->error('Error in managing restricted list: '.$e->getMessage()); + } } } } -- GitLab From 30c8c0fcfccd664ce3634913b2afe8f194fbcc10 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 17 Jun 2024 17:10:05 +0530 Subject: [PATCH 19/21] bump version --- appinfo/info.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index e225c6c..5b8030f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Email Recovery Email Recovery App - 6.0.0 + 6.0.1 agpl MURENA SAS EmailRecovery -- GitLab From 659aecc40897aa35945370323a3ca83b0ec89878 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 17 Jun 2024 17:10:30 +0530 Subject: [PATCH 20/21] lint php --- lib/Listeners/UserConfigChangedListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index 9d4a092..fc78d71 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -50,7 +50,7 @@ class UserConfigChangedListener implements IEventListener { } } catch (\Exception $e) { $this->logger->error('Error in managing restricted list: '.$e->getMessage()); - } + } } } } -- GitLab From 39ab7bc61ed684d66bb49b72708ef82aed12c4ac Mon Sep 17 00:00:00 2001 From: Akhil Date: Mon, 17 Jun 2024 11:52:57 +0000 Subject: [PATCH 21/21] Apply 1 suggestion(s) to 1 file(s) --- lib/Listeners/UserConfigChangedListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Listeners/UserConfigChangedListener.php b/lib/Listeners/UserConfigChangedListener.php index fc78d71..446257b 100644 --- a/lib/Listeners/UserConfigChangedListener.php +++ b/lib/Listeners/UserConfigChangedListener.php @@ -48,7 +48,7 @@ class UserConfigChangedListener implements IEventListener { } else { $this->recoveryEmailService->unrestrictEmail($userEmailAddress); } - } catch (\Exception $e) { + } catch (\Throwable $e) { $this->logger->error('Error in managing restricted list: '.$e->getMessage()); } } -- GitLab