Example #1
0
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $email = $response->getEmail();
     $service = $response->getResourceOwner()->getName();
     /** @var UserOauthAccount $connection */
     $connection = $this->em->getRepository('OjsUserBundle:UserOauthAccount')->findOneBy(['providerId' => $username, 'provider' => $service]);
     if (!$connection && !empty($email)) {
         $userByEmail = $this->userManager->findUserByEmail($email);
         if ($userByEmail) {
             $connection = new UserOauthAccount();
             $connection->setUser($userByEmail);
             $connection->setProvider($service);
             $connection->setProviderId($response->getUsername());
         }
     }
     if (!$connection || $connection->getUser() === null) {
         $message = sprintf("User not found. Please register first and then connect the account from your profile.", $username);
         throw new AccountNotLinkedException($message);
     }
     $connection->setToken($response->getAccessToken());
     $this->em->persist($connection);
     $this->em->flush();
     return $connection->getUser();
 }
Example #2
0
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $email = $response->getEmail();
     $service = $response->getResourceOwner()->getName();
     /** @var UserOauthAccount $connection */
     $connection = $this->em->getRepository('OjsUserBundle:UserOauthAccount')->findOneBy(['providerId' => $username, 'provider' => $service]);
     if (!$connection && !empty($email)) {
         $userByEmail = $this->userManager->findUserByEmail($email);
         if ($userByEmail) {
             $connection = new UserOauthAccount();
             $connection->setUser($userByEmail);
             $connection->setProvider($service);
             $connection->setProviderId($response->getUsername());
         }
     }
     if (!$connection || $connection->getUser() === null) {
         if (empty($response->getEmail())) {
             $message = sprintf("User not found. Please register first and then connect the account from your profile.", $username);
             throw new AccountNotLinkedException($message);
         }
         $fullName = $response->getRealName();
         $parts = explode(" ", $fullName);
         $lastname = array_pop($parts);
         $firstname = implode(" ", $parts);
         $user = $this->userManager->createUser();
         $user->setEnabled(true);
         $user->setUsername($response->getUsername());
         $user->setEmail($response->getEmail());
         $user->setPlainPassword(bin2hex(random_bytes(5)));
         $user->setFirstName($firstname);
         $user->setLastName($lastname);
         $this->userManager->updateUser($user);
         $connection = new UserOauthAccount();
         $connection->setUser($user);
         $connection->setProvider($service);
         $connection->setProviderId($response->getUsername());
     }
     $connection->setToken($response->getAccessToken());
     $this->em->persist($connection);
     $this->em->flush();
     return $connection->getUser();
 }
Example #3
0
 public function addOrcidAccountAction(Request $request)
 {
     $user = $this->getUser();
     if (!$user) {
         throw new AccessDeniedException();
     }
     $orcid = $this->get('ojs.orcid_service');
     $code = $request->get('code');
     $orcid->setRedirectUri('http://' . $this->container->getParameter('base_host') . $this->get('router')->generate('ojs_user_add_orcid_account'));
     if (!$code) {
         return new RedirectResponse($orcid->loginUrl());
     }
     $post = $orcid->authorize($code);
     $em = $this->getDoctrine()->getManager();
     if ($post) {
         $oauth = new UserOauthAccount();
         $oauth->setProvider('orcid')->setProviderAccessToken($post->access_token)->setProviderRefreshToken($post->refresh_token)->setProviderUserId($post->orcid)->setUser($user);
         $em->persist($oauth);
         $user->addOauthAccount($oauth);
         $em->persist($user);
         $em->flush();
         return $this->redirect($this->get('router')->generate('ojs_user_connected_account'));
     }
     throw new \ErrorException("An error", serialize($post));
 }
 public function registerAction(Request $request)
 {
     $allowanceSetting = $this->getDoctrine()->getRepository('OjsAdminBundle:SystemSetting')->findOneBy(['name' => 'user_registration']);
     if ($allowanceSetting) {
         if (!$allowanceSetting->getValue()) {
             return $this->render('OjsSiteBundle:Site:not_available.html.twig', ['title' => 'title.register', 'message' => 'message.registration_not_available']);
         }
     }
     /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
     $formFactory = $this->get('fos_user.registration.form.factory');
     /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
     $userManager = $this->get('ojs_user.manager');
     /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
     $dispatcher = $this->get('event_dispatcher');
     /** @var User $user */
     $user = $userManager->createUser();
     $user->setEnabled(true);
     //Add default data for oauth login
     $session = $this->get('session');
     $oauth_login = $session->get('oauth_login', false);
     if ($oauth_login) {
         $name = explode(' ', $oauth_login['full_name']);
         $firstName = $name[0];
         unset($name[0]);
         $lastName = implode(' ', $name);
         $user->setFirstName($firstName)->setLastName($lastName)->setUsername(StringHelper::slugify($oauth_login['full_name']));
     }
     $event = new GetResponseUserEvent($user, $request);
     $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
     if (null !== $event->getResponse()) {
         return $event->getResponse();
     }
     $form = $formFactory->createForm();
     $form->setData($user);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $event = new FormEvent($form, $request);
         $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
         $user->generateApiKey();
         $userManager->updateUser($user);
         $tokenGenerator = new TokenGenerator();
         $user->setConfirmationToken($tokenGenerator->generateToken());
         if (null === ($response = $event->getResponse())) {
             $url = $this->generateUrl('login');
             $response = new RedirectResponse($url);
         }
         $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
         if ($oauth_login) {
             $em = $this->getDoctrine()->getManager();
             $oauth = new UserOauthAccount();
             $oauth->setProvider($oauth_login['provider'])->setProviderAccessToken($oauth_login['access_token'])->setProviderRefreshToken($oauth_login['refresh_token'])->setProviderUserId($oauth_login['user_id'])->setUser($user);
             $em->persist($oauth);
             $user->addOauthAccount($oauth);
             $em->persist($user);
         }
         $session->getFlashBag()->add('success', 'registration.activation');
         $session->remove('oauth_login');
         $session->save();
         $event = new UserEvent($user);
         $dispatcher = $this->get('event_dispatcher');
         $dispatcher->dispatch('user.register.complete', $event);
         return $response;
     }
     return $this->render('OjsUserBundle:Registration:register.html.twig', array('form' => $form->createView()));
 }
Example #5
0
 public function registerAction(Request $request)
 {
     $error = null;
     $user = new User();
     $session = $this->get('session');
     //Add default data for oauth login
     $oauth_login = $session->get('oauth_login', false);
     if ($oauth_login) {
         $name = explode(' ', $oauth_login['full_name']);
         $firstName = $name[0];
         unset($name[0]);
         $lastName = implode(' ', $name);
         $user->setFirstName($firstName)->setLastName($lastName)->setUsername($this->slugify($oauth_login['full_name']));
     }
     $form = $this->createForm(new RegisterFormType(), $user);
     $form->handleRequest($request);
     if ($form->isValid()) {
         // check user name exists
         $em = $this->getDoctrine()->getManager();
         $user->setPassword($this->encodePassword($user, $user->getPassword()));
         $user->setToken($user->generateToken());
         $user->generateApiKey();
         $user->setStatus(1);
         $user->setIsActive(0);
         $em->persist($user);
         if ($oauth_login) {
             $oauth = new UserOauthAccount();
             $oauth->setProvider($oauth_login['provider'])->setProviderAccessToken($oauth_login['access_token'])->setProviderRefreshToken($oauth_login['refresh_token'])->setProviderUserId($oauth_login['user_id'])->setUser($user);
             $em->persist($oauth);
             $user->addOauthAccount($oauth);
             $em->persist($user);
         }
         $em->flush();
         //$this->authenticateUser($user); // auth. user
         $session->getFlashBag()->add('success', 'Success. <br>You are registered. Check your email to activate your account.');
         $session->remove('oauth_login');
         $session->save();
         $event = new UserEvent($user);
         $dispatcher = $this->get('event_dispatcher');
         $dispatcher->dispatch('user.register.complete', $event);
         return $this->redirect($this->generateUrl('login'));
     }
     return $this->render('OjsUserBundle:Security:register.html.twig', array('form' => $form->createView(), 'errors' => $form->getErrors()));
 }