Skip to content

Commit

Permalink
Merge pull request #75 from silinternational/develop
Browse files Browse the repository at this point in the history
Ensure alias is created when primary email address is changed
  • Loading branch information
mtompset authored Feb 15, 2024
2 parents 1a09f61 + 713efef commit 4f18590
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
31 changes: 23 additions & 8 deletions SilMock/Google/Service/Directory/UsersResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
*
Expand All @@ -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
);
}
Expand Down
80 changes: 63 additions & 17 deletions SilMock/tests/Google/Service/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 = '[email protected]';
$newEmailAddress = '[email protected]';

$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);
Expand Down

0 comments on commit 4f18590

Please sign in to comment.