You are here

urllogin.module in urllogin 8

Same filename and directory in other branches
  1. 6 urllogin.module
  2. 7 urllogin.module
  3. 2.x urllogin.module

Module file for Urllogin.

Allows login using link from URL.

@todo test if already logged in as someone else and abort @todo expand access string from 11 to 12 characters using SHA256 checksum @todo only allow profile module to be used if enabled with correct fields @todo reduce duplication of test code @todo generate token for simplemail-tokens

File

urllogin.module
View source
<?php

/**
 * @file
 * Module file for Urllogin.
 *
 * Allows login using link from URL.
 *
 * @todo test if already logged in as someone else and abort
 * @todo expand access string from 11 to 12 characters using SHA256 checksum
 * @todo only allow profile module to be used if enabled with correct fields
 * @todo reduce duplication of test code
 * @todo generate token for simplemail-tokens
 */
use Drupal\user\Entity\User;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Returns the current passphrase.
 *
 * If the variable $GLOBALS['urllogin_passphrase'] has been set in settings.php
 * then use it as the passphrase.
 * Otherwise use the string set in the admin interface and if
 * urllogin_add_dbpass is set, then append the database access string.
 *
 * @return string
 *   full passphrase for encryption
 */
function urllogin_passphrase() {
  if (NULL !== Drupal::config('system.urllogin')
    ->get('passphrase')) {
    $passphrase = Drupal::config('system.urllogin')
      ->get('passphrase');
  }
  else {
    $passphrase = Drupal::config('urllogin.settings')
      ->get('passphrase');
    if (\Drupal::config('urllogin.settings')
      ->get('add_dbpass') != 0) {

      // Drupal::getConnectionInfoAsUrlglobal;.
      $passphrase .= 'bob';
    }
  }
  return $passphrase;
}

/**
 * Implements hook_help().
 */
function urllogin_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the urllogin module.
    case 'help.page.urllogin':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Allow login using link from URL') . '</p>';
      return $output;
  }
}

/**
 * Tests to see if UID can be logged on.
 *
 * @param int $uid
 *   UID to be tested.
 * @param string $resultmsg
 *   Contains resultmsg message if function fails.
 *
 * @return object
 *   If sucessful, loaded account of user Else NULL.
 */
function urllogin_testuid($uid, &$resultmsg) {
  if ($uid < 2) {
    $resultmsg = t('UID is %uid - cannot login as user 0 or 1', [
      '%uid' => $uid,
    ]);
    return NULL;
  }
  $account = User::load($uid);
  if (!$account) {
    $resultmsg = t('User %uid does not exist', [
      '%uid' => $uid,
    ]);
    return NULL;
  }
  if (!$account
    ->hasPermission('login via url')) {
    $resultmsg = t('User %uid denied access', [
      '%uid' => $uid,
    ]);
    return NULL;
  }
  $resultmsg = t('User %username (%uid) successfully validated', [
    '%username' => $account
      ->getDisplayName(),
    '%uid' => $uid,
  ]);
  return $account;
}

/**
 * Callback for retrieving URL access details.
 *
 * @param int $uid
 *   User ID of user whose URL access details are to be retrieved.
 *
 * @return object
 *   Services Menu object
 */
function urllogin_retrieve($uid) {
  $user_exists = \Drupal::entityQuery('user')
    ->condition('uid', $uid, '=')
    ->execute()
    ->rowCount();
  if (empty($user_exists)) {
    return NULL;
  }
  module_load_include('inc', 'urllogin', 'urllogin_security');
  $codekey = Drupal::config('urllogin.settings')
    ->get('codekey');
  $passphrase = urllogin_passphrase();
  $result = new stdClass();
  $result->token = urllogin_encode($uid, $codekey, $passphrase);
  $result->url = Drupal::fromUri('l/' . $result->token, [
    'absolute' => TRUE,
  ]);
  return $result;
}

Functions

Namesort descending Description
urllogin_help Implements hook_help().
urllogin_passphrase Returns the current passphrase.
urllogin_retrieve Callback for retrieving URL access details.
urllogin_testuid Tests to see if UID can be logged on.