Commit af718585 authored by spokje's avatar spokje Committed by Abdullah Yassin
Browse files

Issue #3068326 by Spokje, Rajab Natshah, itaran, apaderno, abu-zakham: Fix...

Issue #3068326 by Spokje, Rajab Natshah, itaran, apaderno, abu-zakham: Fix LogicException & RuntimeException on user login and registration
parent 731bc54b
Loading
Loading
Loading
Loading
+40 −6
Changes for src/Controller/TwitterAuthController.php: 40 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\social_auth\User\UserAuthenticator;
use Drupal\social_auth\SocialAuthDataHandler;
@@ -59,6 +61,13 @@ class TwitterAuthController extends ControllerBase {
   */
  protected $messenger;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * TwitterLoginController constructor.
   *
@@ -74,25 +83,33 @@ class TwitterAuthController extends ControllerBase {
   *   SocialAuthDataHandler object.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   Used to handle metadata for redirection to authentication URL.
   */
  public function __construct(NetworkManager $network_manager,
                              UserAuthenticator $user_authenticator,
                              TwitterAuthManager $twitter_manager,
                              RequestStack $request,
                              SocialAuthDataHandler $data_handler,
                              MessengerInterface $messenger) {
                              MessengerInterface $messenger,
                              RendererInterface $renderer = NULL) {
    $this->networkManager = $network_manager;
    $this->userAuthenticator = $user_authenticator;
    $this->twitterManager = $twitter_manager;
    $this->request = $request;
    $this->dataHandler = $data_handler;
    $this->messenger = $messenger;
    $this->renderer = $renderer;

    // Sets the plugin id.
    $this->userAuthenticator->setPluginId('social_auth_twitter');

    // Sets the session keys to nullify if user could not logged in.
    $this->userAuthenticator->setSessionKeysToNullify(['access_token']);

    if (!$this->renderer) {
      $this->renderer = \Drupal::service('renderer');
    }
  }

  /**
@@ -105,7 +122,8 @@ public static function create(ContainerInterface $container) {
      $container->get('twitter_auth.manager'),
      $container->get('request_stack'),
      $container->get('social_auth.data_handler'),
      $container->get('messenger')
      $container->get('messenger'),
      $container->get('renderer')
    );
  }

@@ -113,6 +131,8 @@ public static function create(ContainerInterface $container) {
   * Redirects to Twitter for authentication.
   */
  public function redirectToTwitter() {
    $context = new RenderContext();
    $response = $this->renderer->executeInRenderContext($context, function () {
      try {
        /* @var \Drupal\social_auth_twitter\Plugin\Network\TwitterAuth $network_plugin */
        // Creates an instance of the social_auth_twitter Network Plugin.
@@ -123,11 +143,14 @@ public function redirectToTwitter() {
        // If destination parameter is set, save it.
        if ($destination) {
          $this->userAuthenticator->setDestination($destination);
          $this->request->getCurrentRequest()->query->remove('destination');
        }

        /* @var \Abraham\TwitterOAuth\TwitterOAuth $connection */
        $connection = $network_plugin->getSdk();

        if ($connection) {

          // Requests Twitter to get temporary tokens.
          $request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => $network_plugin->getOauthCallback()]);

@@ -141,17 +164,28 @@ public function redirectToTwitter() {
          // Forces session to be saved before redirection.
          $this->twitterManager->save();

      $response = new TrustedRedirectResponse($url);
      $response->send();
          return new TrustedRedirectResponse($url);
        }
        else {
          $this->messenger->addError($this->t('Social Auth Twitter not configured properly. Contact site administrator.'));

      // Redirects the user to allow them to grant permissions.
      return $response;
          return $this->redirect('user.login');
        }
      }
      catch (\Exception $ex) {
        $this->messenger->addError($this->t('You could not be authenticated, please contact the administrator.'));

        return $this->redirect('user.login');
      }
    });

    // Add bubbleable metadata to the response.
    if ($response instanceof TrustedRedirectResponse && !$context->isEmpty()) {
      $bubbleable_metadata = $context->pop();
      $response->addCacheableDependency($bubbleable_metadata);
    }

     return $response;
  }

  /**