You are here

lti_tool_provider.module in LTI Tool Provider 8

Same filename and directory in other branches
  1. 7 lti_tool_provider.module
  2. 2.x lti_tool_provider.module

LTI Tool Provider hook implementations and support functions.

File

lti_tool_provider.module
View source
<?php

/**
 * @file
 * LTI Tool Provider hook implementations and support functions.
 */

/**
 * Length of time to accept and store nonces.
 */
const LTI_TOOL_PROVIDER_NONCE_INTERVAL = 5 * 60;

/**
 * Length of time to accept and store nonces.
 */
const LTI_TOOL_PROVIDER_NONCE_EXPIRY = 1.5 * 60 * 60;

/**
 * Implements hook_help().
 * @param $route_name
 * @return string|null
 */
function lti_tool_provider_help($route_name) : ?string {
  switch ($route_name) {
    case 'help.page.lti_tool_provider':
      return '<p>' . t('The LTI tool provider module provides an oauth based authentication provider for Drupal, as well as configuration options for consumers and user provisioning.') . '</p>';
  }
  return null;
}

/**
 * Implements hook_cron().
 */
function lti_tool_provider_cron() {
  $expires = time() - LTI_TOOL_PROVIDER_NONCE_EXPIRY;
  try {
    $nonceStorage = Drupal::entityTypeManager()
      ->getStorage('lti_tool_provider_nonce');
    $ids = $nonceStorage
      ->getQuery()
      ->condition('timestamp', $expires, '<')
      ->execute();
    $entities = $nonceStorage
      ->loadMultiple($ids);
    $nonceStorage
      ->delete($entities);
  } catch (Exception $e) {
    Drupal::logger('lti_tool_provider')
      ->error($e
      ->getMessage());
  }
}

/**
 * Utility function to get an array of fully qualified LTI user roles.
 *
 * @param mixed $roles
 *   Comma-separated list of roles or array of roles.
 *
 * @return array
 *   An array of roles
 */
function parse_roles($roles) : array {
  $parsedRoles = [];
  if (!is_array($roles)) {
    $roles = explode(',', $roles);
  }
  foreach ($roles as $role) {
    $role = trim($role);
    if (!empty($role)) {
      if (substr($role, 0, 4) !== 'urn:') {
        $role = 'urn:lti:role:ims/lis/' . $role;
      }
      $parsedRoles[] = $role;
    }
  }
  return $parsedRoles;
}

Functions

Namesort descending Description
lti_tool_provider_cron Implements hook_cron().
lti_tool_provider_help Implements hook_help().
parse_roles Utility function to get an array of fully qualified LTI user roles.

Constants

Namesort descending Description
LTI_TOOL_PROVIDER_NONCE_EXPIRY Length of time to accept and store nonces.
LTI_TOOL_PROVIDER_NONCE_INTERVAL Length of time to accept and store nonces.