From 2768ddcf213639df1e7883b0a4dd5a6d5e0a7f70 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 14 Feb 2024 14:45:45 -0500 Subject: [PATCH 1/2] Update the UsersResource to add aliases if primaryEmail changes --- .../Service/Directory/UsersResource.php | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/SilMock/Google/Service/Directory/UsersResource.php b/SilMock/Google/Service/Directory/UsersResource.php index a19b425..7e56607 100644 --- a/SilMock/Google/Service/Directory/UsersResource.php +++ b/SilMock/Google/Service/Directory/UsersResource.php @@ -229,13 +229,26 @@ public function update($userKey, $postBody) 201407101130 ); } + $dbUserProps = json_decode($userEntry['data'], true); + if ($this->keyType($userKey) === 'id') { + $oldEmailAddress = $dbUserProps['primaryEmail']; + } else { + $oldEmailAddress = ''; + } /* * only keep the non-null properties of the $postBody user, * except for suspensionReason. */ - $dbUserProps = json_decode($userEntry['data'], true); $newUserProps = get_object_vars($postBody); + if (! empty($oldEmailAddress) && $oldEmailAddress !== $postBody['primaryEmail']) { + $aliases = $newUserProps['aliases']; + $aliases[] = $oldEmailAddress; + $newUserProps['aliases'] = $aliases; + $aliases = $postBody['aliases']; + $aliases[] = $oldEmailAddress; + $postBody['aliases'] = $aliases; + } foreach ($newUserProps as $key => $value) { if ($value !== null || $key === "suspensionReason") { @@ -277,6 +290,14 @@ public function update($userKey, $postBody) return $this->get($userKey); } + private function keyType(string $userKey) + { + $keyType = 'primaryEmail'; + if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { + $keyType = 'id'; + } + return $keyType; + } /** * Retrieves a user record from the database (users.delete) * @@ -285,17 +306,11 @@ public function update($userKey, $postBody) */ private function getDbUser(string $userKey) { - - $key = 'primaryEmail'; - if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { - $key = 'id'; - } - $sqliteUtils = new SqliteUtils($this->dbFile); return $sqliteUtils->getRecordByDataKey( $this->dataType, $this->dataClass, - $key, + $this->keyType($userKey), $userKey ); } From 4ae42af94969677c3525d0bd43d336b30041a5d9 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 14 Feb 2024 14:46:23 -0500 Subject: [PATCH 2/2] Add PHPUnit tests --- .../tests/Google/Service/DirectoryTest.php | 80 +++++++++++++++---- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/SilMock/tests/Google/Service/DirectoryTest.php b/SilMock/tests/Google/Service/DirectoryTest.php index df20412..e8295c4 100644 --- a/SilMock/tests/Google/Service/DirectoryTest.php +++ b/SilMock/tests/Google/Service/DirectoryTest.php @@ -18,26 +18,28 @@ class DirectoryTest extends TestCase public function getProperties($object, $propKeys = null): array { - if ($propKeys === null) { - $propKeys = [ - "changePasswordAtNextLogin", - "hashFunction", - "id", - "password", - "primaryEmail", - "suspended", - "isEnforcedIn2Sv", - "isEnrolledIn2Sv", - "aliases", - ]; - } - $outArray = []; - foreach ($propKeys as $key) { - $outArray[$key] = $object->$key; + if ($object !== null) { + if ($propKeys === null) { + $propKeys = [ + "changePasswordAtNextLogin", + "hashFunction", + "id", + "password", + "primaryEmail", + "suspended", + "isEnforcedIn2Sv", + "isEnrolledIn2Sv", + "aliases", + ]; + } + + + foreach ($propKeys as $key) { + $outArray[$key] = $object->$key; + } } - return $outArray; } @@ -396,6 +398,50 @@ public function testUsersUpdate_ById() $this->assertEquals($expected, $results, $msg); } + public function testUsersUpdate_ById_ChangeEmail() + { + $fixturesClass = new GoogleFixtures($this->dataFile); + $fixturesClass->removeAllFixtures(); + + $userId = '999991'; + $oldEmailAddress = 'user_test4@sil.org'; + $newEmailAddress = 'user_test4a@sil.org'; + + $userData = [ + "changePasswordAtNextLogin" => false, + "hashFunction" => "SHA-1", + "id" => $userId, + "password" => "testP4ss", + "primaryEmail" => $newEmailAddress, + "suspended" => false, + "isEnforcedIn2Sv" => false, + "isEnrolledIn2Sv" => true, + "aliases" => [], + ]; + + $fixtures = $this->getFixtures(); + $fixturesClass->addFixtures($fixtures); + + $newUser = new Google_Service_Directory_User(); + ObjectUtils::initialize($newUser, $userData); + + $newDir = new Directory('anyclient', $this->dataFile); + $newDir->users->update($userId, $newUser); + $newUser = $newDir->users->get($userId); + + $results = $this->getProperties($newUser); + $expected = $userData; + $expected['aliases'][] = $oldEmailAddress; + $msg = " *** Bad user data returned"; + $this->assertEquals($expected, $results, $msg); + + // Attempt to get the user by the alias, after updating the primary email address + $newUser = $newDir->users->get($oldEmailAddress); + $results = $this->getProperties($newUser); + $msg = " *** Bad user data returned"; + $this->assertEquals($expected, $results, $msg); + } + public function testUsersUpdate_WithAlias() { $fixturesClass = new GoogleFixtures($this->dataFile);