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

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

Merge branch 'delete-user-at-wp' into 'main'

Add delete logic for wp

See merge request !25
parents 3a99df3c b46c30f6
Loading
Loading
Loading
Loading
Loading
+75 −0
Original line number Diff line number Diff line
@@ -27,6 +27,16 @@ class BeforeUserDeletedListener implements IEventListener
        $this->logger = $logger;
        $this->config = $config;
        $this->LDAPConnectionService = $LDAPConnectionService;


        $wordPressUsername = getenv("WP_SHOP_USERNAME");
        $wordPressPassword = getenv("WP_SHOP_PASS");
        $wordPressUrl = getenv("WP_SHOP_URL");

        $this->wordPressUserUrl = $wordPressUrl . "/wp-json/wp/v2/users";
        $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword);
        $this->wordPressReassignUserId = getenv('WP_REASSIGN_USER_ID');

    }


@@ -59,6 +69,8 @@ class BeforeUserDeletedListener implements IEventListener
        } catch (Exception $e) {
            $this->logger->error('Error deleting aliases for user '. $uid . ' :' . $e->getMessage());
        }

        $this->deleteUserAtWP($email);
    }


@@ -136,4 +148,67 @@ class BeforeUserDeletedListener implements IEventListener

        return $aliasEntries;
    }


    private function deleteUserAtWP(string $email) {
        $users = $this->getUsersFromWP($email);

        if(empty($users)) {
            return;
        }

        if(count($users) > 1) {
            $this->logger->error('More than one user in WP results when deleting user with email ' . $email);
            return;
        }

        $user = $users[0];

        if(!empty($user['openid-connect-generic-last-user-claim'])) {
            $curl = new Curl();
            
            $headers = [
                "cache-control: no-cache",
                "content-type: application/json",
                "Authorization: Basic " . $this->wordPressCredentials
            ];
            $params = [
                'force' => true,
                'reassign' => $this->wordPressReassignUserId
            ];
            $deleteUrl = $this->wordPressUserUrl . '/' . $user['id'];

            try {
                $answer = $curl->delete($deleteUrl, $params, $headers);
                $answer = json_decode($answer, true);

                if(!$answer['deleted']) {
                    throw new Exception("User not deleted at WP ". $user['id'] );
                }
            }
            catch(Exception $e) {
                $this->logger->error('Error deleting user at WP with ID ' . $user['id']);
                $this->logger->logException($e, ['app' => Application::APP_ID]);
            }
        }
    } 

    private function getUsersFromWP(string $searchTerm): ?array
    {
        $curl = new Curl();
        $headers = [
            "cache-control: no-cache",
            "content-type: application/json",
            "Authorization: Basic " . $this->wordPressCredentials
        ];
        
        try {
            $answer = $curl->get($this->wordPressUserUrl, ['search' => $searchTerm], $headers);
            return json_decode($answer, true);
        }
        catch(Exception $e) {
            $this->logger->error('There was an issue querying wordpress for users');
            $this->logger->logException($e, ['app' => Application::APP_ID]);
        }
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -35,6 +35,10 @@ class Curl
        return $this->request('POST', $url, $params, $headers, $userOptions);
    }

    public function delete($url, $params = [], $headers = [], $userOptions = []) {
        return $this->request('DELETE', $url, $params, $headers, $userOptions);
    }


    /**
     * Curl run request
@@ -53,7 +57,7 @@ class Curl
        $method = strtoupper($method);
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => $headers
            CURLOPT_HTTPHEADER => $headers
        );
        array_merge($options, $userOptions);
        switch ($method) {
@@ -66,6 +70,12 @@ class Curl
                $options[CURLOPT_POST] = true;
                $options[CURLOPT_POSTFIELDS] = $params;
                break;
            case 'DELETE':
                $options[CURLOPT_CUSTOMREQUEST] = "DELETE";
                if ($params) {
                    $url = $url . '?' . http_build_query($params);
                }
                break;
            default:
                throw new Exception('Unsuported method.');
                break;