diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php index b45b089361473d55b270ec416ca5730446afaae3..c50f3c814327f0eaa37819acbb5989daf72246fc 100644 --- a/lib/Listeners/BeforeUserDeletedListener.php +++ b/lib/Listeners/BeforeUserDeletedListener.php @@ -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]); + } + } } diff --git a/lib/Listeners/curl.class.php b/lib/Listeners/curl.class.php index ea82dc00b6b7679503f41a3723c8a5bb89d04e73..0164d9054d2f0b1c6d130ebe6ad92cd89aa26fef 100644 --- a/lib/Listeners/curl.class.php +++ b/lib/Listeners/curl.class.php @@ -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;