function AutoLoginUrlCreate::create in Auto Login URL 8
Same name and namespace in other branches
- 2.x src/AutoLoginUrlCreate.php \Drupal\auto_login_url\AutoLoginUrlCreate::create()
Create an auto login hash on demand.
Parameters
int $uid: User id.
string $destination: Destination URL.
bool $absolute: Absolute or relative link.
Return value
string Auto Login URL.
File
- src/
AutoLoginUrlCreate.php, line 38
Class
Namespace
Drupal\auto_login_urlCode
function create($uid, $destination, $absolute = FALSE) {
$config = \Drupal::config('auto_login_url.settings');
// Get ALU secret.
$auto_login_url_secret = \Drupal::service('auto_login_url.general')
->getSecret();
// Get user password.
$password = \Drupal::service('auto_login_url.general')
->getUserHash($uid);
// Create key.
$key = Settings::getHashSalt() . $auto_login_url_secret . $password;
// Repeat until the hash that is saved in DB is unique.
$hash_helper = 0;
do {
$data = $uid . microtime(TRUE) . $destination . $hash_helper;
// Generate hash.
$hash = Crypt::hmacBase64($data, $key);
// Get substring.
$hash = substr($hash, 0, $config
->get('token_length'));
// Generate hash to save to DB.
$hash_db = Crypt::hmacBase64($hash, $key);
// Check hash is unique.
$result = $this->connection
->select('auto_login_url', 'alu')
->fields('alu', [
'hash',
])
->condition('alu.hash', $hash_db)
->execute()
->fetchAssoc();
// Increment value in case there will be a next iteration.
$hash_helper++;
} while (isset($result['hash']));
// Insert a new hash.
$this->connection
->insert('auto_login_url')
->fields([
'uid',
'hash',
'destination',
'timestamp',
])
->values([
'uid' => $uid,
'hash' => $hash_db,
'destination' => $destination,
'timestamp' => time(),
])
->execute();
// Check if link is absolute.
$absolute_path = '';
if ($absolute) {
global $base_url;
$absolute_path = $base_url . '/';
}
return $absolute_path . 'autologinurl/' . $uid . '/' . $hash;
}