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

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

test: add logs to check welcome mail sending behavior

parent 3c39e711
Loading
Loading
Loading
Loading
Loading
+30 −22
Original line number Diff line number Diff line
@@ -28,7 +28,8 @@ use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;

class AccountController extends Controller {
class AccountController extends Controller
{
	protected $appName;
	protected $request;
	private $userService;
@@ -77,7 +78,8 @@ 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());
		}
@@ -107,7 +109,9 @@ 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 {
	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');

		$response = new DataResponse();

@@ -164,7 +168,6 @@ 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]);
@@ -183,6 +186,8 @@ class AccountController extends Controller {
			$response->setStatus(500);
		}

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

		return $response;
	}
	/**
@@ -194,7 +199,8 @@ 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.";
		}
@@ -216,7 +222,8 @@ 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);
@@ -244,7 +251,8 @@ 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']);
@@ -262,7 +270,8 @@ 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)
@@ -277,5 +286,4 @@ class AccountController extends Controller {
		$this->session->remove(CaptchaService::CAPTCHA_RESULT_KEY);
		return $response;
	}

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

class UserService {
class UserService
{
	/** @var IUserManager */
	private $userManager;
	/** @var array */
@@ -43,7 +44,8 @@ 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);
@@ -71,14 +73,16 @@ 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;
@@ -97,24 +101,29 @@ 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);
@@ -122,7 +131,8 @@ 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);
@@ -145,7 +155,8 @@ 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'];

@@ -169,17 +180,22 @@ 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 {
	public function sendWelcomeEmail(string $displayname, string $username, string $userEmail, string $language = 'en'): void
	{

		$this->logger->error('welcome_test at start of sendWelcomeEmail. User: ' . $username);

		$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;
		}

@@ -195,26 +211,37 @@ class UserService {
			$email = $this->createSendGridEmail($fromEmail, $fromName, $username, $displayname, $userEmail, $templateID);
			$this->sendEmailWithSendGrid($email, $sendgridAPIkey);
		} catch (Throwable $e) {
			$this->logger->error('Error sending welcome email to user: ' . $username . ': ' . $e->getMessage());
			$this->logger->error('welcome_test 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();
@@ -228,14 +255,19 @@ 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.
	 *
@@ -249,7 +281,8 @@ 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.");
@@ -266,7 +299,8 @@ 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.');
		}
@@ -291,7 +325,8 @@ 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;
@@ -326,7 +361,8 @@ 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)) {
@@ -346,7 +382,8 @@ 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);

@@ -368,7 +405,8 @@ 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;
	}

@@ -380,7 +418,8 @@ 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'];
@@ -415,7 +454,8 @@ 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'];
@@ -449,7 +489,8 @@ 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.");
@@ -462,7 +503,8 @@ 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'];

@@ -500,7 +542,8 @@ 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'];

@@ -526,19 +569,22 @@ 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;
		}
@@ -557,7 +603,8 @@ class UserService {
		$this->LDAPConnectionService->closeLDAPConnection($conn);
	}

	private function getDefaultQuota() {
	private function getDefaultQuota()
	{
		return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024);
	}
}