diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 0053096f811985b94df69eaab63be1dd64d48c55..3503419b7da410be48ce10a9ef53bc0b609fd8aa 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)) { @@ -560,7 +563,27 @@ class RecoveryEmailService { $requests[] = $now; $this->cache->set($key, $requests, 2); } - + /** + * Method hasActiveSubscription + * + * @param string $email [explicite description] + * + * @return bool + */ + private function hasActiveSubscription(string $email): bool { + $shopUsers = $this->shopAccountService->getUsers($email); + if (empty($shopUsers)) { + return false; + } + + foreach ($shopUsers as $shopUser) { + if (!empty($shopUser['has_active_subscription'])) { + return true; + } + } + + return false; + } /** * Scans all verified recovery email addresses and returns a list of spam accounts. * @@ -582,35 +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 { - if (!$this->validateRecoveryEmail($recoveryEmail, $userId)) { - $spamAccounts[] = [ - 'userId' => $userId, - 'recoveryEmail' => $recoveryEmail, - ]; - } - } catch ( - BlacklistedEmailException | - InvalidRecoveryEmailException | - SameRecoveryEmailAsEmailException | - RecoveryEmailAlreadyFoundException | - MurenaDomainDisallowedException - $e) { - $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; } }