/** * @Security("has_role('ROLE_USER')") * @Template */ public function tokenAction(Request $request) { // Forcing the user the be fully authenticated (the current session must be logged with password) $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); // Manging the UserTokenForm $userTokenForm = $this->createForm("user_token_form", $this->getUser()); $userTokenFormHandler = new FormHandler(); $processed = $userTokenFormHandler->setForm($userTokenForm)->onSuccess(function (FormInterface $userTokenForm) { // Shortcut of the token $token = trim($userTokenForm->getData()->getToken()); if (empty($token) || $token == null) { $userTokenForm->getData()->setToken(null); $this->addFlash("success", "Your Blih token has been removed"); } else { // Testing the token with BLIH before saving it $blih = new Blih($userTokenForm->getData()->getLogin()); $blih->setToken($token); $whoAmI = $blih->whoAmI()->body; // Testing if the token is working if (isset($whoAmI->message) && $whoAmI->message == $userTokenForm->getData()->getLogin()) { $this->addFlash("success", "Your Blih token has been updated"); } else { $this->addFlash("alert", "Your Blih token is not working"); } } })->onAfter(function (FormInterface $userTokenForm) { // Synchronize with the database $this->getDoctrine()->getManager()->persist($userTokenForm->getData()); $this->getDoctrine()->getManager()->flush(); })->process($request); // Redirecting if the form handler has processed if ($processed) { return $this->redirect($this->generateUrl($request->get("_route"))); } return ["userTokenForm" => $userTokenForm->createView()]; }
public function updateUser(User $user) { // If the user has not a token, ignore if ($user->getToken() == null) { return; } // Connecting with Blih $blih = new Blih($user->getLogin()); $blih->setToken($user->getToken()); // Getting the repositories $blihRepositories = $blih->repository()->all()->body; if (!isset($blihRepositories->repositories)) { return; } $blihRepositories = $blihRepositories->repositories; $repositories = $user->getRepositories(); // Removing the repositories removed $repositories->forAll(function ($key, Repository $repository) use($blihRepositories) { if (!array_key_exists($repository->getName(), $blihRepositories)) { $this->entityManager->remove($repository); } return true; }); // Updating or creating the repositories foreach ($blihRepositories as $name => $data) { $repository = $this->retrieveOrCreateRepository($user, $name); $this->updateRepository($repository); $this->entityManager->persist($repository); } // Updating the user $user->setLastRepositoriesUpdateDate(new \DateTime()); $this->entityManager->persist($user); $this->entityManager->flush(); }