View source
<?php
namespace Drupal\externalauth;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\externalauth\Event\ExternalAuthEvents;
use Drupal\externalauth\Event\ExternalAuthLoginEvent;
use Drupal\externalauth\Event\ExternalAuthRegisterEvent;
use Drupal\externalauth\Event\ExternalAuthAuthmapAlterEvent;
use Drupal\user\UserInterface;
use Drupal\externalauth\Exception\ExternalAuthRegisterException;
class ExternalAuth implements ExternalAuthInterface {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $entityTypeManager;
protected $authmap;
protected $logger;
protected $eventDispatcher;
public function __construct(EntityTypeManagerInterface $entity_type_manager, AuthmapInterface $authmap, LoggerInterface $logger, EventDispatcherInterface $event_dispatcher) {
$this->entityTypeManager = $entity_type_manager;
$this->authmap = $authmap;
$this->logger = $logger;
$this->eventDispatcher = $event_dispatcher;
}
public function load($authname, $provider) {
if ($uid = $this->authmap
->getUid($authname, $provider)) {
return $this->entityTypeManager
->getStorage('user')
->load($uid);
}
return FALSE;
}
public function login($authname, $provider) {
$account = $this
->load($authname, $provider);
if ($account) {
return $this
->userLoginFinalize($account, $authname, $provider);
}
return FALSE;
}
public function register($authname, $provider, array $account_data = [], $authmap_data = NULL) {
if (!empty($account_data['name'])) {
$username = $account_data['name'];
unset($account_data['name']);
}
else {
$username = $provider . '_' . $authname;
}
$authmap_event = $this->eventDispatcher
->dispatch(ExternalAuthEvents::AUTHMAP_ALTER, new ExternalAuthAuthmapAlterEvent($provider, $authname, $username, $authmap_data));
$entity_storage = $this->entityTypeManager
->getStorage('user');
$account_search = $entity_storage
->loadByProperties([
'name' => $authmap_event
->getUsername(),
]);
if ($account = reset($account_search)) {
throw new ExternalAuthRegisterException(sprintf('User could not be registered. There is already an account with username "%s"', $authmap_event
->getUsername()));
}
$account_data = array_merge([
'name' => $authmap_event
->getUsername(),
'init' => $provider . '_' . $authmap_event
->getAuthname(),
'status' => 1,
'access' => 0,
], $account_data);
$account = $entity_storage
->create($account_data);
$account
->enforceIsNew();
$account
->save();
$this->authmap
->save($account, $provider, $authmap_event
->getAuthname(), $authmap_event
->getData());
$this->eventDispatcher
->dispatch(ExternalAuthEvents::REGISTER, new ExternalAuthRegisterEvent($account, $provider, $authmap_event
->getAuthname(), $authmap_event
->getData()));
$this->logger
->notice('External registration of user %name from provider %provider and authname %authname', [
'%name' => $account
->getAccountName(),
'%provider' => $provider,
'%authname' => $authname,
]);
return $account;
}
public function loginRegister($authname, $provider, array $account_data = [], $authmap_data = NULL) {
$account = $this
->login($authname, $provider);
if (!$account) {
$account = $this
->register($authname, $provider, $account_data, $authmap_data);
return $this
->userLoginFinalize($account, $authname, $provider);
}
return $account;
}
public function userLoginFinalize(UserInterface $account, $authname, $provider) {
user_login_finalize($account);
$this->logger
->notice('External login of user %name', [
'%name' => $account
->getAccountName(),
]);
$this->eventDispatcher
->dispatch(ExternalAuthEvents::LOGIN, new ExternalAuthLoginEvent($account, $provider, $authname));
return $account;
}
public function linkExistingAccount($authname, $provider, UserInterface $account) {
if (!$this->authmap
->get($account
->id(), $provider)) {
$authmap_event = $this->eventDispatcher
->dispatch(ExternalAuthEvents::AUTHMAP_ALTER, new ExternalAuthAuthmapAlterEvent($provider, $authname, $account
->getAccountName(), NULL));
$this->authmap
->save($account, $provider, $authmap_event
->getAuthname(), $authmap_event
->getData());
}
}
}