/** * Initializes this component. * * @param Connector $connector The connector signed in. * @param string $login The login of the user to load data, if null, it will take the signed in user. * @throws \Exception If the Connector is not signed in. */ public function __construct(Connector $connector, $login = null) { $connector->checkSignedIn(); $this->connector = $connector; // Retrieving information about the specified user or signed in user if ($login == null) { $url = str_replace('{LOGIN}', $this->connector->getUser()->getLogin(), self::URL_NETSOUL); } else { $url = str_replace('{LOGIN}', $login, self::URL_NETSOUL); } $response = $this->connector->request($url); $this->data = DataExtractor::retrieve($response); // Parsing the data $logs = array(); foreach ($this->data as $log) { // Building the dates $datetime = null; if ($timestamp = DataExtractor::extract($log, array(0))) { $datetime = new \DateTime(); $datetime->setTimestamp($timestamp); } // Building the log $log = array('timestamp' => $timestamp, 'datetime' => $datetime, 'time_active' => DataExtractor::extract($log, array(1)), 'time_inactive' => DataExtractor::extract($log, array(2)), 'timeout_active' => DataExtractor::extract($log, array(3)), 'timeout_inactive' => DataExtractor::extract($log, array(4)), 'time_average' => DataExtractor::extract($log, array(5))); // Adding the log the temporary array $logs[$log['timestamp']] = $log; } // Replace the temporary array $this->data = $logs; }
/** * Initializes this component. * * @param Connector $connector The Connector signed in. * @param int $school_year The school year. * @param string $code_module The module code. * @param string $code_instance The instance code. */ public function __construct(Connector $connector, $school_year, $code_module, $code_instance) { $connector->checkSignedIn(); $this->connector = $connector; // Retrieving information about the module $url = str_replace(array('{SCHOOL_YEAR}', '{CODE_MODULE}', '{CODE_INSTANCE}'), array($school_year, $code_module, $code_instance), self::URL_MODULE); $response = $this->connector->request($url); $this->data = DataExtractor::retrieve($response); }
/** * Initializes this component. * * @param Connector $connector The connector signed in. * @param string $login The login of the user to load data, if null, it will take the signed in user. * @throws \Exception If the Connector is not signed in. */ public function __construct(Connector $connector, $login = null) { $connector->checkSignedIn(); $this->connector = $connector; // Retrieving information about the specified user or signed in user if ($login == null) { $url = self::URL_SIGNED_IN_USER; } else { $url = str_replace('{LOGIN}', $login, self::URL_USER); } $response = $this->connector->request($url); $this->data = DataExtractor::retrieve($response); }
public function checkAuthentication(UserInterface $user, UsernamePasswordToken $token) { $connector = new Connector($user->getUsername(), $token->getCredentials()); if (!$connector->isSignedIn()) { throw new BadCredentialsException(); } $student = $connector->getStudent(); $user->fromStudent($student); $user->setLastConnectionAt(new \DateTime()); if ($user->getId() == null || $user->getAccount() == null) { $user->setAccount(new Account()); } $this->em->persist($user); $this->em->flush(); }
public function retrieve($login) { // Creating or retrieving the user $user = $this->entityManager->getRepository("AppBundle:User")->findOneByLogin($login); if ($user == null) { $user = new User(); $user->setLogin($login); } // Updating from Intranet $connector = new Connector(); $connector->authenticate(Connector::SIGN_IN_METHOD_CREDENTIALS, $this->connectorParameters["login"], $this->connectorParameters["password"]); if (!$connector->isSignedIn()) { throw new \Exception(); } $intranetUser = new \EpitechAPI\Component\User($connector, $user->getLogin()); $user->updateFromIntranet($intranetUser); return $user; }
/** * Obtains the users registered to this event. * * @param bool $as_object Whether the return type is an array of logins or an array of object User. * @return array */ public function getRegisteredUsers($as_object = false) { if ($this->registered_users_login == null) { $this->registered_users_login = array(); $url = str_replace(array('{SCHOOL_YEAR}', '{CODE_MODULE}', '{CODE_INSTANCE}', '{CODE_ACTIVITY}', '{CODE_EVENT}'), array($this->getSchoolYear(), $this->getModuleCode(), $this->getInstanceCode(), $this->getActivityCode(), $this->getEventCode()), self::URL_USER_REGISTERED); $response = $this->connector->request($url); $registered_users = DataExtractor::retrieve($response); foreach ($registered_users as $registered_user) { $this->registered_users_login[$registered_user['login']] = $registered_user['login']; } } if ($as_object) { if ($this->registered_users == null) { $this->registered_users = array(); foreach ($this->registered_users_login as $login) { $this->registered_users[$login] = new User($this->connector, $login); } } return $this->registered_users; } return $this->registered_users_login; }
protected function getUserOrCreateIt($login) { if (($user = $this->getContainer()->get("doctrine")->getManager()->getRepository("AppBundle:User")->findOneByLogin($login)) == null) { $user = new User(); $user->setLogin($login); $connector = new Connector(); $connector->authenticate(Connector::SIGN_IN_METHOD_CREDENTIALS, $this->getContainer()->getParameter("connector")["login"], $this->getContainer()->getParameter("connector")["password"]); if ($connector->isSignedIn() == false) { throw new \RuntimeException("Intranet is not responding"); } $student = new \EpitechAPI\Component\User($connector, $login); $user->updateFromIntranet($student); $this->getContainer()->get("doctrine")->getManager()->persist($user); $this->getContainer()->get("doctrine")->getManager()->flush(); } return $user; }
/** * @Security("has_role('ROLE_SUPER_ADMIN')") * @Template() */ public function editAction(Request $request) { // Shortcuts $em = $this->getDoctrine()->getManager(); $csrf = $this->get('form.csrf_provider'); $post = $request->request->all(); $admin_logger = $this->get('after_epi.admin.logger'); if ($request->isMethod('POST')) { $errors = array(); if (!array_key_exists('csrf_token', $post) || !$csrf->isCsrfTokenValid('user_edit', $post['csrf_token'])) { $errors[0][] = 'La vérification du jeton de sécurité a échoué.'; } if ($request->query->has('id')) { if (($user = $em->getRepository('AfterEpiUserBundle:User')->find($request->query->get('id'))) == null) { throw $this->createNotFoundException('User not found'); } if (!array_key_exists('roles', $post) || !is_array($post['roles']) || count($post['roles']) == 0) { $errors[0][] = 'Vous devez mettre au moins 1 droit.'; } if (count($errors) > 0) { return array('user' => $user, 'errors' => $errors, 'post' => $post); } $user->setRoles($post['roles']); $em->persist($user); $em->flush(); $admin_logger->log("Edition de l'utlisateur [" . $user->getId() . "] [" . $user->getLogin() . "]"); $this->get('session')->getFlashBag()->add('success', "L'utilisateur [" . $user->getLogin() . "] a été modifié."); return $this->redirect($this->generateUrl($request->get('_route'), $request->query->all())); } if (!array_key_exists('login', $post) || empty($post['login'])) { $errors['login'] = '******'; } if (!array_key_exists('password', $post) || empty($post['password'])) { $errors['password'] = '******'; } if (count($errors) > 0) { return array('errors' => $errors, 'post' => $post); } if (($user = $em->getRepository('AfterEpiUserBundle:User')->findOneBy(array('login' => $post['login']))) != null) { $this->get('session')->getFlashBag()->add('info', "Utilisateur [" . $user->getLogin() . "] existe déjà."); return $this->redirect($this->generateUrl('afterepi_user_admin_view', array('id' => $user->getId()))); } $connector = new Connector($this->getUser()->getLogin(), $post['password']); if (!$connector->isSignedIn()) { $this->get('session')->getFlashBag()->add('alert', "Connexion impossible à l'intranet."); return $this->redirect($this->generateUrl($request->get('_route'))); } $student = new Student($connector, $request->get('login', null)); if ($student->getLogin() == null || $student->getFirstName() == null || $student->getLastName() == null) { $this->get('session')->getFlashBag()->add('alert', "L'utilisateur [" . $request->get('login', null) . "] est invalide."); return $this->redirect($this->generateUrl($request->get('_route'))); } $user = new User(); $user->fromStudent($student); $user->setAccount(new Account()); $em->persist($user); $em->flush(); $admin_logger->log("Edition de l'utlisateur [" . $user->getId() . "] [" . $user->getLogin() . "]"); $this->get('session')->getFlashBag()->add('success', "L'utilisateur [" . $user->getLogin() . "] a été ajouté."); return $this->redirect($this->generateUrl('afterepi_user_admin_view', array('id' => $user->getId()))); } else { if ($request->query->has('id')) { if (($user = $em->getRepository('AfterEpiUserBundle:User')->find($request->query->get('id'))) == null) { throw $this->createNotFoundException('User not found'); } return array('user' => $user); } } return array(); }