/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<info>Starting former student synchronization process.</info>'); $filter = $input->getArgument('filter'); $formerStudents = $this->directory->findByLdapFilter($filter); if (!$formerStudents) { $output->writeln("<error>{$filter} returned no results.</error>"); return; } $output->writeln('<info>Found ' . count($formerStudents) . ' former students in the directory.</info>'); $formerStudentsCampusIds = array_map(function (array $arr) { return $arr['campusId']; }, $formerStudents); $notFormerStudents = $this->userManager->findUsersWhoAreNotFormerStudents($formerStudentsCampusIds); $usersToUpdate = $notFormerStudents->filter(function (UserInterface $user) { return !$user->isUserSyncIgnore(); }); if (!$usersToUpdate->count() > 0) { $output->writeln("<info>There are no students to update.</info>"); return; } $output->writeln('<info>There are ' . $usersToUpdate->count() . ' students in Ilios who will be marked as a Former Student.</info>'); $rows = $usersToUpdate->map(function (UserInterface $user) { return [$user->getCampusId(), $user->getFirstName(), $user->getLastName(), $user->getEmail()]; })->toArray(); $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 mark these users as Former Students? </question>' . "\n", true); if ($helper->ask($input, $output, $question)) { $formerStudentRole = $this->userRoleManager->findUserRoleBy(array('title' => 'Former Student')); foreach ($usersToUpdate as $user) { $formerStudentRole->addUser($user); $user->addRole($formerStudentRole); $this->userManager->updateUser($user, false); } $this->userRoleManager->updateUserRole($formerStudentRole); $output->writeln('<info>Former students updated successfully!</info>'); } else { $output->writeln('<comment>Update 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>'); } }