You are here

public function UrlloginController::linkTest in urllogin 8

Same name and namespace in other branches
  1. 2.x src/Controller/UrlloginController.php \Drupal\urllogin\Controller\UrlloginController::linkTest()

Diagnostic test page for setting up urllogin urls.

Parameters

string $urlstring: login string from URL.

string $dest: Optional destination URL.

Return value

object Page containing test results

1 string reference to 'UrlloginController::linkTest'
urllogin.routing.yml in ./urllogin.routing.yml
urllogin.routing.yml

File

src/Controller/UrlloginController.php, line 31

Class

UrlloginController
Controller routines for urllogin routes.

Namespace

Drupal\urllogin\Controller

Code

public function linkTest($urlstring = 'none', $dest = '') {

  // module_load_include('inc', 'urllogin', 'urllogin');.
  module_load_include('inc', 'urllogin', 'urllogin_security');

  // Sanitize.
  $urlstr = Html::escape($urlstring);
  $element = [
    '#markup' => '',
  ];
  $page = "<ul><li>Initial URL string = [{$urlstr}]</li>";
  $resultmsg = "";
  $user = User::load(\Drupal::currentUser()
    ->id());
  $config = $this
    ->config('urllogin.settings');
  $codekey = $config
    ->get('urllogin.codekey');
  $codemin = $config
    ->get('urllogin.codemin');
  $uid = urllogin_decode($urlstr, $codekey, $codemin, urllogin_passphrase(), $resultmsg, $user
    ->get('uid')->value);
  if ($uid > -1) {
    $account = urllogin_testuid($uid, $resultmsg);
  }
  else {
    $account = NULL;
  }
  if ($account != NULL) {

    // Find where to go: get rid of first two arguments and use the rest of
    // the URL as the destination.
    $current_path = \Drupal::service('path.current')
      ->getPath();
    $args = explode('/', $current_path);
    unset($args[0]);
    unset($args[1]);
    unset($args[2]);
    $goto = implode('/', $args);

    // Maintain the original query string.
    $query = $_GET;
    unset($query['q']);
    if (count($query) > 0) {
      $goto .= '?' . implode('&', $query);
    }

    // Check in case this user is already logged in.
    $logged_in = $user
      ->get('uid')->value == $uid;
    if ($logged_in) {
      $resultmsg = t('User %username (%uid) was already logged in. Redirected to: %goto', [
        '%username' => $account
          ->get('name')->value,
        '%uid' => $uid,
        '%goto' => $goto,
      ]);
      \Drupal::logger('urllogin')
        ->notice($resultmsg);
    }
    else {
      $resultmsg = t('Logging in as %username (%uid). Redirected to: %goto', [
        '%username' => $account
          ->get('name')->value,
        '%uid' => $uid,
        '%goto' => $goto,
      ]);
    }

    // Get rid of first two arguments and use the rest of the URL as the
    // destination.
    $page .= "<li>{$resultmsg}</li><li>goto: {$goto}</li></ul>";
  }
  $element['#markup'] .= $page;
  return $element;
}