Loading lib/Listeners/BeforeUserDeletedListener.php +75 −0 Original line number Diff line number Diff line Loading @@ -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'); } Loading Loading @@ -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); } Loading Loading @@ -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]); } } } lib/Listeners/curl.class.php +11 −1 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) { Loading @@ -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; Loading Loading
lib/Listeners/BeforeUserDeletedListener.php +75 −0 Original line number Diff line number Diff line Loading @@ -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'); } Loading Loading @@ -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); } Loading Loading @@ -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]); } } }
lib/Listeners/curl.class.php +11 −1 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) { Loading @@ -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; Loading