nextcloud_webdav_client-1.0.x-dev/src/Controller/OAuth2InitiateController.php

src/Controller/OAuth2InitiateController.php
<?php

namespace Drupal\nextcloud_webdav_client\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\nextcloud_webdav_client\Service\NextCloudOAuth2Manager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Controller for initiating OAuth2 authorization flow.
 */
class OAuth2InitiateController extends ControllerBase {

  /**
   * The OAuth2 manager service.
   *
   * @var \Drupal\nextcloud_webdav_client\Service\NextCloudOAuth2Manager
   */
  protected $oauth2Manager;

  /**
   * Constructs an OAuth2InitiateController object.
   *
   * @param \Drupal\nextcloud_webdav_client\Service\NextCloudOAuth2Manager $oauth2_manager
   *   The OAuth2 manager service.
   */
  public function __construct(NextCloudOAuth2Manager $oauth2_manager) {
    $this->oauth2Manager = $oauth2_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('nextcloud_webdav_client.oauth2_manager')
    );
  }

  /**
   * Initiates the OAuth2 authorization flow.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response to the OAuth2 provider.
   */
  public function initiate(Request $request): RedirectResponse {
    $callback_url = Url::fromRoute('nextcloud_webdav_client.oauth2_callback', [], [
      'absolute' => TRUE,
    ])->toString();

    // Get session and initiate OAuth2 flow with CSRF protection.
    $session = $request->getSession();
    $auth_url = $this->oauth2Manager->initiateOAuth2Flow($callback_url, $session);

    return new RedirectResponse($auth_url);
  }

  /**
   * Initiates the OAuth2 authorization flow for per-user mode.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response to the OAuth2 provider.
   */
  public function initiateUser(Request $request): RedirectResponse {
    $callback_url = Url::fromRoute('nextcloud_webdav_client.user_oauth2_callback', [], [
      'absolute' => TRUE,
    ])->toString();

    $current_user = $this->currentUser();
    $user = $this->entityTypeManager()->getStorage('user')->load($current_user->id());

    // Get session and initiate OAuth2 flow with CSRF protection.
    $session = $request->getSession();
    $auth_url = $this->oauth2Manager->initiateUserOAuth2Flow($user, $callback_url, $session);

    return new RedirectResponse($auth_url);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc