You are here

class LTIToolProviderController in LTI Tool Provider 8

Same name and namespace in other branches
  1. 2.x src/Controller/LTIToolProviderController.php \Drupal\lti_tool_provider\Controller\LTIToolProviderController

Returns responses for lti_tool_provider module routes.

Hierarchy

Expanded class hierarchy of LTIToolProviderController

1 file declares its use of LTIToolProviderController
LTIToolProviderControllerTest.php in tests/src/Unit/LTIToolProviderControllerTest.php

File

src/Controller/LTIToolProviderController.php, line 26

Namespace

Drupal\lti_tool_provider\Controller
View source
class LTIToolProviderController extends ControllerBase {

  /**
   * The configuration factory.
   *
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * A logger instance.
   *
   * @var LoggerChannelFactoryInterface
   */
  protected $loggerFactory;

  /**
   * The event dispatcher.
   *
   * @var EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The page cache kill switch.
   *
   * @var ResponsePolicyInterface|KillSwitch
   */
  protected $killSwitch;

  /**
   * The request.
   *
   * @var Request
   */
  protected $request;

  /**
   * The request session.
   *
   * @var SessionInterface
   */
  protected $session;

  /**
   * The LTI context.
   *
   * @var mixed
   */
  protected $context;

  /**
   * Optional destination.
   *
   * @var string
   */
  protected $destination;

  /**
   * Constructs a HTTP basic authentication provider object.
   *
   * @param ConfigFactoryInterface $configFactory
   *   The configuration factory.
   * @param LoggerChannelFactoryInterface $loggerFactory
   *   A logger instance.
   * @param EventDispatcherInterface $eventDispatcher
   *   The event dispatcher.
   * @param ResponsePolicyInterface $killSwitch
   *   The page cache kill switch.
   * @param Request $request
   *   The request.
   * @param SessionInterface $session
   *   The request session.
   * @param mixed $context
   *   The LTI context.
   * @param string | null $destination
   *   Optional destination.
   */
  public function __construct(ConfigFactoryInterface $configFactory, LoggerChannelFactoryInterface $loggerFactory, EventDispatcherInterface $eventDispatcher, ResponsePolicyInterface $killSwitch, Request $request, SessionInterface $session, $context, ?string $destination) {
    $this->configFactory = $configFactory;
    $this->loggerFactory = $loggerFactory
      ->get('lti_tool_provider');
    $this->eventDispatcher = $eventDispatcher;
    $this->killSwitch = $killSwitch;
    $this->request = $request;
    $this->session = $session;
    $this->context = $context;
    $this->destination = $destination;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : LTIToolProviderController {

    /* @var $configFactory ConfigFactoryInterface */
    $configFactory = $container
      ->get('config.factory');

    /* @var $loggerFactory LoggerChannelFactoryInterface */
    $loggerFactory = $container
      ->get('logger.factory');

    /* @var $eventDispatcher EventDispatcherInterface */
    $eventDispatcher = $container
      ->get('event_dispatcher');

    /* @var $killSwitch ResponsePolicyInterface */
    $killSwitch = $container
      ->get('page_cache_kill_switch');
    $request = Drupal::request();
    $session = $request
      ->getSession();
    $context = $session
      ->get('lti_tool_provider_context');
    $destination = Drupal::config('lti_tool_provider.settings')
      ->get('destination');
    return new static($configFactory, $loggerFactory, $eventDispatcher, $killSwitch, $request, $session, $context, $destination);
  }

  /**
   * LTI launch.
   *
   * Authenticates the user via the authentication.lti_tool_provider service,
   * login that user, and then redirect the user to the appropriate page.
   *
   * @return RedirectResponse
   *   Redirect user to appropriate LTI url.
   *
   * @see \Drupal\lti_tool_provider\Authentication\Provider\LTIToolProvider
   *   This controller requires that the authentication.lti_tool_provider
   *   service is attached to this route in lti_tool_provider.routing.yml.
   */
  public function ltiLaunch() : RedirectResponse {
    try {
      $destination = '/';
      if (empty($this->context)) {
        throw new Exception('LTI context missing.');
      }
      if (!empty($this->destination)) {
        $destination = $this->destination;
      }
      if (isset($this->context['custom_destination']) && !empty($this->context['custom_destination'])) {
        $destination = $this->context['custom_destination'];
      }
      $this->killSwitch
        ->trigger();
      $event = new LtiToolProviderLaunchRedirectEvent($this->context, $destination);
      LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
      if ($event
        ->isCancelled()) {
        throw new Exception($event
          ->getMessage());
      }
      $destination = $event
        ->getDestination();
      return new RedirectResponse($destination);
    } catch (Exception $e) {
      $this->loggerFactory
        ->warning($e
        ->getMessage());
      return new RedirectResponse('/', 500);
    }
  }

  /**
   * LTI return.
   *
   * Logs the user out and returns the user to the LMS.
   *
   * @return RedirectResponse
   *   Redirect user to appropriate return url.
   */
  public function ltiReturn() {
    try {
      $destination = '/';
      if (empty($this->context)) {
        throw new Exception('LTI context missing in return request.');
      }
      if (!empty($this->destination)) {
        $destination = $this->destination;
      }
      if (isset($this->context['launch_presentation_return_url']) && !empty($this->context['launch_presentation_return_url'])) {
        $destination = $this->context['launch_presentation_return_url'];
      }
      $this->killSwitch
        ->trigger();
      $event = new LtiToolProviderReturnEvent($this->context, $destination);
      LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
      if ($event
        ->isCancelled()) {
        throw new Exception($event
          ->getMessage());
      }
      $destination = $event
        ->getDestination();
      $this
        ->userLogout();
      return new TrustedRedirectResponse($destination);
    } catch (Exception $e) {
      $this->loggerFactory
        ->warning($e
        ->getMessage());
      return new RedirectResponse('/', 500);
    }
  }

  /**
   * Checks access for LTI routes.
   *
   * @return AccessResult
   *   The access result.
   */
  public function access() : AccessResult {
    return AccessResult::allowedIf(!empty($this->context));
  }
  protected function userLogout() {
    user_logout();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
LTIToolProviderController::$configFactory protected property The configuration factory. Overrides ControllerBase::$configFactory
LTIToolProviderController::$context protected property The LTI context.
LTIToolProviderController::$destination protected property Optional destination.
LTIToolProviderController::$eventDispatcher protected property The event dispatcher.
LTIToolProviderController::$killSwitch protected property The page cache kill switch.
LTIToolProviderController::$loggerFactory protected property A logger instance. Overrides LoggerChannelTrait::$loggerFactory
LTIToolProviderController::$request protected property The request.
LTIToolProviderController::$session protected property The request session.
LTIToolProviderController::access public function Checks access for LTI routes.
LTIToolProviderController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
LTIToolProviderController::ltiLaunch public function LTI launch.
LTIToolProviderController::ltiReturn public function LTI return.
LTIToolProviderController::userLogout protected function
LTIToolProviderController::__construct public function Constructs a HTTP basic authentication provider object.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.