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

Commit 356e2efd authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

Revert "test: add logs to check welcome mail sending behavior"

This reverts commit d805e7ca.
parent d805e7ca
Loading
Loading
Loading
Loading
+22 −30
Original line number Diff line number Diff line
@@ -28,8 +28,7 @@ use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;

class AccountController extends Controller
{
class AccountController extends Controller {
	protected $appName;
	protected $request;
	private $userService;
@@ -78,8 +77,7 @@ class AccountController extends Controller
	 * @param string $lang Language code (default: 'en')
	 *
	 */
	public function index(string $lang = 'en')
	{
	public function index(string $lang = 'en') {
		if ($this->userSession->isLoggedIn()) {
			return new RedirectResponse($this->urlGenerator->linkToDefaultPageUrl());
		}
@@ -109,9 +107,7 @@ class AccountController extends Controller
	 *
	 * @return \OCP\AppFramework\Http\DataResponse
	 */
	public function create(string $displayname = '', string $recoveryEmail = '', string $username = '', string $password = '', string $language = 'en', bool $newsletterEos = false, bool $newsletterProduct = false): DataResponse
	{
		$this->logger->error('welcome_test start creating account');
	public function create(string $displayname = '', string $recoveryEmail = '', string $username = '', string $password = '', string $language = 'en', bool $newsletterEos = false, bool $newsletterProduct = false): DataResponse {
		
		$response = new DataResponse();
		
@@ -168,6 +164,7 @@ class AccountController extends Controller
			$this->userService->addUsernameToCommonDataStore($username);
			$response->setStatus(200);
			$response->setData(['success' => true]);

		} catch (LDAPUserCreationException | Error $e) {
			$this->logger->logException($e, ['app' => Application::APP_ID]);
			$response->setData(['message' => 'A server-side error occurred while processing your request! Please try again later.', 'success' => false]);
@@ -186,8 +183,6 @@ class AccountController extends Controller
			$response->setStatus(500);
		}

		$this->logger->error('welcome_test done creating account');

		return $response;
	}
	/**
@@ -199,8 +194,7 @@ class AccountController extends Controller
	 *
	 * @return string|null If validation fails, a string describing the error; otherwise, null.
	 */
	public function validateInput(string $inputName, string $value, int $maxLength = null): ?string
	{
	public function validateInput(string $inputName, string $value, int $maxLength = null) : ?string {
		if ($value === '') {
			return "$inputName is required.";
		}
@@ -222,8 +216,7 @@ class AccountController extends Controller
	 *
	 * @return \OCP\AppFramework\Http\DataResponse
	 */
	public function checkUsernameAvailable(string $username): DataResponse
	{
	public function checkUsernameAvailable(string $username) : DataResponse {
		$this->session->remove(self::SESSION_USERNAME_CHECK);
		$response = new DataResponse();
		$response->setStatus(400);
@@ -251,8 +244,7 @@ class AccountController extends Controller
	 * @PublicPage
	 * @NoCSRFRequired
	 */
	public function captcha(): Http\DataDisplayResponse
	{
	public function captcha(): Http\DataDisplayResponse {
		$captchaValue = $this->captchaService->generateCaptcha();

		$response = new Http\DataDisplayResponse($captchaValue, Http::STATUS_OK, ['Content-Type' => 'image/png']);
@@ -270,8 +262,7 @@ class AccountController extends Controller
	 *
	 * @return \OCP\AppFramework\Http\DataResponse
	 */
	public function verifyCaptcha(string $captchaInput = '', string $bypassToken = ''): DataResponse
	{
	public function verifyCaptcha(string $captchaInput = '', string $bypassToken = '') : DataResponse {
		$response = new DataResponse();
		$captchaToken = $this->config->getSystemValue('bypass_captcha_token', '');
		// Initialize the default status to 400 (Bad Request)
@@ -286,4 +277,5 @@ class AccountController extends Controller
		$this->session->remove(CaptchaService::CAPTCHA_RESULT_KEY);
		return $response;
	}

}
+70 −117
Original line number Diff line number Diff line
@@ -23,8 +23,7 @@ use OCP\Util;
use Throwable;
use UnexpectedValueException;

class UserService
{
class UserService {
	/** @var IUserManager */
	private $userManager;
	/** @var array */
@@ -44,8 +43,7 @@ class UserService
	/** @var LDAPConnectionService */
	private $LDAPConnectionService;
	private BlackListService $blackListService;
	public function __construct($appName, IUserManager $userManager, IConfig $config, CurlService $curlService, ILogger $logger, Defaults $defaults, IFactory $l10nFactory, LDAPConnectionService $LDAPConnectionService, 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);
@@ -73,16 +71,14 @@ class UserService
	}


	public function getConfigValue(string $key, mixed $default = false)
	{
	public function getConfigValue(string $key, mixed $default = false) {
		if (!empty($this->appConfig[$key])) {
			return $this->appConfig[$key];
		}
		return $default;
	}

	public function userExists(string $uid): bool
	{
	public function userExists(string $uid): bool {
		$exists = $this->userManager->userExists($uid);
		if ($exists) {
			return $exists;
@@ -101,29 +97,24 @@ class UserService
		return $exists;
	}

	public function getUser(string $uid): ?IUser
	{
	public function getUser(string $uid): ?IUser {
		if($this->userExists($uid)) {
			return $this->userManager->get($uid);
		}
		return null;
	}

	public function setRecoveryEmail(string $uid, string $recoveryEmail): void
	{
	public function setRecoveryEmail(string $uid, string $recoveryEmail): void {
		$this->config->setUserValue($uid, 'email-recovery', 'recovery-email', $recoveryEmail);
	}
	public function setUnverifiedRecoveryEmail(string $uid, string $recoveryEmail): void
	{
	public function setUnverifiedRecoveryEmail(string $uid, string $recoveryEmail): void {
		$this->config->setUserValue($uid, 'email-recovery', 'unverified-recovery-email', $recoveryEmail);
	}
	public function setTOS(string $uid, bool $tosAccepted): void
	{
	public function setTOS(string $uid, bool $tosAccepted): void {
		$this->config->setUserValue($uid, 'terms_of_service', 'tosAccepted', intval($tosAccepted));
	}

	public function getHMEAliasesFromConfig($uid): array
	{
	public function getHMEAliasesFromConfig($uid) : array {
		$aliases = $this->config->getUserValue($uid, 'hide-my-email', 'email-aliases', []);
		if (!empty($aliases)) {
			$aliases = json_decode($aliases, true);
@@ -131,8 +122,7 @@ class UserService
		return $aliases;
	}

	public function addHMEAliasInConfig($uid, $alias): bool
	{
	public function addHMEAliasInConfig($uid, $alias) : bool {
		$aliases = $this->getHMEAliasesFromConfig($uid);
		$aliases[] = $alias;
		$aliases = json_encode($aliases);
@@ -155,8 +145,7 @@ class UserService
	 * @param $welcomeSecret string generated at ecloud selfhosting install and added as a custom var in NC's config
	 * @return mixed response of the external endpoint
	 */
	public function deleteEmailAccount(string $email)
	{
	public function deleteEmailAccount(string $email) {
		$commonServicesURL = $this->apiConfig['commonServicesURL'];
		$commonApiVersion = $this->apiConfig['commonApiVersion'];

@@ -180,22 +169,17 @@ class UserService
			throw new Exception('Error deleting mail folder of' . $email . '. Status Code: '.$this->curl->getLastStatusCode());
		}
	}
	public function sendWelcomeEmail(string $displayname, string $username, string $userEmail, string $language = 'en'): void
	{

		$this->logger->error('welcome_test at start of sendWelcomeEmail. User: ' . $username);
	public function sendWelcomeEmail(string $displayname, string $username, string $userEmail, string $language = 'en') : void {
		
		$sendgridAPIkey = $this->getSendGridAPIKey();
		if (empty($sendgridAPIkey)) {
			$this->logger->warning("sendgrid_api_key is missing or empty.", ['app' => Application::APP_ID]);
			$this->logger->error('welcome_test sendgrid_api_key is empty. User: ' . $username);
			return;
		}
		
		$templateIDs = $this->getSendGridTemplateIDs();
		if (empty($templateIDs)) {
			$this->logger->warning("welcome_sendgrid_template_ids is missing or empty.", ['app' => Application::APP_ID]);
			$this->logger->error('welcome_test sendgrid_template_ids is empty. User: ' . $username);
			return;
		}
		
@@ -211,37 +195,26 @@ class UserService
			$email = $this->createSendGridEmail($fromEmail, $fromName, $username, $displayname, $userEmail, $templateID);
			$this->sendEmailWithSendGrid($email, $sendgridAPIkey);
		} catch (Throwable $e) {
			$this->logger->error('welcome_test Error sending welcome email to user: ' . $username . ': ' . $e->getMessage());
			$this->logger->error('Error sending welcome email to user: ' . $username . ': ' . $e->getMessage());
		}
	}

	private function getSendGridAPIKey(): string
	{
	private function getSendGridAPIKey() : string {
		return $this->config->getSystemValue('sendgrid_api_key', '');
	}

	private function getSendGridTemplateIDs(): array
	{
	private function getSendGridTemplateIDs() : array {
		return $this->config->getSystemValue('welcome_sendgrid_template_ids', '');
	}

	public function getMainDomain(): string
	{
	public function getMainDomain() : string {
		return $this->config->getSystemValue('main_domain', '');
	}

	public function getLegacyDomain(): string
	{
	public function getLegacyDomain() : string {
		return $this->config->getSystemValue('legacy_domain', '');
	}

	public function setUserLanguage(string $username, string $language = 'en')
	{
	public function setUserLanguage(string $username, string $language = 'en') {
		$this->config->setUserValue($username, 'core', 'lang', $language);
	}
	
	private function createSendGridEmail(string $fromEmail, string $fromName, string $username, string $displayname, string $userEmail, string $templateID): \SendGrid\Mail\Mail
	{
	private function createSendGridEmail(string $fromEmail, string $fromName, string $username, string $displayname, string $userEmail, string $templateID) : \SendGrid\Mail\Mail {
		$mainDomain = $this->getMainDomain();

		$email = new \SendGrid\Mail\Mail();
@@ -255,19 +228,14 @@ class UserService
		]);
		return $email;
	}

	private function sendEmailWithSendGrid(\SendGrid\Mail\Mail $email, string $sendgridAPIkey): void
	{
	private function sendEmailWithSendGrid(\SendGrid\Mail\Mail $email, string $sendgridAPIkey): void {
		$sendgrid = new \SendGrid($sendgridAPIkey);
		$response = $sendgrid->send($email, [ CURLOPT_TIMEOUT => 15 ]);

		$this->logger->error('welcome_test sendGrid response: ' . $response->statusCode());

		if ($response->statusCode() < 200 || $response->statusCode() > 299) {
			$this->logger->error("SendGrid API error - Status Code: " . $response->statusCode());
		}
	}

	/**
	 * Register a new user.
	 *
@@ -281,8 +249,7 @@ class UserService
	 * @throws Exception If the username or recovery email is already taken.
	 * @throws LDAPUserCreationException If there is an error adding new entry to LDAP store
	 */
	public function registerUser(string $displayname, string $recoveryEmail, string $username, string $userEmail, string $password): void
	{
	public function registerUser(string $displayname, string $recoveryEmail, string $username, string $userEmail, string $password): void {
		
		if ($this->userExists($username) || $this->isUsernameTaken($username)) {
			throw new Exception("Username '$username' is already taken.");
@@ -299,8 +266,7 @@ class UserService
	 * @throws Exception If the recovery email address has an incorrect format, is already taken, or if the domain is disallowed.
	 * @return void
	 */
	public function validateRecoveryEmail(string $recoveryEmail): void
	{
	public function validateRecoveryEmail(string $recoveryEmail): void {
		if (!$this->isValidEmailFormat($recoveryEmail)) {
			throw new RecoveryEmailValidationException('Recovery email address has an incorrect format.');
		}
@@ -325,8 +291,7 @@ class UserService
	 * @return void
	 * @throws LDAPUserCreationException If there is an error adding new entry to LDAP store
	 */
	private function addNewUserToLDAP(string $displayName, string $username, string $userEmail, string $password): void
	{
	private function addNewUserToLDAP(string $displayName, string $username, string $userEmail, string $password): void {
		$connection = $this->LDAPConnectionService->getLDAPConnection();
		$base = $this->LDAPConnectionService->getLDAPBaseUsers()[0];
		$newUserDN = "username=$username," . $base;
@@ -361,8 +326,7 @@ class UserService
	 *
	 * @return bool True if the recovery email address is available, false otherwise.
	 */
	public function checkRecoveryEmailAvailable(string $recoveryEmail): bool
	{
	public function checkRecoveryEmailAvailable(string $recoveryEmail): bool {
		$recoveryEmail = strtolower($recoveryEmail);
		$users = $this->config->getUsersForUserValue('email-recovery', 'recovery-email', $recoveryEmail);
		if(count($users)) {
@@ -382,8 +346,7 @@ class UserService
	 *
	 * @return bool True if the recovery email address is disallowed, false otherwise.
	 */
	public function isRecoveryEmailDomainDisallowed(string $recoveryEmail): bool
	{
	public function isRecoveryEmailDomainDisallowed(string $recoveryEmail): bool {
		
		$recoveryEmail = strtolower($recoveryEmail);
		
@@ -405,8 +368,7 @@ class UserService
	 *
	 * @return bool True if the recovery email address is valid, false otherwise.
	 */
	public function isValidEmailFormat(string $recoveryEmail): bool
	{
	public function isValidEmailFormat(string $recoveryEmail): bool {
		return filter_var($recoveryEmail, FILTER_VALIDATE_EMAIL) !== false;
	}

@@ -418,8 +380,7 @@ class UserService
	 *
	 * @return void
	 */
	public function createHMEAlias(string $username, string $resultmail): void
	{
	public function createHMEAlias(string $username, string $resultmail): void {
		$commonServicesURL = $this->apiConfig['commonServicesURL'];
		$aliasDomain = $this->apiConfig['aliasDomain'];
		$token = $this->apiConfig['commonServicesToken'];
@@ -454,8 +415,7 @@ class UserService
	 *
	 * @return mixed The result of the domain alias creation request, decoded from JSON.
	 */
	public function createNewDomainAlias(string $username, string $userEmail): mixed
	{
	public function createNewDomainAlias(string $username, string $userEmail): mixed {
		$commonServicesURL = $this->apiConfig['commonServicesURL'];
		$commonApiVersion = $this->config->getSystemValue('commonApiVersion', '');
		$domain = $this->apiConfig['mainDomain'];
@@ -489,8 +449,7 @@ class UserService
	 *
	 * @return void
	 */
	public function setAccountDataLocally(string $uid, string $mailAddress): void
	{
	public function setAccountDataLocally(string $uid, string $mailAddress): void {
		$user = $this->getUser($uid);
		if (is_null($user)) {
			throw new Exception("User with username '$uid' not found.");
@@ -503,8 +462,7 @@ class UserService
		$user->setQuota($quota);
	}

	public function isUsernameTaken(string $username): bool
	{
	public function isUsernameTaken(string $username) : bool {
		$commonServicesURL = $this->apiConfig['commonServicesURL'];
		$commonApiVersion = $this->apiConfig['commonApiVersion'];

@@ -542,8 +500,7 @@ class UserService
	 *
	 * @throws AddUsernameToCommonStoreException If an error occurs while adding the username to the common data store.
	 */
	public function addUsernameToCommonDataStore(string $username): void
	{
	public function addUsernameToCommonDataStore(string $username) : void {
		$commonServicesURL = $this->apiConfig['commonServicesURL'];
		$commonApiVersion = $this->apiConfig['commonApiVersion'];

@@ -569,22 +526,19 @@ class UserService
		}
	}

	public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void
	{
	public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void {
		$userActiveAttributes = $this->getActiveAttributes($isEnabled);
		$this->updateAttributesInLDAP($username, $userActiveAttributes);
	}

	private function getActiveAttributes(bool $isEnabled): array
	{
	private function getActiveAttributes(bool $isEnabled): array {
		return [
			'active' => $isEnabled ? 'TRUE' : 'FALSE',
			'mailActive' => $isEnabled ? 'TRUE' : 'FALSE',
		];
	}

	public function updateAttributesInLDAP(string $username, array $attributes): void
	{
	public function updateAttributesInLDAP(string $username, array $attributes): void {
		if (!$this->LDAPConnectionService->isLDAPEnabled()) {
			return;
		}
@@ -603,8 +557,7 @@ class UserService
		$this->LDAPConnectionService->closeLDAPConnection($conn);
	}
	
	private function getDefaultQuota()
	{
	private function getDefaultQuota() {
		return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024);
	}
}