public function ImportUsers() { set_time_limit(300); $groupsList = $this->groupViewRepository->GetList(); /** @var GroupItemView[] $groups */ $groups = $groupsList->Results(); $groupsIndexed = array(); foreach ($groups as $group) { $groupsIndexed[$group->Name()] = $group->Id(); } $importFile = $this->page->GetImportFile(); $csv = new UserImportCsv($importFile); $importCount = 0; $messages = array(); $rows = $csv->GetRows(); if (count($rows) == 0) { $this->page->SetImportResult(new CsvImportResult(0, array(), 'Empty file or missing header row')); return; } for ($i = 0; $i < count($rows); $i++) { $row = $rows[$i]; try { $emailValidator = new EmailValidator($row->email); $uniqueEmailValidator = new UniqueEmailValidator($this->userRepository, $row->email); $uniqueUsernameValidator = new UniqueUserNameValidator($this->userRepository, $row->username); $emailValidator->Validate(); $uniqueEmailValidator->Validate(); $uniqueUsernameValidator->Validate(); if (!$emailValidator->IsValid()) { $messages[] = $emailValidator->Messages()[0] . " ({$row->email})"; continue; } if (!$uniqueEmailValidator->IsValid()) { $messages[] = $uniqueEmailValidator->Messages()[0] . " ({$row->email})"; continue; } if (!$uniqueUsernameValidator->IsValid()) { $messages[] = $uniqueUsernameValidator->Messages()[0] . " ({$row->username})"; continue; } $timezone = empty($row->timezone) ? Configuration::Instance()->GetKey(ConfigKeys::DEFAULT_TIMEZONE) : $row->timezone; $password = empty($row->password) ? 'password' : $row->password; $language = empty($row->language) ? 'en_us' : $row->language; $user = $this->manageUsersService->AddUser($row->username, $row->email, $row->firstName, $row->lastName, $password, $timezone, $language, Configuration::Instance()->GetKey(ConfigKeys::DEFAULT_HOMEPAGE), array(UserAttribute::Phone => $row->phone, UserAttribute::Organization => $row->organization, UserAttribute::Position => $row->position), array()); $userGroups = array(); foreach ($row->groups as $groupName) { if (array_key_exists($groupName, $groupsIndexed)) { Log::Debug('Importing user %s with group %s', $row->username, $groupName); $userGroups[] = new UserGroup($groupsIndexed[$groupName], $groupName); } } if (count($userGroups) > 0) { $user->ChangeGroups($userGroups); $this->userRepository->Update($user); } $importCount++; } catch (Exception $ex) { Log::Error('Error importing users. %s', $ex); } } $this->page->SetImportResult(new CsvImportResult($importCount, $csv->GetSkippedRowNumbers(), $messages)); }