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

Commit 603ffd22 authored by Ronak Patel's avatar Ronak Patel
Browse files

Merge branch 'dev/fix-delete-for-active-subscriber' into 'main'

Throw an exception if shop user has active subscription

See merge request !69
parents 7b98753b c63ab037
Loading
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
<?php

namespace OCA\EcloudAccounts\Exception;

class DeletingUserWithActiveSubscriptionException extends \Exception {
	public function __construct($message = null, $code = 0) {
		parent::__construct($message, $code);
	}
}
+15 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use OCP\User\Events\BeforeUserDeletedEvent;
use OCA\EcloudAccounts\Service\LDAPConnectionService;
use OCA\EcloudAccounts\Service\ShopAccountService;
use OCA\EcloudAccounts\Service\UserService;
use OCA\EcloudAccounts\Exception\DeletingUserWithActiveSubscriptionException;

class BeforeUserDeletedListener implements IEventListener {
	private $logger;
@@ -38,6 +39,10 @@ class BeforeUserDeletedListener implements IEventListener {
		$user = $event->getUser();
		$email = $user->getEMailAddress();
		$uid = $user->getUID();

		// Handle shop accounts first; none of the other things should happen before the active subscription check
		$this->handleShopAccounts($uid, $email);

		$isUserOnLDAP = $this->LDAPConnectionService->isUserOnLDAPBackend($user);

		$this->logger->info("PostDelete user {user}", array('user' => $uid));
@@ -58,12 +63,22 @@ class BeforeUserDeletedListener implements IEventListener {
		} catch (Exception $e) {
			$this->logger->error('Error deleting aliases for user '. $uid . ' :' . $e->getMessage());
		}
	}

	private function handleShopAccounts(string $uid, string $email) {
		$deleteShopAccount = $this->shopAccountService->getShopDeletePreference($uid);
		$shopUsers = $this->shopAccountService->getUsers($email);

		if (!empty($shopUsers)) {
			foreach ($shopUsers as $shopUser) {
				if ($shopUser['has_active_subscription']) {
					throw new DeletingUserWithActiveSubscriptionException('Cannot delete user ' . $uid . ' as an active subscription exists');
				}

				if (!isset($shopUser['id'])) {
					continue;
				}

				if ($deleteShopAccount) {
					$this->shopAccountService->deleteUser($shopUser['shop_url'], $shopUser['id']);
				} else {