I'm attempting insert a new user into our org and this user has multiple aliases. Google_Service_Directory_User has a facility to "setAliases". On insert, the account is provisioned properly with all the attributes set correctly, minus the aliases - those aliases just seem to be ignored.
$dirObj = new Google_Service_Directory($client);
$primaryEmail = 'joeschmo99@my.test.domain.com';
$alias1 = 'joetest1@my.test.domain.com';
$alias2 = 'joetest2@my.test.domain.com';
$firstName = 'Joe';
$lastName = 'Schmo99';
$shaPass = sha1($someRandomPass);
$nameObject = new Google_Service_Directory_UserName();
$nameObject->setGivenName($firstName);
$nameObject->setFamilyName($lastName);
$nameObject->setFullName("$firstName $lastName");
$userObject = new Google_Service_Directory_User();
$userObject->setName($nameObject);
$userObject->setPassword($shaPass);
$userObject->setHashFunction('SHA-1');
$userObject->setPrimaryEmail($primaryEmail);
$userObject->setAliases( array( $alias1, $alias2 ));
$results = $dirObj->users->insert($userObject);
print_r($results);
Any suggestions for inserting a new user with aliases?
You are doing almost everything right except for the alias insertion. Let's go over this step by step.
First you create the directory object and then the user object:
$service = new Google_Service_Directory($client);
$userObj = new Google_Service_Directory_User(
array(
"name" => array("givenName" => "Joe", "familyName" => "Schmo99"),
"primaryEmail" => "joeschmo99@my.test.domain.com",
"password" => $shaPass
)
);
Next, you insert the user object in the account and save the returned primary email to a variable:
$newUser = $service->users->insert($userObj);
$createdUser = $newUser->primaryEmail;
Create an array of the user aliases you want to assign:
$userAliases = array("joetest1@my.test.domain.com", "joetest2@my.test.domain.com");
Loop through the array, create a user alias object for each alias email and insert them to the user object:
foreach ($userAliases as $userAlias) {
$newAlias = new Google_Service_Directory_Alias(
array(
"alias" => $userAlias,
)
);
$service->users_aliases->insert($createdUser, $newAlias);
}
Please don't forget to review the official documentation on how to insert aliases for a better and more detailed explanation. I hope this helps! :)