/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $campusId = $input->getArgument('campusId'); $user = $this->userManager->findUserBy(['campusId' => $campusId]); if ($user) { throw new \Exception('User #' . $user->getId() . " with campus id {$campusId} already exists."); } $schoolId = $input->getArgument('schoolId'); $school = $this->schoolManager->findSchoolBy(['id' => $schoolId]); if (!$school) { throw new \Exception("School with id {$schoolId} could not be found."); } $userRecord = $this->directory->findByCampusId($campusId); if (!$userRecord) { $output->writeln("<error>Unable to find campus ID {$campusId} in the directory.</error>"); return; } $table = new Table($output); $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email', 'Username', 'Phone Number'))->setRows(array([$userRecord['campusId'], $userRecord['firstName'], $userRecord['lastName'], $userRecord['email'], $userRecord['username'], $userRecord['telephoneNumber']])); $table->render(); $helper = $this->getHelper('question'); $output->writeln(''); $question = new ConfirmationQuestion("<question>Do you wish to add this user to Ilios?</question>\n", true); if ($helper->ask($input, $output, $question)) { $user = $this->userManager->createUser(); $user->setFirstName($userRecord['firstName']); $user->setLastName($userRecord['lastName']); $user->setEmail($userRecord['email']); $user->setCampusId($userRecord['campusId']); $user->setAddedViaIlios(true); $user->setEnabled(true); $user->setSchool($school); $user->setUserSyncIgnore(false); $this->userManager->updateUser($user); $authentication = $this->authenticationManager->createAuthentication(); $authentication->setUser($user); $authentication->setUsername($userRecord['username']); $this->authenticationManager->updateAuthentication($authentication); $output->writeln('<info>Success! New user #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' created.</info>'); } else { $output->writeln('<comment>Canceled.</comment>'); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $filter = $input->getArgument('filter'); $schoolId = $input->getArgument('schoolId'); $school = $this->schoolManager->findSchoolBy(['id' => $schoolId]); if (!$school) { throw new \Exception("School with id {$schoolId} could not be found."); } $output->writeln("<info>Searching for new students to add to " . $school->getTitle() . ".</info>"); $students = $this->directory->findByLdapFilter($filter); if (!$students) { $output->writeln("<error>{$filter} returned no results.</error>"); return; } $output->writeln('<info>Found ' . count($students) . ' students in the directory.</info>'); $campusIds = $this->userManager->getAllCampusIds(); $newStudents = array_filter($students, function (array $arr) use($campusIds) { return !in_array($arr['campusId'], $campusIds); }); if (!count($newStudents) > 0) { $output->writeln("<info>There are no new students to add.</info>"); return; } $output->writeln('<info>There are ' . count($newStudents) . ' new students to be added to ' . $school->getTitle() . '.</info>'); $rows = array_map(function (array $arr) { return [$arr['campusId'], $arr['firstName'], $arr['lastName'], $arr['email']]; }, $newStudents); $table = new Table($output); $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email'))->setRows($rows); $table->render(); $helper = $this->getHelper('question'); $output->writeln(''); $question = new ConfirmationQuestion('<question>Do you wish to add these students to ' . $school->getTitle() . '? </question>' . "\n", true); if ($helper->ask($input, $output, $question)) { $studentRole = $this->userRoleManager->findUserRoleBy(array('title' => 'Student')); foreach ($newStudents as $userRecord) { if (empty($userRecord['email'])) { $output->writeln('<error>Unable to add student ' . var_export($userRecord, true) . ' they have no email address.</error>'); continue; } if (empty($userRecord['campusId'])) { $output->writeln('<error>Unable to add student ' . var_export($userRecord, true) . ' they have no campus ID.</error>'); continue; } if (empty($userRecord['username'])) { $output->writeln('<error>Unable to add student ' . var_export($userRecord, true) . ' they have no username.</error>'); continue; } $user = $this->userManager->createUser(); $user->setFirstName($userRecord['firstName']); $user->setLastName($userRecord['lastName']); $user->setEmail($userRecord['email']); $user->setCampusId($userRecord['campusId']); $user->setAddedViaIlios(true); $user->setEnabled(true); $user->setSchool($school); $user->setUserSyncIgnore(false); $user->addRole($studentRole); $this->userManager->updateUser($user); $authentication = $this->authenticationManager->createAuthentication(); $authentication->setUser($user); $authentication->setUsername($userRecord['username']); $this->authenticationManager->updateAuthentication($authentication, false); $studentRole->addUser($user); $this->userRoleManager->updateUserRole($studentRole); $output->writeln('<info>Success! New student #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' created.</info>'); } } else { $output->writeln('<comment>Update canceled.</comment>'); } }