/**
  * Creates a user (users.insert) and sets its aliases property if any
  *     are given.
  *
  * @param Google_User $postBody
  * @return null|a real Google_Service_Directory_User instance
  * @throws \Exception with code 201407101120, if the user already exists
  */
 public function insert($postBody)
 {
     $defaults = array('id' => intval(str_replace(array(' ', '.'), '', microtime())), 'suspended' => false, 'changePasswordAtNextLogin' => false, 'isAdmin' => false, 'isDelegatedAdmin' => false, 'lastLoginTime' => time(), 'creationTime' => time());
     // array_merge will not work, since $postBody is an object which only
     // implements ArrayAccess
     foreach ($defaults as $key => $value) {
         if (!isset($postBody[$key])) {
             $postBody[$key] = $value;
         }
     }
     $currentUser = $this->get($postBody->primaryEmail);
     if ($currentUser) {
         throw new \Exception("Account already exists: " . $postBody['primaryEmail'], 201407101120);
     }
     $newUser = new \Google_Service_Directory_User();
     ObjectUtils::initialize($newUser, $postBody);
     $userData = json_encode($newUser);
     // record the user in the database
     $sqliteUtils = new SqliteUtils($this->_dbFile);
     $sqliteUtils->recordData($this->_dataType, $this->_dataClass, $userData);
     // record the user's aliases in the database
     if ($postBody->aliases) {
         $usersAliases = new UsersAliasesResource($this->_dbFile);
         foreach ($postBody->aliases as $alias) {
             $newAlias = new Alias();
             $newAlias->alias = $alias;
             $newAlias->kind = "personal";
             $newAlias->primaryEmail = $postBody->primaryEmail;
             $insertedAlias = $usersAliases->insertAssumingUserExists($newAlias);
         }
     }
     // Get (and return) the new user that was just created back out of the database
     return $this->get($postBody->primaryEmail);
 }
 /**
  * @expectedException \Exception
  * @expectedExceptionCode 201407101130
  */
 public function testUsersUpdate_NotThere()
 {
     $fixturesClass = new GoogleFixtures($this->dataFile);
     $fixturesClass->removeAllFixtures();
     $userId = 999999;
     $userData = array("changePasswordAtNextLogin" => false, "hashFunction" => "SHA-1", "id" => $userId, "password" => "testP4ss", "primaryEmail" => "*****@*****.**", "suspended" => false);
     $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);
     // the assert is in the doc comment
 }
 /**
  * Gets a Google_Service_Directory_Aliases instance with its
  *     aliases property populated with Google_Service_Directory_Alias
  *     instances for that user
  *
  * @param string $keyType - "Email" or "Id"
  * @param string $userKey - The Email or immutable Id of the user
  * @return null|a real Google_Service_Directory_Aliases instance
  */
 public function fetchAliasesByUser($keyType, $userKey)
 {
     $sqliteUtils = new SqliteUtils($this->_dbFile);
     $aliases = $sqliteUtils->getAllRecordsByDataKey($this->_dataType, $this->_dataClass, $keyType, $userKey);
     if (!$aliases) {
         return null;
     }
     $foundAliases = array();
     foreach ($aliases as $nextAlias) {
         $newAlias = new \Google_Service_Directory_Alias();
         ObjectUtils::initialize($newAlias, json_decode($nextAlias['data'], true));
         $foundAliases[] = $newAlias;
     }
     $newUsersAliases = new \Google_Service_Directory_Aliases();
     $newUsersAliases->setAliases($foundAliases);
     return $newUsersAliases;
 }