From c48e751c3f6683d46a70e3639c54fd2b59736235 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 24 Jun 2025 15:37:17 +0530 Subject: [PATCH 1/5] get user email for checking subscription --- lib/Service/RecoveryEmailService.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 79247ff..cb0d2f7 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -11,6 +11,7 @@ use OCA\EmailRecovery\Exception\MurenaDomainDisallowedException; use OCA\EmailRecovery\Exception\RecoveryEmailAlreadyFoundException; use OCA\EmailRecovery\Exception\SameRecoveryEmailAsEmailException; use OCA\EmailRecovery\Exception\TooManyVerificationAttemptsException; +use OCA\EcloudAccounts\Service\ShopAccountService; use OCA\EmailRecovery\Db\ConfigMapper; use OCP\Defaults; use OCP\Http\Client\IClientService; @@ -54,8 +55,9 @@ class RecoveryEmailService { private DomainService $domainService; private IL10N $l; private ISession $session; + private ShopAccountService $shopAccountService; - public function __construct(string $appName, ILogger $logger, IConfig $config, ISession $session, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, DomainService $domainService, IL10N $l, ICacheFactory $cacheFactory, IClientService $httpClientService, ConfigMapper $configMapper) { + public function __construct(string $appName, ILogger $logger, IConfig $config, ISession $session, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, DomainService $domainService, IL10N $l, ICacheFactory $cacheFactory, IClientService $httpClientService, ConfigMapper $configMapper, ShopAccountService $shopAccountService) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; @@ -73,6 +75,7 @@ class RecoveryEmailService { $this->cacheFactory = $cacheFactory; // Initialize the cache factory $this->cache = $this->cacheFactory->createDistributed(self::CACHE_KEY); // Initialize the cache $this->configMapper = $configMapper; + $this->shopAccountService = $shopAccountService; $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); if (!empty($commonServiceURL)) { @@ -604,9 +607,11 @@ class RecoveryEmailService { $recoveryEmail = strtolower(trim($entry['configvalue'])); $userId = strtolower(trim($entry['userid'])); if ($recoveryEmail !== '' && $userId !== '') { + $user = $this->userManager->get($userId); + $email = $user->getEMailAddress(); try { if (!$this->validateRecoveryEmail($recoveryEmail, $userId)) { - if ($this->recoveryEmailService->hasActiveSubscription($recoveryEmail)) { + if ($this->hasActiveSubscription($email)) { $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); continue; // skip if subscription active } @@ -619,7 +624,9 @@ class RecoveryEmailService { BlacklistedEmailException | InvalidRecoveryEmailException $e) { - if ($this->recoveryEmailService->hasActiveSubscription($recoveryEmail)) { + $user = $this->userManager->get($userId); + $email = $user->getEMailAddress(); + if ($this->hasActiveSubscription($email)) { $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); continue; // skip if subscription active } -- GitLab From d125858e87b41bc7ef68b6f60af1270812c0c5f2 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 24 Jun 2025 16:15:59 +0530 Subject: [PATCH 2/5] Check for null user before accessing email or checking subscription --- lib/Service/RecoveryEmailService.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index cb0d2f7..ed822d1 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -607,9 +607,12 @@ class RecoveryEmailService { $recoveryEmail = strtolower(trim($entry['configvalue'])); $userId = strtolower(trim($entry['userid'])); if ($recoveryEmail !== '' && $userId !== '') { - $user = $this->userManager->get($userId); - $email = $user->getEMailAddress(); try { + $user = $this->userManager->get($userId); + if ($user === null) { + continue; + } + $email = $user->getEMailAddress(); if (!$this->validateRecoveryEmail($recoveryEmail, $userId)) { if ($this->hasActiveSubscription($email)) { $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); @@ -625,6 +628,11 @@ class RecoveryEmailService { InvalidRecoveryEmailException $e) { $user = $this->userManager->get($userId); + $user = $this->userManager->get($userId); + if ($user === null) { + continue; + } + $email = $user->getEMailAddress(); if ($this->hasActiveSubscription($email)) { $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); -- GitLab From efb2e438f9fead9ed83ecfa944dfdc614a76ed34 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 24 Jun 2025 16:30:21 +0530 Subject: [PATCH 3/5] Check for null user before accessing email or checking subscription --- lib/Service/RecoveryEmailService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index ed822d1..750ee23 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -575,11 +575,13 @@ class RecoveryEmailService { if (empty($shopUsers)) { return false; } + foreach ($shopUsers as $shopUser) { - if ($shopUser['has_active_subscription']) { + if (!empty($shopUser['has_active_subscription'])) { return true; } } + return false; } /** -- GitLab From 5f72baedf64b08f99ce75e81ed026c16f7456460 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 24 Jun 2025 19:10:04 +0530 Subject: [PATCH 4/5] removed duplicate code added for get user --- lib/Service/RecoveryEmailService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 750ee23..011d00b 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -629,7 +629,6 @@ class RecoveryEmailService { BlacklistedEmailException | InvalidRecoveryEmailException $e) { - $user = $this->userManager->get($userId); $user = $this->userManager->get($userId); if ($user === null) { continue; -- GitLab From 4fd72a19fba3deb1b96accde56dc73b402ae691e Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 25 Jun 2025 14:34:24 +0530 Subject: [PATCH 5/5] code refactoring-removed duplicate code --- lib/Service/RecoveryEmailService.php | 64 +++++++++++++--------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 011d00b..3503419 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -605,51 +605,45 @@ class RecoveryEmailService { public function getAllSpamEmails(): array { $verifiedEmails = $this->configMapper->getAllVerifiedRecoveryEmails(); $spamAccounts = []; + foreach ($verifiedEmails as $entry) { $recoveryEmail = strtolower(trim($entry['configvalue'])); $userId = strtolower(trim($entry['userid'])); - if ($recoveryEmail !== '' && $userId !== '') { - try { - $user = $this->userManager->get($userId); - if ($user === null) { - continue; - } - $email = $user->getEMailAddress(); - if (!$this->validateRecoveryEmail($recoveryEmail, $userId)) { - if ($this->hasActiveSubscription($email)) { - $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); - continue; // skip if subscription active - } - $spamAccounts[] = [ - 'userId' => $userId, - 'recoveryEmail' => $recoveryEmail, - ]; - } - } catch ( - BlacklistedEmailException | - InvalidRecoveryEmailException - $e) { - $user = $this->userManager->get($userId); - if ($user === null) { - continue; - } - - $email = $user->getEMailAddress(); - if ($this->hasActiveSubscription($email)) { - $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); - continue; // skip if subscription active - } - $this->logger->info("Validation failed (spam) for $userId <$recoveryEmail>: " . $e->getMessage()); + + if ($recoveryEmail === '' || $userId === '') { + continue; + } + + $user = $this->userManager->get($userId); + if ($user === null) { + continue; + } + + $email = $user->getEMailAddress(); + + if ($this->hasActiveSubscription($email)) { + $this->logger->info("User $userId has an active subscription. Skipping spam flag for <$recoveryEmail>."); + continue; + } + + try { + if (!$this->validateRecoveryEmail($recoveryEmail, $userId)) { $spamAccounts[] = [ 'userId' => $userId, 'recoveryEmail' => $recoveryEmail, ]; - } catch (\Throwable $e) { - // Catch all other exceptions - $this->logger->info("Error while checking $userId <$recoveryEmail>: " . $e->getMessage()); } + } catch (BlacklistedEmailException | InvalidRecoveryEmailException $e) { + $this->logger->info("Validation failed (spam) for $userId <$recoveryEmail>: " . $e->getMessage()); + $spamAccounts[] = [ + 'userId' => $userId, + 'recoveryEmail' => $recoveryEmail, + ]; + } catch (\Throwable $e) { + $this->logger->info("Error while checking $userId <$recoveryEmail>: " . $e->getMessage()); } } + return $spamAccounts; } } -- GitLab