public function ensureUserExists($username, $email, $password, $parentGroupName = null)
{
if ($parentGroupName) {
$parentSearchHits = $this->searchUserGroups($parentGroupName);
// Found matching Group(s)
if (!empty($parentSearchHits)) {
$firstGroupId = $parentSearchHits[0]->valueObject->contentInfo->id;
foreach ($parentSearchHits as $userGroupHit) {
$groupId = $userGroupHit->valueObject->contentInfo->id;
// Search for user in this group
$user = $this->searchUserByLogin($username, $groupId);
if ($user) {
return $user;
}
}
// create user inside existing parent Group, use first group found
$parentGroup = $this->userService->loadUserGroup($firstGroupId);
return $this->createUser($username, $email, $password, $parentGroup);
}
// else
// Parent Group does not exist yet, so create it at "root" User Group.
$rootGroup = $this->userService->loadUserGroup(self::USERGROUP_ROOT_CONTENT_ID);
$parentGroup = $this->createUserGroup($parentGroupName, $rootGroup);
return $this->createUser($username, $email, $password, $parentGroup);
}
// else,
$user = $this->searchUserByLogin($username);
if (!$user) {
$user = $this->createUser($username, $email, $password);
}
return $user;
}