public function connectServiceAction(Request $request, $service)
    {
        $connect = $this->container->getParameter('hwi_oauth.connect');
        if (!$connect) {
            throw new NotFoundHttpException();
        }

        $hasUser = $this->isGranted($this->container->getParameter('hwi_oauth.grant_rule'));
        if (!$hasUser) {
            throw new AccessDeniedException('Cannot connect an account.');
        }

        // Get the data from the resource owner
        $resourceOwner = $this->getResourceOwnerByName($service);

        $session = $request->getSession();
        if (!$session->isStarted()) {
            $session->start();
        }

        $key = $request->query->get('key', time());

        if ($resourceOwner->handles($request)) {
            $accessToken = $resourceOwner->getAccessToken(
                $request,
                $this->container->get('hwi_oauth.security.oauth_utils')->getServiceAuthUrl($request, $resourceOwner)
            );

            // save in session
            $session->set('_hwi_oauth.connect_confirmation.'.$key, $accessToken);
        } else {
            $accessToken = $session->get('_hwi_oauth.connect_confirmation.'.$key);
        }

        // Redirect to the login path if the token is empty (Eg. User cancelled auth)
        if (null === $accessToken) {
            if ($this->container->getParameter('hwi_oauth.failed_use_referer') && $targetPath = $this->getTargetPath($session, 'failed_target_path')) {
                return $this->redirect($targetPath);
            }

            return $this->redirectToRoute($this->container->getParameter('hwi_oauth.failed_auth_path'));
        }

        // Show confirmation page?
        if (!$this->container->getParameter('hwi_oauth.connect.confirmation')) {
            return $this->getConfirmationResponse($request, $accessToken, $service);
        }

        /** @var $form FormInterface */
        $form = $this->createForm(FormType::class);

        // Handle the form
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            return $this->getConfirmationResponse($request, $accessToken, $service);
        }

        $event = new GetResponseUserEvent($this->getUser(), $request);
        $this->get('event_dispatcher')->dispatch(HWIOAuthEvents::CONNECT_INITIALIZE, $event);

        if ($response = $event->getResponse()) {
            return $response;
        }

        return $this->render('@HWIOAuth/Connect/connect_confirm.html.twig', [
            'key' => $key,
            'service' => $service,
            'form' => $form->createView(),
            'userInformation' => $resourceOwner->getUserInformation($accessToken),
        ]);
    }
public function connectServiceAction(Request $request, $service)
    {
        $connect = $this->container->getParameter('hwi_oauth.connect');
        if (!$connect) {
            throw new NotFoundHttpException();
        }

        $hasUser = $this->isGranted($this->container->getParameter('hwi_oauth.grant_rule'));
        if (!$hasUser) {
            throw new AccessDeniedException('Cannot connect an account.');
        }

        // Get the data from the resource owner
        $resourceOwner = $this->getResourceOwnerByName($service);

        $session = $request->getSession();
        if (!$session->isStarted()) {
            $session->start();
        }

        $key = $request->query->get('key', time());

        if ($resourceOwner->handles($request)) {
            $accessToken = $resourceOwner->getAccessToken(
                $request,
                $this->container->get('hwi_oauth.security.oauth_utils')->getServiceAuthUrl($request, $resourceOwner)
            );

            // save in session
            $session->set('_hwi_oauth.connect_confirmation.'.$key, $accessToken);
        } else {
            $accessToken = $session->get('_hwi_oauth.connect_confirmation.'.$key);
        }

        // Redirect to the login path if the token is empty (Eg. User cancelled auth)
        if (null === $accessToken) {
            if ($this->container->getParameter('hwi_oauth.failed_use_referer') && $targetPath = $this->getTargetPath($session, 'failed_target_path')) {
                return $this->redirect($targetPath);
            }

            return $this->redirectToRoute($this->container->getParameter('hwi_oauth.failed_auth_path'));
        }

        // Show confirmation page?
        if (!$this->container->getParameter('hwi_oauth.connect.confirmation')) {
            return $this->getConfirmationResponse($request, $accessToken, $service);
        }

        /** @var $form FormInterface */
        $form = $this->createForm(FormType::class);

        // Handle the form
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            return $this->getConfirmationResponse($request, $accessToken, $service);
        }

        $event = new GetResponseUserEvent($this->getUser(), $request);
        $this->get('event_dispatcher')->dispatch(HWIOAuthEvents::CONNECT_INITIALIZE, $event);

        if ($response = $event->getResponse()) {
            return $response;
        }

        return $this->render('@HWIOAuth/Connect/connect_confirm.html.twig', [
            'key' => $key,
            'service' => $service,
            'form' => $form->createView(),
            'userInformation' => $resourceOwner->getUserInformation($accessToken),
        ]);
    }
public function connectServiceAction ( Request $ request , $ service ) { $ connect = $ this -> container -> getParameter ( 'hwi_oauth.connect' ) ; if ( ! $ connect ) { throw new NotFoundHttpException ( ) ; } $ hasUser = $ this -> isGranted ( $ this -> container -> getParameter ( 'hwi_oauth.grant_rule' ) ) ; if ( ! $ hasUser ) { throw new AccessDeniedException ( 'Cannot connect an account.' ) ; } // Get the data from the resource owner $ resourceOwner = $ this -> getResourceOwnerByName ( $ service ) ; $ session = $ request -> getSession ( ) ; if ( ! $ session -> isStarted ( ) ) { $ session -> start ( ) ; } $ key = $ request -> query -> get ( 'key' , time ( ) ) ; if ( $ resourceOwner -> handles ( $ request ) ) { $ accessToken = $ resourceOwner -> getAccessToken ( $ request , $ this -> container -> get ( 'hwi_oauth.security.oauth_utils' ) -> getServiceAuthUrl ( $ request , $ resourceOwner ) ) ; // save in session $ session -> set ( '_hwi_oauth.connect_confirmation.' . $ key , $ accessToken ) ; } else { $ accessToken = $ session -> get ( '_hwi_oauth.connect_confirmation.' . $ key ) ; } // Redirect to the login path if the token is empty (Eg. User cancelled auth) if ( null === $ accessToken ) { if ( $ this -> container -> getParameter ( 'hwi_oauth.failed_use_referer' ) && $ targetPath = $ this -> getTargetPath ( $ session , 'failed_target_path' ) ) { return $ this -> redirect ( $ targetPath ) ; } return $ this -> redirectToRoute ( $ this -> container -> getParameter ( 'hwi_oauth.failed_auth_path' ) ) ; } // Show confirmation page? if ( ! $ this -> container -> getParameter ( 'hwi_oauth.connect.confirmation' ) ) { return $ this -> getConfirmationResponse ( $ request , $ accessToken , $ service ) ; } /** @var $form FormInterface */ $ form = $ this -> createForm ( FormType :: class ) ; // Handle the form $ form -> handleRequest ( $ request ) ; if ( $ form -> isSubmitted ( ) && $ form -> isValid ( ) ) { return $ this -> getConfirmationResponse ( $ request , $ accessToken , $ service ) ; } $ event = new GetResponseUserEvent ( $ this -> getUser ( ) , $ request ) ; $ this -> get ( 'event_dispatcher' ) -> dispatch ( HWIOAuthEvents :: CONNECT_INITIALIZE , $ event ) ; if ( $ response = $ event -> getResponse ( ) ) { return $ response ; } return $ this -> render ( '@HWIOAuth/Connect/connect_confirm.html.twig' , [ 'key' => $ key , 'service' => $ service , 'form' => $ form -> createView ( ) , 'userInformation' => $ resourceOwner -> getUserInformation ( $ accessToken ) , ] ) ; }
Connects a user to a given account if the user is logged in and connect is enabled.

@param Request $request the active request
@param string  $service name of the resource owner to connect to

@throws \Exception

@return Response

@throws NotFoundHttpException if `connect` functionality was not enabled
@throws AccessDeniedException if no user is authenticated
Connects a user to a given account if the user is logged in and connect is enabled .