/** * @covers Ilios\CoreBundle\Service\Directory::findByLdapFilter */ public function testFindByLdapFilter() { $ldapManager = m::mock('Ilios\\CoreBundle\\Service\\LdapManager'); $obj = new Directory($ldapManager, 'campusId'); $filter = '(one)(two)'; $ldapManager->shouldReceive('search')->with($filter)->andReturn(array(1, 2)); $result = $obj->findByLdapFilter($filter); $this->assertSame($result, array(1, 2)); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $searchTerms = $input->getArgument('searchTerms'); $userRecords = $this->directory->find($searchTerms); if (!$userRecords) { $output->writeln('<error>Unable to find anyone matching those terms in the directory.</error>'); return; } $rows = array_map(function ($arr) { return [$arr['campusId'], $arr['firstName'], $arr['lastName'], $arr['email'], $arr['telephoneNumber']]; }, $userRecords); $table = new Table($output); $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email', 'Phone Number'))->setRows($rows); $table->render(); }
/** * {@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)) { /* @var UserRoleInterface $formerStudentRole */ $formerStudentRole = $this->userRoleManager->findOneBy(array('title' => 'Former Student')); /* @var UserInterface $user */ foreach ($usersToUpdate as $user) { $formerStudentRole->addUser($user); $user->addRole($formerStudentRole); $this->userManager->update($user, false); } $this->userRoleManager->update($formerStudentRole); $output->writeln('<info>Former students updated successfully!</info>'); } else { $output->writeln('<comment>Update canceled,</comment>'); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $campusId = $input->getArgument('campusId'); $user = $this->userManager->findOneBy(['campusId' => $campusId]); if ($user) { throw new \Exception('User #' . $user->getId() . " with campus id {$campusId} already exists."); } $schoolId = $input->getArgument('schoolId'); $school = $this->schoolManager->findOneBy(['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->create(); $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->update($user); $authentication = $this->authenticationManager->create(); $authentication->setUser($user); $authentication->setUsername($userRecord['username']); $this->authenticationManager->update($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) { $userId = $input->getArgument('userId'); $user = $this->userManager->findOneBy(['id' => $userId]); if (!$user) { throw new \Exception("No user with id #{$userId} was found."); } $userRecord = $this->directory->findByCampusId($user->getCampusId()); if (!$userRecord) { $output->writeln('<error>Unable to find ' . $user->getCampusId() . ' in the directory.'); return; } $table = new Table($output); $table->setHeaders(array('Record', 'Campus ID', 'First', 'Last', 'Email', 'Phone Number'))->setRows(array(['Ilios User', $user->getCampusId(), $user->getFirstName(), $user->getLastName(), $user->getEmail(), $user->getPhone()], ['Directory User', $userRecord['campusId'], $userRecord['firstName'], $userRecord['lastName'], $userRecord['email'], $userRecord['telephoneNumber']])); $table->render(); $helper = $this->getHelper('question'); $output->writeln(''); $question = new ConfirmationQuestion('<question>Do you wish to update this Ilios User with the data ' . 'from the Directory User? </question>' . "\n", true); if ($helper->ask($input, $output, $question)) { $user->setFirstName($userRecord['firstName']); $user->setLastName($userRecord['lastName']); $user->setEmail($userRecord['email']); $user->setPhone($userRecord['telephoneNumber']); $authentication = $user->getAuthentication(); if (!$authentication) { $authentication = $this->authenticationManager->create(); $authentication->setUser($user); } $authentication->setUsername($userRecord['username']); $this->authenticationManager->update($authentication, false); $this->userManager->update($user); $output->writeln('<info>User 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->findOneBy(['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->findOneBy(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->create(); $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->update($user); $authentication = $this->authenticationManager->create(); $authentication->setUser($user); $authentication->setUsername($userRecord['username']); $this->authenticationManager->update($authentication, false); $studentRole->addUser($user); $this->userRoleManager->update($studentRole); $output->writeln('<info>Success! New student #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' created.</info>'); } } else { $output->writeln('<comment>Update canceled.</comment>'); } }
/** * {@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->findBy(['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>'); /* @var UserInterface $user */ foreach ($users as $user) { $user->setExamined(true); $this->userManager->update($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->update($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->create(); $pendingUpdate->setUser($user); $pendingUpdate->setProperty('email'); $pendingUpdate->setValue($recordArray['email']); $pendingUpdate->setType('emailMismatch'); $this->pendingUserUpdateManager->update($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->create(); $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->update($authentication, false); } if ($update) { $updated++; } $user->setExamined(true); $this->userManager->update($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->findBy(['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->create(); $update->setUser($user); $update->setType('missingFromDirectory'); $this->pendingUserUpdateManager->update($update, false); } $this->em->flush(); $output->writeln("<info>Completed sync process {$totalRecords} users found in the directory; " . "{$updated} users updated.</info>"); }