Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<info>Starting User Sync Process.</info>');
     $this->userManager->resetExaminedFlagForAllUsers();
     $this->pendingUserUpdateManager->removeAllPendingUserUpdates();
     $campusIds = $this->userManager->getAllCampusIds(false, false);
     $output->writeln('<info>Attempting to update the ' . count($campusIds) . ' enabled and non sync ignored users in the system.</info>');
     $output->writeln('<info>Searching the directory for users.</info>');
     $allUserRecords = $this->directory->findByCampusIds($campusIds);
     if (!$allUserRecords) {
         $output->writeln('<error>[E] Unable to find any users in the directory.</error>');
     }
     $totalRecords = count($allUserRecords);
     $output->writeln("<info>Found {$totalRecords} records in the directory.</info>");
     $updated = 0;
     $chunks = array_chunk($allUserRecords, 500);
     foreach ($chunks as $userRecords) {
         foreach ($userRecords as $recordArray) {
             $users = $this->userManager->findUsersBy(['campusId' => $recordArray['campusId'], 'enabled' => true, 'userSyncIgnore' => false]);
             if (count($users) == 0) {
                 //this shouldn't happen unless the user gets updated between
                 //listing all the IDs and getting results back from
                 //the directory
                 $output->writeln('<error>[E] Unable to find an enabled sync active user with ' . 'campus ID ' . $recordArray['campusId'] . '.</error>');
                 continue;
             }
             if (count($users) > 1) {
                 $output->writeln('<error>[E] Multiple accounts exist for the same ' . 'campus ID (' . $recordArray['campusId'] . ').  ' . 'None of them will be updated.</error>');
                 foreach ($users as $user) {
                     $user->setExamined(true);
                     $this->userManager->updateUser($user, false);
                 }
                 continue;
             }
             $user = $users[0];
             $update = false;
             $fixSmallThings = true;
             $output->writeln('<info>[I] Comparing User #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' (' . $user->getEmail() . ') ' . 'to directory user by campus ID ' . $user->getCampusId() . '.</info>');
             if (!$this->validateDirectoryRecord($recordArray, $output)) {
                 $user->setExamined(true);
                 $this->userManager->updateUser($user, false);
                 //don't do anything else with invalid directory data
                 continue;
             }
             if ($user->getEmail() != $recordArray['email']) {
                 if (strtolower($user->getEmail()) == strtolower($recordArray['email'])) {
                     $update = true;
                     $output->writeln('  <comment>[I] Updating email from "' . $user->getEmail() . '" to "' . $recordArray['email'] . '" since the only difference was the case.</comment>');
                     $user->setEmail($recordArray['email']);
                 } else {
                     $fixSmallThings = false;
                     $output->writeln('  <comment>[I] Email address "' . $user->getEmail() . '" differs from "' . $recordArray['email'] . '" logging for further action.</comment>');
                     $pendingUpdate = $this->pendingUserUpdateManager->createPendingUserUpdate();
                     $pendingUpdate->setUser($user);
                     $pendingUpdate->setProperty('email');
                     $pendingUpdate->setValue($recordArray['email']);
                     $pendingUpdate->setType('emailMismatch');
                     $this->pendingUserUpdateManager->updatePendingUserUpdate($pendingUpdate, false);
                 }
             }
             if ($fixSmallThings && $user->getFirstName() != $recordArray['firstName']) {
                 $update = true;
                 $output->writeln('  <comment>[I] Updating first name from "' . $user->getFirstName() . '" to "' . $recordArray['firstName'] . '".</comment>');
                 $user->setFirstName($recordArray['firstName']);
             }
             if ($fixSmallThings && $user->getLastName() != $recordArray['lastName']) {
                 $update = true;
                 $output->writeln('  <comment>[I] Updating last name from "' . $user->getLastName() . '" to "' . $recordArray['lastName'] . '".</comment>');
                 $user->setLastName($recordArray['lastName']);
             }
             if ($fixSmallThings && $user->getPhone() != $recordArray['telephoneNumber']) {
                 $update = true;
                 $output->writeln('  <comment>[I] Updating phone number from "' . $user->getPhone() . '" to "' . $recordArray['telephoneNumber'] . '".</comment>');
                 $user->setPhone($recordArray['telephoneNumber']);
             }
             $authentication = $user->getAuthentication();
             if (!$authentication) {
                 $output->writeln('  <comment>[I] User had no authentication data, creating it now.</comment>');
                 $authentication = $this->authenticationManager->createAuthentication();
                 $authentication->setUser($user);
             }
             if ($fixSmallThings && $authentication->getUsername() != $recordArray['username']) {
                 $update = true;
                 $output->writeln('  <comment>[I] Updating username from "' . $authentication->getUsername() . '" to "' . $recordArray['username'] . '".</comment>');
                 $authentication->setUsername($recordArray['username']);
                 $this->authenticationManager->updateAuthentication($authentication, false);
             }
             if ($update) {
                 $updated++;
             }
             $user->setExamined(true);
             $this->userManager->updateUser($user, false);
         }
         $this->em->flush();
         $this->em->clear();
     }
     $output->writeln('<info>Searching for users who were not examined during the sync process.</info>');
     $unsyncedUsers = $this->userManager->findUsersBy(['examined' => false, 'enabled' => true, 'userSyncIgnore' => false], ['lastName' => ' ASC', 'firstName' => 'ASC']);
     $output->writeln('<info>Found ' . count($unsyncedUsers) . ' unexamined users.</info>');
     foreach ($unsyncedUsers as $user) {
         $output->writeln('<comment>[I] User #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' ' . $user->getEmail() . ' not found in the directory.  Logged for further study.</comment>');
         $update = $this->pendingUserUpdateManager->createPendingUserUpdate();
         $update->setUser($user);
         $update->setType('missingFromDirectory');
         $this->pendingUserUpdateManager->updatePendingUserUpdate($update, false);
     }
     $this->em->flush();
     $output->writeln("<info>Completed sync process {$totalRecords} users found in the directory; " . "{$updated} users updated.</info>");
 }
 /**
  * {@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>');
     }
 }