diff --git a/appinfo/routes.php b/appinfo/routes.php index 9999debd79a1882f37b49c3d3da4b6740811780d..aa39f72a61b332638b6b36366653524fc5024983 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -7,6 +7,8 @@ return ['routes' => [ ['name' => 'shop_account#set_shop_email_post_delete', 'url' => '/shop-accounts/set_shop_email_post_delete', 'verb' => 'POST' ], ['name' => 'shop_account#set_shop_delete_preference', 'url' => '/shop-accounts/set_shop_delete_preference', 'verb' => 'POST' ], ['name' => 'shop_account#get_order_info', 'url' => '/shop-accounts/order_info', 'verb' => 'GET'], + ['name' => 'shop_account#get_shop_user', 'url' => '/shop-accounts/user', 'verb' => 'GET'], + ['name' => 'shop_account#check_shop_email_post_delete', 'url' => '/shop-accounts/check_shop_email_post_delete', 'verb' => 'GET'], [ 'name' => 'user#preflighted_cors', 'url' => '/api/{path}', 'verb' => 'OPTIONS', 'requirements' => array('path' => '.+') diff --git a/lib/Controller/ShopAccountController.php b/lib/Controller/ShopAccountController.php index 7a4c7c09333f940a0ea12e9d5d6d4183c0a36301..c62b56c56964118f182e4e8f4eb918409b21eaa1 100644 --- a/lib/Controller/ShopAccountController.php +++ b/lib/Controller/ShopAccountController.php @@ -3,66 +3,66 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Controller; +use Exception; use OCA\EcloudAccounts\Service\ShopAccountService; use OCP\IUserSession; use OCP\IRequest; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; -use OCP\IL10N; class ShopAccountController extends Controller { private $shopAccountService; private $userSession; - private $l10n; - private $shopOrdersUrl; - public function __construct($appName, IRequest $request, IUserSession $userSession, ShopAccountService $shopAccountService, IL10N $l10n) + public function __construct($appName, IRequest $request, IUserSession $userSession, ShopAccountService $shopAccountService) { parent::__construct($appName, $request); $this->shopAccountService = $shopAccountService; $this->userSession = $userSession; - $this->l10n = $l10n; - $this->shopOrdersUrl = getenv("WP_SHOP_URL") . '/my-account/orders'; } /** * @NoAdminRequired */ - - public function setShopEmailPostDelete(string $shopEmailPostDelete) { + public function checkShopEmailPostDelete(string $shopEmailPostDelete) { $user = $this->userSession->getUser(); - $userId = $user->getUID(); $email = $user->getEMailAddress(); $response = new DataResponse(); - $data = ['message' => '']; - - if(!filter_var($shopEmailPostDelete, FILTER_VALIDATE_EMAIL)) { + try { + $this->shopAccountService->validateShopEmailPostDelete($shopEmailPostDelete, $email); + } + catch(Exception $e) { $response->setStatus(400); - $data['message'] = 'Invalid Email Format.'; - $response->setData($data); + $response->setData(['message' => $e->getMessage()]); return $response; } + } + /** + * @NoAdminRequired + */ - if($shopEmailPostDelete === $email) { - $response->setStatus(400); - $data['message'] = 'Murena.com email cannot be same as this account\'s email.'; - $response->setData($data); - return $response; + public function setShopEmailPostDelete(string $shopEmailPostDelete) { + $user = $this->userSession->getUser(); + $userId = $user->getUID(); + $email = $user->getEMailAddress(); + $response = new DataResponse(); + + try { + $this->shopAccountService->validateShopEmailPostDelete($shopEmailPostDelete, $email); } - if($this->shopAccountService->shopEmailExists($shopEmailPostDelete, $email)) { + catch(Exception $e) { $response->setStatus(400); - $data['message'] = 'A Murena.com account already uses this e-mail address.'; - $response->setData($data); + $response->setData(['message' => $e->getMessage()]); return $response; } $this->shopAccountService->setShopEmailPostDeletePreference($userId, $shopEmailPostDelete); } - + /** * @NoAdminRequired */ @@ -76,27 +76,34 @@ class ShopAccountController extends Controller { /** * @NoAdminRequired */ - public function getOrderInfo() { + public function getOrderInfo(int $userId) { + $response = new DataResponse(); + $data = ['count' => 0, 'my_orders_url' => $this->shopAccountService->getShopUrl() . '/my-account/orders']; + $orders = $this->shopAccountService->getOrders($userId); + + if($orders) { + $data['count'] = count($orders); + } + + $response->setData($data); + return $response; + } + + /** + * @NoAdminRequired + */ + public function getShopUser() { $response = new DataResponse(); $user = $this->userSession->getUser(); $email = $user->getEMailAddress(); $shopUser = $this->shopAccountService->getUser($email); - $data = ['count' => 0, 'my_orders_url' => $this->shopOrdersUrl]; - if(!$shopUser) { - $response->setData($data); - return $response; - } - - $orders = $this->shopAccountService->getOrders($shopUser['id']); - if(!$orders) { - $response->setData($data); + if(!$shopUser || !$this->shopAccountService->isUserOIDC($shopUser)) { + $response->setStatus(404); return $response; } - - $data['count'] = count($orders); - $response->setData($data); + $response->setData($shopUser); return $response; } } \ No newline at end of file diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index 859e3ef9faba384db317b9d1f347d1d678115c51..1faebc0db75ab8cbe281bb34ee7e2de13ede701d 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Service; +use Exception; class CurlService { diff --git a/lib/Service/ShopAccountService.php b/lib/Service/ShopAccountService.php index 97288422b5c5ef139926f998107e6cb344d50db4..4595ef572306ad0c1b6dd5dd2c1878ae050397fa 100644 --- a/lib/Service/ShopAccountService.php +++ b/lib/Service/ShopAccountService.php @@ -19,28 +19,45 @@ class ShopAccountService { public function __construct($appName, IConfig $config, CurlService $curlService, ILogger $logger) { - $shopUsername = getenv("WP_SHOP_USERNAME"); - $shopPassword = getenv("WP_SHOP_PASS"); - $shopUrl = getenv("WP_SHOP_URL"); - + $this->config = $config; + $shopUsername = $this->config->getSystemValue('murena_shop_username'); + $shopPassword = $this->config->getSystemValue('murena_shop_password'); + + $this->shopUrl = $this->config->getSystemValue('murena_shop_url'); $this->appName = $appName; - $this->shopUserUrl = $shopUrl . "/wp-json/wp/v2/users"; - $this->shopOrdersUrl = $shopUrl . "/wp-json/wc/v3/orders"; + + $this->shopUserUrl = $this->shopUrl . "/wp-json/wp/v2/users"; + $this->shopOrdersUrl = $this->shopUrl . "/wp-json/wc/v3/orders"; $this->shopCredentials = base64_encode($shopUsername . ":" . $shopPassword); $this->shopReassignUserId = getenv('WP_REASSIGN_USER_ID'); - $this->config = $config; $this->curl = $curlService; $this->logger = $logger; } + public function getShopUrl() { + return $this->shopUrl; + } + public function setShopDeletePreference($userId, bool $delete) { $this->config->setUserValue($userId, $this->appName, 'delete_shop_account', intval($delete)); } - public function shopEmailExists(string $shopEmail, string $ncUserEmail) : bool { + public function shopEmailExists(string $shopEmail) : bool { return !empty($this->getUser($shopEmail)); } + public function validateShopEmailPostDelete(string $shopEmailPostDelete, string $cloudEmail) : void { + if(!filter_var($shopEmailPostDelete, FILTER_VALIDATE_EMAIL)) { + throw new Exception('Invalid Email Format.'); + } + if($shopEmailPostDelete === $cloudEmail) { + throw new Exception('Murena.com email cannot be same as this account\'s email.'); + } + if($this->shopEmailExists($shopEmailPostDelete)) { + throw new Exception('A Murena.com account already uses this e-mail address.'); + } + } + public function setShopEmailPostDeletePreference($userId, string $shopEmailPostDelete) { $this->config->setUserValue($userId, $this->appName, 'shop_email_post_delete', $shopEmailPostDelete); } @@ -52,7 +69,7 @@ class ShopAccountService { public function getShopEmailPostDeletePreference($userId) { $recoveryEmail = $this->config->getUserValue($userId, 'email-recovery', 'recovery-email'); - return $this->config->getUserValue($userId, $this->appName, 'shop_email_post_delete', $recoveryEmail); + return $this->config->getUserValue($userId, $this->appName, 'shop_email_post_delete', $recoveryEmail); } public function getOrders(int $userId): ?array { @@ -86,7 +103,7 @@ class ShopAccountService { return $users[0]; } - public function deleteUser(int $userId) : void { + public function deleteUser(int $userId) : void { $params = [ 'force' => true, 'reassign' => $this->shopReassignUserId @@ -104,8 +121,8 @@ class ShopAccountService { $this->logger->error('Error deleting user at WP with ID ' . $userId); $this->logger->logException($e, ['app' => Application::APP_ID]); } - - } + + } public function updateUserEmail(int $userId, string $email) : void { $updateUrl = $this->shopUserUrl . '/' . strval($userId); @@ -116,7 +133,7 @@ class ShopAccountService { try { $answer = $this->callShopAPI($updateUrl, 'POST', $params); - + if($answer['email'] !== $email) { throw new Exception('Unknown error while updating!'); } @@ -128,7 +145,7 @@ class ShopAccountService { } private function callShopAPI(string $url, string $method, array $data = []) { - + $headers = [ "cache-control: no-cache", "content-type: application/json", diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index 9faa8a1983231343aba817b3dec988adfe1d7502..f981db6895912a90dd225fa5184afbb794fda940 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -1,25 +1,4 @@ - * - * @author Thomas Citharel - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ namespace OCA\EcloudAccounts\Settings; @@ -68,7 +47,7 @@ class Personal implements ISettings { * @since 9.1 */ public function getForm(): TemplateResponse { - + $user = $this->userSession->getUser(); if ($user) { $onlyUser = $this->userManager->countUsers() < 2; @@ -85,7 +64,7 @@ class Personal implements ISettings { $this->initialState->provideInitialState('only_user', $onlyUser); $this->initialState->provideInitialState('only_admin', $onlyAdmin); } - + return new TemplateResponse($this->appName, 'personal'); } @@ -95,11 +74,8 @@ class Personal implements ISettings { * @psalm-return 'drop_account' */ public function getSection(): ?string { - $user = $this->userSession->getUser(); - $shopUser = $this->shopAccountService->getUser($user->getEMailAddress()); $dropAccountEnabled = $this->appManager->isEnabledForUser(self::DROP_ACCOUNT_APP_ID); - - if($dropAccountEnabled && $shopUser && $this->shopAccountService->isUserOIDC($shopUser)) { + if($dropAccountEnabled) { return self::DROP_ACCOUNT_APP_ID; } return null; diff --git a/src/PersonalSettings.vue b/src/PersonalSettings.vue index 9f2a106763d06df5e3e8c11e0364e459ae06b22b..bff823bba9b7b811b512615d98430d63c54e5006 100644 --- a/src/PersonalSettings.vue +++ b/src/PersonalSettings.vue @@ -1,5 +1,5 @@