From 8b3e463eff0e8a2a380138a0a88cfbbb75478d53 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 5 Oct 2022 16:08:37 +0530 Subject: [PATCH 01/23] api created --- appinfo/routes.php | 1 + lib/Controller/ShopAccountController.php | 25 +++++++++++++++++++++++- lib/Service/ShopAccountService.php | 12 ++++++------ lib/Settings/Personal.php | 8 +++----- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 9999debd..a51eb60b 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -7,6 +7,7 @@ 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/shop_user', '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 7a4c7c09..06ad7dff 100644 --- a/lib/Controller/ShopAccountController.php +++ b/lib/Controller/ShopAccountController.php @@ -62,7 +62,7 @@ class ShopAccountController extends Controller { $this->shopAccountService->setShopEmailPostDeletePreference($userId, $shopEmailPostDelete); } - + /** * @NoAdminRequired */ @@ -99,4 +99,27 @@ class ShopAccountController extends Controller { $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]; + $data = ['isuseroidc' => false]; + if(!$shopUser) { + $response->setData($data); + return $response; + } + $isUserOIDC=$this->shopAccountService->isUserOIDC($shopUser); + $data['count'] = count($shopUser); + $data['isuseroidc'] = $isUserOIDC; + $response->setData($data); + return $response; + } } \ No newline at end of file diff --git a/lib/Service/ShopAccountService.php b/lib/Service/ShopAccountService.php index 97288422..630e2ca1 100644 --- a/lib/Service/ShopAccountService.php +++ b/lib/Service/ShopAccountService.php @@ -52,7 +52,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 +86,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 +104,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 +116,7 @@ class ShopAccountService { try { $answer = $this->callShopAPI($updateUrl, 'POST', $params); - + if($answer['email'] !== $email) { throw new Exception('Unknown error while updating!'); } @@ -128,7 +128,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 9faa8a19..e3a026e9 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -68,7 +68,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 +85,7 @@ class Personal implements ISettings { $this->initialState->provideInitialState('only_user', $onlyUser); $this->initialState->provideInitialState('only_admin', $onlyAdmin); } - + return new TemplateResponse($this->appName, 'personal'); } @@ -96,10 +96,8 @@ class Personal implements ISettings { */ 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; -- GitLab From 94542bc7fdf1a969cc2970562585a9f30401e7d7 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 5 Oct 2022 17:01:43 +0530 Subject: [PATCH 02/23] api in frontend --- src/PersonalSettings.vue | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/PersonalSettings.vue b/src/PersonalSettings.vue index 9f2a1067..b84157c1 100644 --- a/src/PersonalSettings.vue +++ b/src/PersonalSettings.vue @@ -7,7 +7,7 @@

-
+
0) { + this.shopUser = true + } + } + } catch (e) { + } + + }, enableDeleteAccountEvent() { const elem = document.getElementById('body-settings') const event = new Event('enable-delete-account') -- GitLab From 5ba45179b22876c7b6a1510a639482f2a0a47384 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 5 Oct 2022 17:06:20 +0530 Subject: [PATCH 03/23] api in frontend --- src/PersonalSettings.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PersonalSettings.vue b/src/PersonalSettings.vue index b84157c1..11440ef0 100644 --- a/src/PersonalSettings.vue +++ b/src/PersonalSettings.vue @@ -122,7 +122,7 @@ export default { ) const { status, data } = await Axios.get(url) if (status === 200) { - this.isUseroidc =data.isuseroidc + this.isUseroidc = data.isuseroidc if (data.count > 0) { this.shopUser = true } -- GitLab From 8edbed2eef141bf1abc963df47df515f8dffd804 Mon Sep 17 00:00:00 2001 From: Akhil Date: Thu, 6 Oct 2022 16:53:48 +0530 Subject: [PATCH 04/23] Corrections --- appinfo/routes.php | 1 + lib/Controller/ShopAccountController.php | 63 ++++++++++++++---------- lib/Service/ShopAccountService.php | 19 ++++--- src/PersonalSettings.vue | 32 +++++++----- 4 files changed, 69 insertions(+), 46 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index a51eb60b..1c868639 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -8,6 +8,7 @@ return ['routes' => [ ['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/shop_user', 'verb' => 'GET'], + ['name' => 'shop_account#check_shop_email_post_delete', 'url' => '/shop/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 06ad7dff..e274ece8 100644 --- a/lib/Controller/ShopAccountController.php +++ b/lib/Controller/ShopAccountController.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Controller; +use Exception; use OCA\EcloudAccounts\Service\ShopAccountService; use OCP\IUserSession; use OCP\IRequest; @@ -24,38 +25,53 @@ class ShopAccountController extends Controller { $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->validateShopEmailPostDelete($shopEmailPostDelete, $email); + } + catch(Exception $e) { $response->setStatus(400); - $data['message'] = 'Invalid Email Format.'; - $response->setData($data); + $response->setData(['message' => $e->getMessage()]); return $response; } + } - if($shopEmailPostDelete === $email) { - $response->setStatus(400); - $data['message'] = 'Murena.com email cannot be same as this account\'s email.'; - $response->setData($data); - return $response; + private 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->shopAccountService->shopEmailExists($shopEmailPostDelete)) { + throw new Exception('A Murena.com account already uses this e-mail address.'); + } + } + /** + * @NoAdminRequired + */ + + public function setShopEmailPostDelete(string $shopEmailPostDelete) { + $user = $this->userSession->getUser(); + $userId = $user->getUID(); + $email = $user->getEMailAddress(); + $response = new DataResponse(); + + try { + $this->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; } @@ -81,8 +97,8 @@ class ShopAccountController extends Controller { $user = $this->userSession->getUser(); $email = $user->getEMailAddress(); + $data = ['count' => 0, 'my_orders_url' => $this->shopAccountService->getShopUrl()]; $shopUser = $this->shopAccountService->getUser($email); - $data = ['count' => 0, 'my_orders_url' => $this->shopOrdersUrl]; if(!$shopUser) { $response->setData($data); return $response; @@ -110,16 +126,11 @@ class ShopAccountController extends Controller { $shopUser = $this->shopAccountService->getUser($email); - $data = ['count' => 0]; - $data = ['isuseroidc' => false]; - if(!$shopUser) { - $response->setData($data); + if(!$shopUser || !$this->shopAccountService->isUserOIDC($shopUser)) { + $response->setStatus(404); return $response; } - $isUserOIDC=$this->shopAccountService->isUserOIDC($shopUser); - $data['count'] = count($shopUser); - $data['isuseroidc'] = $isUserOIDC; - $response->setData($data); + $response->setData($shopUser); return $response; } } \ No newline at end of file diff --git a/lib/Service/ShopAccountService.php b/lib/Service/ShopAccountService.php index 630e2ca1..5429273a 100644 --- a/lib/Service/ShopAccountService.php +++ b/lib/Service/ShopAccountService.php @@ -19,13 +19,14 @@ 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"); - + $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; @@ -33,11 +34,15 @@ class ShopAccountService { $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)); } diff --git a/src/PersonalSettings.vue b/src/PersonalSettings.vue index 11440ef0..9e33f9a8 100644 --- a/src/PersonalSettings.vue +++ b/src/PersonalSettings.vue @@ -1,5 +1,5 @@