View source
<?php
namespace Drupal\externalauth;
use Drupal\Core\Database\Connection;
use Drupal\user\UserInterface;
class Authmap implements AuthmapInterface {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public function save(UserInterface $account, $provider, $authname, $data = NULL) {
if (!is_scalar($data)) {
$data = serialize($data);
}
$this->connection
->merge('authmap')
->keys([
'uid' => $account
->id(),
'provider' => $provider,
])
->fields([
'authname' => $authname,
'data' => $data,
])
->execute();
}
public function get($uid, $provider) {
$authname = $this->connection
->select('authmap', 'am')
->fields('am', [
'authname',
])
->condition('uid', $uid)
->condition('provider', $provider)
->range(0, 1)
->execute()
->fetchObject();
if ($authname) {
return $authname->authname;
}
return FALSE;
}
public function getAuthData($uid, $provider) {
$data = $this->connection
->select('authmap', 'am')
->fields('am', [
'authname',
'data',
])
->condition('uid', $uid)
->condition('provider', $provider)
->range(0, 1)
->execute()
->fetchAssoc();
return $data;
}
public function getAll($uid) {
$query = $this->connection
->select('authmap', 'am')
->fields('am', [
'provider',
'authname',
])
->condition('uid', $uid)
->orderBy('provider', 'ASC')
->execute();
$result = $query
->fetchAllAssoc('provider');
if ($result) {
foreach ($result as $provider => $data) {
$result[$provider] = $data->authname;
}
}
return $result;
}
public function getUid($authname, $provider) {
$authname = $this->connection
->select('authmap', 'am')
->fields('am', [
'uid',
])
->condition('authname', $authname)
->condition('provider', $provider)
->range(0, 1)
->execute()
->fetchObject();
if ($authname) {
return $authname->uid;
}
return FALSE;
}
public function delete($uid, $provider = NULL) {
$query = $this->connection
->delete('authmap')
->condition('uid', $uid);
if ($provider) {
$query
->condition('provider', $provider);
}
$query
->execute();
}
public function deleteProvider($provider) {
$this->connection
->delete('authmap')
->condition('provider', $provider)
->execute();
}
}