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

Commit cfcdcd94 authored by Akhil's avatar Akhil 🙂
Browse files

Merge branch 'dev/ecloud-account-load-fix' into 'main'

ecloud accounts performance fix

See merge request !29
parents b4f8463f e11ff6f8
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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' => '.+')
+43 −36
Original line number Diff line number Diff line
@@ -3,59 +3,59 @@ 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;
        }

@@ -76,27 +76,34 @@ class ShopAccountController extends Controller {
    /**
     * @NoAdminRequired
     */
    public function getOrderInfo() {
    public function getOrderInfo(int $userId) {
        $response = new DataResponse();
        $user = $this->userSession->getUser();
        $email = $user->getEMailAddress();
        $data = ['count' => 0, 'my_orders_url' => $this->shopAccountService->getShopUrl() . '/my-account/orders'];
        $orders = $this->shopAccountService->getOrders($userId);

        if($orders) {
            $data['count'] = count($orders);
        }

        $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']);
    /**
     * @NoAdminRequired
     */
    public function getShopUser() {
        $response = new DataResponse();
        $user = $this->userSession->getUser();
        $email = $user->getEMailAddress();

        if(!$orders) {
            $response->setData($data);
        $shopUser = $this->shopAccountService->getUser($email);

        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
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
declare(strict_types=1);

namespace OCA\EcloudAccounts\Service;
use Exception;

class CurlService
{
+31 −14
Original line number Diff line number Diff line
@@ -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);
    }
+3 −27
Original line number Diff line number Diff line
<?php
/**
 * @copyright Copyright (c) 2017 Thomas Citharel <nextcloud@tcit.fr>
 *
 * @author Thomas Citharel <nextcloud@tcit.fr>
 *
 * @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 <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\EcloudAccounts\Settings;

@@ -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;
Loading