Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 05e6443f authored by Ronak Patel's avatar Ronak Patel
Browse files

separate call for BlacklistService

parent 2ba85e00
Loading
Loading
Loading
Loading
Loading
+117 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace OCA\EcloudAccounts\Service;

require __DIR__ . '/../../vendor/autoload.php';

use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\ILogger;

class BlackListService {
	private IAppData $appData;
	private ILogger $logger;
	private const BLACKLISTED_DOMAINS_FOLDER_NAME = '/';
	private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json';
	private const BLACKLISTED_DOMAINS_URL = 'https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains.json';

	public function __construct(ILogger $logger, IAppData $appData) {
		$this->appData = $appData;
		$this->logger = $logger;
	}

	/**
	 * Check if an email domain is blacklisted against a JSON list of disposable email domains.
	 *
	 * @param string $email The email address to check.
	 * @return bool True if the email domain is blacklisted, false otherwise.
	 */
	public function isBlacklistedEmail(string $email): bool {
		if (!$this->ensureDocumentsFolder()) {
			return false;
		}
		$blacklistedDomains = $this->getBlacklistedDomainData();
		if (empty($blacklistedDomains)) {
			return false;
		}
		$emailParts = explode('@', $email);
		$emailDomain = strtolower(end($emailParts));
		return in_array($emailDomain, $blacklistedDomains);
	}
	/**
	 * Update the blacklisted domains data by fetching it from a URL and saving it locally.
	 *
	 * @return void
	 */
	public function updateBlacklistedDomains(): void {
		$blacklisted_domain_url = self::BLACKLISTED_DOMAINS_URL;
		$json_data = file_get_contents($blacklisted_domain_url);
		$this->setBlacklistedDomainsData($json_data);
	}
	/**
	 * Store blacklisted domain data in a file within AppData.
	 *
	 * @param string $data The data to be stored in the file.
	 */
	private function setBlacklistedDomainsData(string $data): void {
		$file = $this->getBlacklistedDomainsFilePath();
		$file->putContent($data);
	}
	/**
	 * Retrieve the blacklisted domain file path
	 *
	 */
	private function getBlacklistedDomainsFilePath() {
		$foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME;
		try {
			$currentFolder = $this->appData->getFolder($foldername);
		} catch (NotFoundException $e) {
			$this->logger->error('Folder '.$foldername.' not found!');
			$currentFolder = $this->appData->newFolder($foldername);
		}
		$filename = self::BLACKLISTED_DOMAINS_FILE_NAME;
		if ($currentFolder->fileExists($filename)) {
			return $currentFolder->getFile($filename);
		}
		return $currentFolder->newFile($filename);
	}
	/**
	 * Retrieve the blacklisted domain data.
	 *
	 * @return array The array of blacklisted domains.
	 */
	public function getBlacklistedDomainData(): array {
		$foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME;
		$document = self::BLACKLISTED_DOMAINS_FILE_NAME;
		try {
			$blacklistedDomainsInJson = $this->appData->getFolder($foldername)->getFile((string) $document)->getContent();
			if (empty($blacklistedDomainsInJson)) {
				return [];
			}
			return json_decode($blacklistedDomainsInJson, true);
		} catch (NotFoundException $e) {
			$this->logger->error('Blacklisted domains file '.$document.' not found!');
			return [];
		}
		
	}
	/**
	 * Ensure the specified folder exists within AppData.
	 *
	 */
	private function ensureDocumentsFolder(): bool {
		$foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME;
		try {
			$this->appData->getFolder($foldername);
		} catch (NotFoundException $e) {
			$this->logger->error('Blacklisted domains folder '.$foldername.' not found!');
			return false;
		} catch (\RuntimeException $e) {
			$this->logger->error($e);
			return false;
		}
		return true;
	}
}
+4 −110
Original line number Diff line number Diff line
@@ -12,8 +12,6 @@ use OCA\EcloudAccounts\Exception\AddUsernameToCommonStoreException;
use OCA\EcloudAccounts\Exception\BlacklistedEmailException;
use OCA\EcloudAccounts\Exception\LDAPUserCreationException;
use OCP\Defaults;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser;
@@ -43,12 +41,8 @@ class UserService {
	private $apiConfig;
	/** @var LDAPConnectionService */
	private $LDAPConnectionService;
	private IAppData $appData;
	private const BLACKLISTED_DOMAINS_FOLDER_NAME = '/';
	private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json';
	private const BLACKLISTED_DOMAINS_URL = 'https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains.json';

	public function __construct($appName, IUserManager $userManager, IConfig $config, CurlService $curlService, ILogger $logger, Defaults $defaults, IFactory $l10nFactory, LDAPConnectionService $LDAPConnectionService, IAppData $appData) {
	private BlackListService $blackListService;
	public function __construct($appName, IUserManager $userManager, IConfig $config, CurlService $curlService, ILogger $logger, Defaults $defaults, IFactory $l10nFactory, LDAPConnectionService $LDAPConnectionService, BlackListService $blackListService) {
		$this->userManager = $userManager;
		$this->config = $config;
		$this->appConfig = $this->config->getSystemValue($appName);
@@ -57,7 +51,7 @@ class UserService {
		$this->defaults = $defaults;
		$this->l10nFactory = $l10nFactory;
		$this->LDAPConnectionService = $LDAPConnectionService;
		$this->appData = $appData;
		$this->blackListService = $blackListService;
		$commonServiceURL = $this->config->getSystemValue('common_services_url', '');

		if (!empty($commonServiceURL)) {
@@ -281,35 +275,10 @@ class UserService {
		if ($this->isRecoveryEmailDomainDisallowed($recoveryEmail)) {
			throw new Exception('You cannot set an email address with a Murena domain as recovery email address.');
		}
		if ($this->isBlacklistedEmail($recoveryEmail)) {
		if ($this->blackListService->isBlacklistedEmail($recoveryEmail)) {
			throw new BlacklistedEmailException('The domain of this email address is blacklisted. Please provide another recovery address.');
		}
	}
	/**
	 * Check if an email domain is blacklisted against a JSON list of disposable email domains.
	 *
	 * @param string $email The email address to check.
	 * @return bool True if the email domain is blacklisted, false otherwise.
	 */
	public function isBlacklistedEmail(string $email): bool {
		// Get the blacklisted domains from configuration
		if (!$this->ensureDocumentsFolder()) {
			return false;
		}
		$blacklistedDomains = $this->getBlacklistedDomainData();
		if (empty($blacklistedDomains)) {
			return false;
		}

		// Split the email address into parts using explode
		$emailParts = explode('@', $email);
		
		// Extract the domain part
		$emailDomain = strtolower(end($emailParts));
		
		// Check if the email domain is in the blacklisted domains array
		return in_array($emailDomain, $blacklistedDomains);
	}
	/**
	 * Add a new user to the LDAP directory.
	 *
@@ -590,79 +559,4 @@ class UserService {
	private function getDefaultQuota() {
		return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024);
	}
	/**
	 * Update the blacklisted domains data by fetching it from a URL and saving it locally.
	 *
	 * @return void
	 */
	public function updateBlacklistedDomains(): void {
		$blacklisted_domain_url = self::BLACKLISTED_DOMAINS_URL;
		$json_data = file_get_contents($blacklisted_domain_url);
		$this->setBlacklistedDomainsData($json_data);
	}

	/**
	 * Store blacklisted domain data in a file within AppData.
	 *
	 * @param string $data The data to be stored in the file.
	 */
	private function setBlacklistedDomainsData(string $data): void {
		$file = $this->getBlacklistedDomainsFilePath();
		$file->putContent($data);
	}
	/**
	 * Retrieve the blacklisted domain file path
	 *
	 */
	private function getBlacklistedDomainsFilePath() {
		$foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME;
		try {
			$currentFolder = $this->appData->getFolder($foldername);
		} catch (NotFoundException $e) {
			$this->logger->error('Folder '.$foldername.' not found!');
			$currentFolder = $this->appData->newFolder($foldername);
		}
		$filename = self::BLACKLISTED_DOMAINS_FILE_NAME;
		if ($currentFolder->fileExists($filename)) {
			return $currentFolder->getFile($filename);
		}
		return $currentFolder->newFile($filename);
	}
	/**
	 * Retrieve the blacklisted domain data.
	 *
	 * @return array The array of blacklisted domains.
	 */
	public function getBlacklistedDomainData(): array {
		$foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME;
		$document = self::BLACKLISTED_DOMAINS_FILE_NAME;
		try {
			$blacklistedDomainsInJson = $this->appData->getFolder($foldername)->getFile((string) $document)->getContent();
			if (empty($blacklistedDomainsInJson)) {
				return [];
			}
			return json_decode($blacklistedDomainsInJson, true);
		} catch (NotFoundException $e) {
			$this->logger->error('Blacklisted domains file '.$document.' not found!');
			return [];
		}
		
	}
	/**
	 * Ensure the specified folder exists within AppData.
	 *
	 */
	private function ensureDocumentsFolder(): bool {
		$foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME;
		try {
			$this->appData->getFolder($foldername);
		} catch (NotFoundException $e) {
			$this->logger->error('Blacklisted domains folder '.$foldername.' not found!');
			return false;
		} catch (\RuntimeException $e) {
			$this->logger->error($e);
			return false;
		}
		return true;
	}
}