View source
<?php
namespace Drupal\urllogin\Controller;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Controller\ControllerBase;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Component\Utility\Html;
class UrlloginController extends ControllerBase {
public function linkTest($urlstring = 'none', $dest = '') {
module_load_include('inc', 'urllogin', 'urllogin_security');
$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) {
$current_path = \Drupal::service('path.current')
->getPath();
$args = explode('/', $current_path);
unset($args[0]);
unset($args[1]);
unset($args[2]);
$goto = implode('/', $args);
$query = $_GET;
unset($query['q']);
if (count($query) > 0) {
$goto .= '?' . implode('&', $query);
}
$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,
]);
}
$page .= "<li>{$resultmsg}</li><li>goto: {$goto}</li></ul>";
}
$element['#markup'] .= $page;
return $element;
}
public function status($testuid = 0) {
module_load_include('inc', 'urllogin', 'urllogin_security');
$element = [
'#markup' => '',
];
$config = $this
->config('urllogin.settings');
$codekey = $config
->get('urllogin.codekey');
$codemin = $config
->get('urllogin.codemin');
$uid = (int) $testuid;
$passphrase = urllogin_passphrase();
$page = '<ul>';
$page .= t('<li>Test UID: @uid </li>', [
'@uid' => $uid,
]);
$page .= t('<li>Passphrase: @passphrase</li>', [
'@passphrase' => $passphrase,
]);
$page .= t('<li>Current Validation number: @codekey .</li>', [
'@codekey' => $codekey,
]);
$page .= t('<li>Minimum validation number: @codemin .</li>', [
'@codemin' => $codemin,
]);
$urlstr = urllogin_encode($uid, $codekey, urllogin_passphrase());
$route_parameters = [
'urlstring' => $urlstr,
];
$page .= '<li>' . t('Encoded URL access string: [') . $urlstr . ']</li>';
$page .= '</ul>';
$testlink = 'l_test/' . $urlstr;
$testlink = Link::fromTextAndUrl($testlink, Url::fromRoute('urllogin.l_test', $route_parameters))
->toString();
$testpage = Link::fromTextAndUrl(t('the test page'), Url::fromRoute('urllogin.l_test'))
->toString();
$page .= t('<p>This page can be used to generate individual access strings for testing purposes.
Simply add the UID of the user to the end of the url for this page, revisit the page and the
access string will be displayed above.</p> <p>To test the access string,
use @testpage by appending the access string to it, e.g.: @testlink.</p>', [
'@testpage' => $testpage,
'@testlink' => $testlink,
]);
$element['#markup'] .= $page;
return $element;
}
public function userList() {
module_load_include('inc', 'urllogin', 'urllogin_security');
$codekey = \Drupal::config('urllogin.settings')
->get('codekey');
$passphrase = urllogin_passphrase();
$thissite = \Drupal::request()
->getSchemeAndHttpHost();
$destination = '/' . \Drupal::config('urllogin.settings')
->get('destination');
$output = "UID, Username, Email, Login URL \n";
$response = new Response();
$response->headers
->set('Content-type', 'text/csv; charset=utf-8');
$response->headers
->set('Content-Disposition', 'inline; filename="userlist.csv"');
$ids = \Drupal::entityQuery('user')
->condition('status', 1)
->execute();
$users = User::loadMultiple($ids);
foreach ($users as $data) {
if ($data
->hasPermission('login via url')) {
$urlstr = $thissite . '/l/' . urllogin_encode($data->uid->value, $codekey, $passphrase) . $destination;
$output .= $data->uid->value . "," . $data->name->value . "," . $data->mail->value . "," . $urlstr . "\r\n";
}
}
$response
->setContent(render($output));
return $response;
}
public function content() {
$build = [
'#markup' => $this
->t('Hello World!'),
];
return $build;
}
public function login($urlstring = 'none', $arg = NULL) {
module_load_include('inc', 'urllogin', 'urllogin_security');
$urlstr = Html::escape($urlstring);
$resultmsg = "";
$user = User::load(\Drupal::currentUser()
->id());
$config = $this
->config('urllogin.settings');
$codekey = $config
->get('codekey');
$codemin = $config
->get('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;
}
\Drupal::logger('urllogin')
->debug($resultmsg);
if ($account != NULL) {
$current_path = \Drupal::service('path.current')
->getPath();
$args = explode('/', $current_path);
unset($args[0]);
unset($args[1]);
unset($args[2]);
$goto = implode('/', $args);
$query = $_GET;
unset($query['q']);
if (count($query) > 0) {
$goto .= '?' . implode('&', $query);
}
$logged_in = $user
->get('uid')->value == $uid;
if ($logged_in) {
$resultmsg = t('User %username (%uid) was already logged in. Redirected to: %goto', [
'%username' => $account->name,
'%uid' => $uid,
'%goto' => $goto,
]);
\Drupal::logger('urllogin')
->notice($resultmsg);
}
else {
$account = User::load($uid);
user_login_finalize($account);
$user = User::load(\Drupal::currentUser()
->id());
$logged_in = $user
->get('uid')->value == $uid;
if ($logged_in) {
$resultmsg = t('Logging in as %username (%uid). Redirected to: %goto', [
'%username' => $account->name,
'%uid' => $uid,
'%goto' => $goto,
]);
\Drupal::logger('urllogin')
->notice($resultmsg);
if (\Drupal::moduleHandler()
->moduleExists('persistent_login')) {
_persistent_login_create_cookie($account);
}
}
else {
$resultmsg = t('Failed login as %username (%uid)', [
'%username' => $account->name,
'%uid' => $uid,
]);
}
}
if ($logged_in) {
$url = '/';
$url .= implode('/', $args);
$redirect = new RedirectResponse(Url::fromUserInput($url)
->toString());
$redirect
->send();
}
}
\Drupal::logger('urllogin')
->notice($resultmsg);
if ($uid == -2) {
$response = [
'#markup' => '<h1>' . t('The link you used to access this page has expired.') . '</h1>' . '<p>' . t('If you have created a password, you can log on') . ' ' . Link::fromTextAndUrl(t('here'), Url::fromRoute('user.login'))
->toString() . '.</p>',
];
return $response;
}
else {
return $this
->redirect('<front>');
}
}
}