From 3a92001dd621b062fdd356b8fd73786115b6846e Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 14 Sep 2022 17:20:07 +0530 Subject: [PATCH 1/7] Add delete logic for wp --- lib/Listeners/UserDeletedListener.php | 67 +++++++++++++++++++++++++++ lib/Listeners/curl.class.php | 6 +++ 2 files changed, 73 insertions(+) diff --git a/lib/Listeners/UserDeletedListener.php b/lib/Listeners/UserDeletedListener.php index 6e63f568..6a7cf842 100644 --- a/lib/Listeners/UserDeletedListener.php +++ b/lib/Listeners/UserDeletedListener.php @@ -27,6 +27,14 @@ class UserDeletedListener 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 . "?rest_route=/wp/v2/users"; + $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword); + } @@ -47,6 +55,9 @@ class UserDeletedListener implements IEventListener $this->config->getSystemValue('e_welcome_secret'), $isUserOnLDAP ); + + $email = $user->getEMailAddress(); + $this->deleteUserAtWP($email); } /** @@ -91,4 +102,60 @@ class UserDeletedListener implements IEventListener return null; } + + + 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 with given email ' . $email); + } + + $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 + ]; + $deleteUrl = $this->wordPressUserUrl . '/' . $user['id']; + $reassignUserId = getenv('WP_REASSIGN_USER_ID'); + try { + $answer = $curl->delete($deleteUrl, ['reassign' => $reassignUserId, 'force' => true], $headers); + $answer = json_decode($answer, true); + + // TODO: handle this answer + } + 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 + ]; + $searchUrl = $this->wordPressUserUrl . "&search=" . $searchTerm; + + try { + $answer = $curl->get($searchUrl, [], $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 ea82dc00..ea11f666 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 @@ -66,6 +70,8 @@ class Curl $options[CURLOPT_POST] = true; $options[CURLOPT_POSTFIELDS] = $params; break; + case 'DELETE': + $options[CURLOPT_CUSTOMREQUEST] = "DELETE"; default: throw new Exception('Unsuported method.'); break; -- GitLab From 50c567d6247190ebb327531266ec7ec6e45ad73c Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 14 Sep 2022 18:52:01 +0530 Subject: [PATCH 2/7] fix delete url --- lib/Listeners/UserDeletedListener.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Listeners/UserDeletedListener.php b/lib/Listeners/UserDeletedListener.php index 6a7cf842..15bb628c 100644 --- a/lib/Listeners/UserDeletedListener.php +++ b/lib/Listeners/UserDeletedListener.php @@ -33,6 +33,7 @@ class UserDeletedListener implements IEventListener $wordPressUrl = getenv("WP_SHOP_URL"); $this->wordPressUserUrl = $wordPressUrl . "?rest_route=/wp/v2/users"; + $this->wordPressUserDeleteUrl = $wordPressUrl . "/wp-json/wp/v2/users"; $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword); } @@ -124,13 +125,15 @@ class UserDeletedListener implements IEventListener "content-type: application/json", "Authorization: Basic " . $this->wordPressCredentials ]; - $deleteUrl = $this->wordPressUserUrl . '/' . $user['id']; + $deleteUrl = $this->wordPressUserDeleteUrl . '/' . $user['id']; $reassignUserId = getenv('WP_REASSIGN_USER_ID'); try { $answer = $curl->delete($deleteUrl, ['reassign' => $reassignUserId, 'force' => true], $headers); $answer = json_decode($answer, true); - // TODO: handle this answer + 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']); -- GitLab From 47fb0d355d890e6d4ff616746937ff900e524302 Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 14 Sep 2022 19:13:20 +0530 Subject: [PATCH 3/7] Add to correct listener --- lib/Listeners/BeforeUserDeletedListener.php | 70 +++++++++++++++++++++ lib/Listeners/UserDeletedListener.php | 70 --------------------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php index b45b0893..5112a686 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 . "?rest_route=/wp/v2/users"; + $this->wordPressUserDeleteUrl = $wordPressUrl . "/wp-json/wp/v2/users"; + $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword); + } @@ -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,62 @@ 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 with given email ' . $email); + } + + $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 + ]; + $deleteUrl = $this->wordPressUserDeleteUrl . '/' . $user['id']; + $reassignUserId = getenv('WP_REASSIGN_USER_ID'); + try { + $answer = $curl->delete($deleteUrl, ['reassign' => $reassignUserId, 'force' => true], $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 + ]; + $searchUrl = $this->wordPressUserUrl . "&search=" . $searchTerm; + + try { + $answer = $curl->get($searchUrl, [], $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/UserDeletedListener.php b/lib/Listeners/UserDeletedListener.php index 15bb628c..6e63f568 100644 --- a/lib/Listeners/UserDeletedListener.php +++ b/lib/Listeners/UserDeletedListener.php @@ -27,15 +27,6 @@ class UserDeletedListener 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 . "?rest_route=/wp/v2/users"; - $this->wordPressUserDeleteUrl = $wordPressUrl . "/wp-json/wp/v2/users"; - $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword); - } @@ -56,9 +47,6 @@ class UserDeletedListener implements IEventListener $this->config->getSystemValue('e_welcome_secret'), $isUserOnLDAP ); - - $email = $user->getEMailAddress(); - $this->deleteUserAtWP($email); } /** @@ -103,62 +91,4 @@ class UserDeletedListener implements IEventListener return null; } - - - 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 with given email ' . $email); - } - - $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 - ]; - $deleteUrl = $this->wordPressUserDeleteUrl . '/' . $user['id']; - $reassignUserId = getenv('WP_REASSIGN_USER_ID'); - try { - $answer = $curl->delete($deleteUrl, ['reassign' => $reassignUserId, 'force' => true], $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 - ]; - $searchUrl = $this->wordPressUserUrl . "&search=" . $searchTerm; - - try { - $answer = $curl->get($searchUrl, [], $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]); - } - } } -- GitLab From 8cf2b2faebc7d7004bebb1b2d88f2e23442259d5 Mon Sep 17 00:00:00 2001 From: Akhil Date: Thu, 15 Sep 2022 15:45:55 +0530 Subject: [PATCH 4/7] Fixes --- lib/Listeners/BeforeUserDeletedListener.php | 15 ++++++++++----- lib/Listeners/curl.class.php | 4 ++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php index 5112a686..c5f69d60 100644 --- a/lib/Listeners/BeforeUserDeletedListener.php +++ b/lib/Listeners/BeforeUserDeletedListener.php @@ -33,8 +33,7 @@ class BeforeUserDeletedListener implements IEventListener $wordPressPassword = getenv("WP_SHOP_PASS"); $wordPressUrl = getenv("WP_SHOP_URL"); - $this->wordPressUserUrl = $wordPressUrl . "?rest_route=/wp/v2/users"; - $this->wordPressUserDeleteUrl = $wordPressUrl . "/wp-json/wp/v2/users"; + $this->wordPressUserUrl = $wordPressUrl . "/wp-json/wp/v2/users"; $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword); } @@ -170,10 +169,16 @@ class BeforeUserDeletedListener implements IEventListener "content-type: application/json", "Authorization: Basic " . $this->wordPressCredentials ]; - $deleteUrl = $this->wordPressUserDeleteUrl . '/' . $user['id']; $reassignUserId = getenv('WP_REASSIGN_USER_ID'); + $params = [ + 'force' => true, + 'reassign' => $reassignUserId + ]; + + $deleteUrl = $this->wordPressUserUrl . '/' . $user['id']; + try { - $answer = $curl->delete($deleteUrl, ['reassign' => $reassignUserId, 'force' => true], $headers); + $answer = $curl->delete($deleteUrl, $params, $headers); $answer = json_decode($answer, true); if(!$answer['deleted']) { @@ -187,7 +192,7 @@ class BeforeUserDeletedListener implements IEventListener } } - private function getUsersFromWP(string $searchTerm): array + private function getUsersFromWP(string $searchTerm): ?array { $curl = new Curl(); $headers = [ diff --git a/lib/Listeners/curl.class.php b/lib/Listeners/curl.class.php index ea11f666..6923b65e 100644 --- a/lib/Listeners/curl.class.php +++ b/lib/Listeners/curl.class.php @@ -72,6 +72,10 @@ class Curl break; case 'DELETE': $options[CURLOPT_CUSTOMREQUEST] = "DELETE"; + if ($params) { + $url = $url . '?' . http_build_query($params); + } + break; default: throw new Exception('Unsuported method.'); break; -- GitLab From 3f69148ec3ef3be6ff040c289dc2d93577c455be Mon Sep 17 00:00:00 2001 From: Akhil Date: Thu, 15 Sep 2022 16:37:12 +0530 Subject: [PATCH 5/7] Fix curl class --- lib/Listeners/BeforeUserDeletedListener.php | 4 ++-- lib/Listeners/curl.class.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php index c5f69d60..aa9bb95e 100644 --- a/lib/Listeners/BeforeUserDeletedListener.php +++ b/lib/Listeners/BeforeUserDeletedListener.php @@ -164,6 +164,7 @@ class BeforeUserDeletedListener implements IEventListener if(!empty($user['openid-connect-generic-last-user-claim'])) { $curl = new Curl(); + $headers = [ "cache-control: no-cache", "content-type: application/json", @@ -174,9 +175,8 @@ class BeforeUserDeletedListener implements IEventListener 'force' => true, 'reassign' => $reassignUserId ]; - $deleteUrl = $this->wordPressUserUrl . '/' . $user['id']; - + try { $answer = $curl->delete($deleteUrl, $params, $headers); $answer = json_decode($answer, true); diff --git a/lib/Listeners/curl.class.php b/lib/Listeners/curl.class.php index 6923b65e..0164d905 100644 --- a/lib/Listeners/curl.class.php +++ b/lib/Listeners/curl.class.php @@ -57,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) { -- GitLab From ea603b52655df955174fc0b8bbd4adc09e219661 Mon Sep 17 00:00:00 2001 From: Akhil Date: Thu, 15 Sep 2022 16:40:09 +0530 Subject: [PATCH 6/7] small cleanup --- lib/Listeners/BeforeUserDeletedListener.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php index aa9bb95e..abe3897f 100644 --- a/lib/Listeners/BeforeUserDeletedListener.php +++ b/lib/Listeners/BeforeUserDeletedListener.php @@ -35,6 +35,7 @@ class BeforeUserDeletedListener implements IEventListener $this->wordPressUserUrl = $wordPressUrl . "/wp-json/wp/v2/users"; $this->wordPressCredentials = base64_encode($wordPressUsername . ":" . $wordPressPassword); + $this->wordPressReassignUserId = getenv('WP_REASSIGN_USER_ID'); } @@ -157,7 +158,8 @@ class BeforeUserDeletedListener implements IEventListener } if(count($users) > 1) { - $this->logger->error('More than one user in WP results with given email ' . $email); + $this->logger->error('More than one user in WP results when deleting user with email ' . $email); + return; } $user = $users[0]; @@ -170,10 +172,9 @@ class BeforeUserDeletedListener implements IEventListener "content-type: application/json", "Authorization: Basic " . $this->wordPressCredentials ]; - $reassignUserId = getenv('WP_REASSIGN_USER_ID'); $params = [ 'force' => true, - 'reassign' => $reassignUserId + 'reassign' => $this->wordPressReassignUserId ]; $deleteUrl = $this->wordPressUserUrl . '/' . $user['id']; -- GitLab From 45a33defa0363dee45cfbf0daa2312d476c6d984 Mon Sep 17 00:00:00 2001 From: Akhil Date: Thu, 15 Sep 2022 17:05:06 +0530 Subject: [PATCH 7/7] Move search params inside array --- lib/Listeners/BeforeUserDeletedListener.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php index abe3897f..c50f3c81 100644 --- a/lib/Listeners/BeforeUserDeletedListener.php +++ b/lib/Listeners/BeforeUserDeletedListener.php @@ -201,10 +201,9 @@ class BeforeUserDeletedListener implements IEventListener "content-type: application/json", "Authorization: Basic " . $this->wordPressCredentials ]; - $searchUrl = $this->wordPressUserUrl . "&search=" . $searchTerm; try { - $answer = $curl->get($searchUrl, [], $headers); + $answer = $curl->get($this->wordPressUserUrl, ['search' => $searchTerm], $headers); return json_decode($answer, true); } catch(Exception $e) { -- GitLab