diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 8f9c849bdeca3fcaa757f7ecf434261f7be3d9ea..88cb74b6f460960df4d6dcce5d07df10db7c6fb7 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -9,12 +9,14 @@ namespace OCA\EcloudAccounts\Controller; use Exception; use OCA\EcloudAccounts\AppInfo\Application; use OCA\EcloudAccounts\Service\CaptchaService; +use OCA\EcloudAccounts\Service\NewsLetterService; use OCA\EcloudAccounts\Service\UserService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; use OCP\IRequest; use OCP\ISession; use OCP\IURLGenerator; @@ -25,11 +27,14 @@ class AccountController extends Controller { protected $appName; protected $request; private $userService; + private $newsletterService; private $captchaService; protected $l10nFactory; private $session; private $userSession; private $urlGenerator; + /** @var IConfig */ + private IConfig $config; private const SESSION_USERNAME_CHECK = 'username_check_passed'; private const CAPTCHA_VERIFIED_CHECK = 'captcha_verified'; @@ -37,19 +42,23 @@ class AccountController extends Controller { $AppName, IRequest $request, UserService $userService, + NewsLetterService $newsletterService, CaptchaService $captchaService, IFactory $l10nFactory, IUserSession $userSession, IURLGenerator $urlGenerator, - ISession $session + ISession $session, + IConfig $config ) { parent::__construct($AppName, $request); $this->appName = $AppName; $this->userService = $userService; + $this->newsletterService = $newsletterService; $this->captchaService = $captchaService; $this->l10nFactory = $l10nFactory; $this->session = $session; $this->userSession = $userSession; + $this->config = $config; $this->urlGenerator = $urlGenerator; } @@ -86,10 +95,12 @@ class AccountController extends Controller { * @param string $username User's username * @param string $password User's password * @param string $language User's language preference + * @param bool $newsletterEos Users's subscribe to eos newsletter + * @param bool $newsletterProduct Users's subscribe to murena product newsletter * * @return \OCP\AppFramework\Http\DataResponse */ - public function create(string $displayname = '', string $recoveryEmail = '', string $username = '', string $password = '', string $language = ''): DataResponse { + public function create(string $displayname = '', string $recoveryEmail = '', string $username = '', string $password = '', string $language = '', bool $newsletterEos = false, bool $newsletterProduct = false): DataResponse { $response = new DataResponse(); @@ -125,12 +136,12 @@ class AccountController extends Controller { $userEmail = $username.'@'.$mainDomain; $newUserEntry = $this->userService->registerUser($displayname, $recoveryEmail, $username, $userEmail, $password); - $this->userService->setAccountDataLocally($username, $userEmail, $newUserEntry['quota']); $this->userService->createHMEAlias($username, $userEmail); $this->userService->createNewDomainAlias($username, $userEmail); $this->userService->setTOS($username, true); $this->userService->setUserLanguage($username, $language); + $this->newsletterService->setNewsletterSignup($newsletterEos, $newsletterProduct, $userEmail, $language); if($recoveryEmail !== '') { $this->userService->setUnverifiedRecoveryEmail($username, $recoveryEmail); diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index d3b132d500d7368f85a599708f8c41bbf76d3ca0..9cf95068e15081df7b8bbf374cdd6844ff1c3ccd 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -82,6 +82,13 @@ class CurlService { break; case 'POST': $options[CURLOPT_POST] = true; + $jsonContent = in_array('Content-Type: application/json', $headers); + if ($jsonContent) { + $params = json_encode($params); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception('JSON encoding failed: ' . json_last_error_msg()); + } + } $options[CURLOPT_POSTFIELDS] = $params; break; case 'DELETE': @@ -104,7 +111,6 @@ class CurlService { if ($errno = curl_errno($ch)) { - var_dump($errno); $errorMessage = curl_strerror($errno); throw new Exception("Curl error $errno - $errorMessage"); } diff --git a/lib/Service/NewsLetterService.php b/lib/Service/NewsLetterService.php new file mode 100644 index 0000000000000000000000000000000000000000..110861c68550daeba8e4d2f2be57f36ff84b0846 --- /dev/null +++ b/lib/Service/NewsLetterService.php @@ -0,0 +1,81 @@ +userManager = $userManager; + $this->config = $config; + $this->curl = $curlService; + $this->logger = $logger; + } + + public function setNewsletterSignup(bool $newsletterEos, bool $newsletterProduct, string $userEmail, string $language): void { + try { + if ($newsletterEos || $newsletterProduct) { + $listIds = []; + $newsletterListIds = $this->config->getSystemValue('newsletter_list_ids'); + if ($newsletterEos) { + $listIds[] = $newsletterListIds['eos']; + } + + if ($newsletterProduct) { + $listIds[] = $newsletterListIds['product']; + } + + if (!empty($listIds)) { + + $this->signupForNewsletter($userEmail, $listIds, $language); + + } + } + } catch (Exception $e) { + $this->logger->error('Signup for newsletter failed: ' . $e->getMessage()); + } + } + + private function signupForNewsletter(string $userEmail, array $listIds, string $userLanguage): void { + $newsletterApiUrl = $this->config->getSystemValue('newsletter_base_url', ''); + + if (empty($newsletterApiUrl)) { + return; + } + + $endpoint = '/api/signup'; + $url = $newsletterApiUrl . $endpoint; + + $params = [ + 'email' => $userEmail, + 'list_ids' => $listIds, + 'contact_language' => $userLanguage + ]; + $params_string = json_encode($params); + $headers = [ + 'Content-Type: application/json', + 'Content-Length: ' . strlen($params_string) + ]; + $this->curl->post($url, $params, $headers); + + if ($this->curl->getLastStatusCode() !== 200) { + throw new Exception('Error adding email ' . $userEmail . ' to newsletter app'); + } + } +} diff --git a/src/Signup.vue b/src/Signup.vue index bfb682eae8a03a989a8676fd112ccdba1900fae1..8be5e5cf6e0e991ef80fd126b45f66e8c437062f 100644 --- a/src/Signup.vue +++ b/src/Signup.vue @@ -40,8 +40,8 @@ export default { captchaInput: '', email: '', accepttns: false, - newsletter_eos: false, - newsletter_product: false, + newsletterEos: false, + newsletterProduct: false, selectedLanguage: 'en', }, appName: APPLICATION_NAME, @@ -74,6 +74,8 @@ export default { password: this.formData.password, recoveryEmail: this.formData.email, language: this.formData.selectedLanguage, + newsletterEos: this.formData.newsletterEos, + newsletterProduct: this.formData.newsletterProduct, } this.submitForm(data) } diff --git a/src/signup/RegistrationForm.vue b/src/signup/RegistrationForm.vue index ff81f3d7da1263e8ab610bd2a27b94a285f35a5f..5ce865c9c4b6c04e386f527f31b6338cd5237f52 100644 --- a/src/signup/RegistrationForm.vue +++ b/src/signup/RegistrationForm.vue @@ -125,7 +125,7 @@