View source
<?php
namespace Drupal\simple_oauth\Authentication;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\simple_oauth\Entity\Oauth2TokenInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
class TokenAuthUser implements TokenAuthUserInterface {
protected $subject;
protected $token;
protected $consumer;
public function __construct(Oauth2TokenInterface $token) {
$this->consumer = $token
->get('client')->entity;
if (!($this->subject = $token
->get('auth_user_id')->entity)) {
$this->subject = $this->consumer
->get('user_id')->entity;
}
if (!$this->subject) {
throw OAuthServerException::invalidClient();
}
$this->token = $token;
}
public function getToken() {
return $this->token;
}
public function getConsumer() {
return $this->consumer;
}
public function getRoles($exclude_locked_roles = FALSE) {
return array_map(function ($item) {
return $item['target_id'];
}, $this->token
->get('scopes')
->getValue());
}
public function hasPermission($permission) {
if ((int) $this
->id() === 1) {
return TRUE;
}
return $this
->getRoleStorage()
->isPermissionInRoles($permission, $this
->getRoles());
}
protected function getRoleStorage() {
$storage = \Drupal::entityTypeManager()
->getStorage('user_role');
return $storage;
}
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $this->subject
->access($operation, $account, $return_as_object);
}
public function isAuthenticated() {
return $this->subject
->isAuthenticated();
}
public function isAnonymous() {
return $this->subject
->isAnonymous();
}
public function getPreferredLangcode($fallback_to_default = TRUE) {
return $this->subject
->getPreferredLangcode($fallback_to_default);
}
public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
return $this->subject
->getPreferredAdminLangcode($fallback_to_default);
}
public function getUsername() {
return $this->subject
->getUsername();
}
public function getAccountName() {
return $this->subject
->getAccountName();
}
public function getDisplayName() {
return $this->subject
->getDisplayName();
}
public function getEmail() {
return $this->subject
->getEmail();
}
public function getTimeZone() {
return $this->subject
->getTimeZone();
}
public function getLastAccessedTime() {
return $this->subject
->getLastAccessedTime();
}
public function getCacheContexts() {
return $this->subject
->getCacheContexts();
}
public function getCacheTags() {
return $this->subject
->getCacheTags();
}
public function getCacheMaxAge() {
return $this->subject
->getCacheMaxAge();
}
public function hasTranslationChanges() {
return $this->subject
->hasTranslationChanges();
}
public function setRevisionTranslationAffected($affected) {
return $this->subject
->setRevisionTranslationAffected($affected);
}
public function isRevisionTranslationAffected() {
return $this->subject
->isRevisionTranslationAffected();
}
public function getChangedTime() {
return $this->subject
->getChangedTime();
}
public function setChangedTime($timestamp) {
return $this->subject
->setChangedTime($timestamp);
}
public function getChangedTimeAcrossTranslations() {
return $this->subject
->getChangedTimeAcrossTranslations();
}
public function uuid() {
return $this->subject
->uuid();
}
public function id() {
return $this->subject
->id();
}
public function language() {
return $this->subject
->language();
}
public function isNew() {
return $this->subject
->isNew();
}
public function enforceIsNew($value = TRUE) {
return $this->subject
->enforceIsNew($value);
}
public function getEntityTypeId() {
return $this->subject
->getEntityTypeId();
}
public function bundle() {
return $this->subject
->bundle();
}
public function label() {
return $this->subject
->label();
}
public function urlInfo($rel = 'canonical', array $options = []) {
return $this->subject
->urlInfo($rel, $options);
}
public function url($rel = 'canonical', $options = []) {
return $this->subject
->url($rel, $options);
}
public function link($text = NULL, $rel = 'canonical', array $options = []) {
return $this->subject
->link($text, $rel, $options);
}
public function hasLinkTemplate($key) {
return $this->subject
->hasLinkTemplate($key);
}
public function uriRelationships() {
return $this->subject
->uriRelationships();
}
public static function load($id) {
return User::load($id);
}
public static function loadMultiple(array $ids = NULL) {
return User::loadMultiple($ids);
}
public static function create(array $values = []) {
return User::create($values);
}
public function save() {
return $this->subject
->save();
}
public function delete() {
$this->subject
->delete();
}
public function preSave(EntityStorageInterface $storage) {
$this->subject
->preSave($storage);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
$this->subject
->postSave($storage, $update);
}
public static function preCreate(EntityStorageInterface $storage, array &$values) {
User::preCreate($storage, $values);
}
public function postCreate(EntityStorageInterface $storage) {
return $this->subject
->postCreate($storage);
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
User::preDelete($storage, $entities);
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
User::postDelete($storage, $entities);
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
User::postLoad($storage, $entities);
}
public function createDuplicate() {
return $this->subject
->createDuplicate();
}
public function getEntityType() {
return $this->subject
->getEntityType();
}
public function referencedEntities() {
return $this->subject
->referencedEntities();
}
public function getOriginalId() {
return $this->subject
->getOriginalId();
}
public function getCacheTagsToInvalidate() {
return $this->subject
->getCacheTagsToInvalidate();
}
public function setOriginalId($id) {
return $this->subject
->setOriginalId($id);
}
public function getTypedData() {
return $this->subject
->getTypedData();
}
public function getConfigDependencyKey() {
return $this->subject
->getConfigDependencyKey();
}
public function getConfigDependencyName() {
return $this->subject
->getConfigDependencyName();
}
public function getConfigTarget() {
return $this->subject
->getConfigTarget();
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return User::baseFieldDefinitions($entity_type);
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
return User::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
}
public function hasField($field_name) {
return $this->subject
->hasField($field_name);
}
public function getFieldDefinition($name) {
return $this->subject
->getFieldDefinition($name);
}
public function getFieldDefinitions() {
return $this->subject
->getFieldDefinitions();
}
public function toArray() {
return $this->subject
->toArray();
}
public function get($field_name) {
return $this->subject
->get($field_name);
}
public function set($field_name, $value, $notify = TRUE) {
return $this->subject
->set($field_name, $value, $notify);
}
public function getFields($include_computed = TRUE) {
return $this->subject
->getFields($include_computed);
}
public function getTranslatableFields($include_computed = TRUE) {
return $this->subject
->getTranslatableFields($include_computed);
}
public function onChange($field_name) {
$this->subject
->onChange($field_name);
}
public function validate() {
return $this->subject
->validate();
}
public function isValidationRequired() {
return $this->subject
->isValidationRequired();
}
public function setValidationRequired($required) {
return $this->subject
->setValidationRequired($required);
}
public function addCacheContexts(array $cache_contexts) {
return $this->subject
->addCacheContexts($cache_contexts);
}
public function addCacheTags(array $cache_tags) {
return $this->subject
->addCacheTags($cache_tags);
}
public function mergeCacheMaxAge($max_age) {
return $this->subject
->mergeCacheMaxAge($max_age);
}
public function addCacheableDependency($other_object) {
return $this->subject
->addCacheableDependency($other_object);
}
public function isNewRevision() {
return $this->subject
->isNewRevision();
}
public function setNewRevision($value = TRUE) {
$this->subject
->setNewRevision($value);
}
public function getRevisionId() {
return $this->subject
->getRevisionId();
}
public function isDefaultRevision($new_value = NULL) {
return $this->subject
->isDefaultRevision($new_value);
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
$this->subject
->preSaveRevision($storage, $record);
}
public function isDefaultTranslation() {
return $this->subject
->isDefaultTranslation();
}
public function getTranslationLanguages($include_default = TRUE) {
return $this->subject
->getTranslationLanguages($include_default);
}
public function getTranslation($langcode) {
return $this->subject
->getTranslation($langcode);
}
public function getUntranslated() {
return $this->subject
->getUntranslated();
}
public function hasTranslation($langcode) {
return $this->subject
->hasTranslation($langcode);
}
public function addTranslation($langcode, array $values = []) {
return $this->subject
->addTranslation($langcode, $values);
}
public function removeTranslation($langcode) {
$this->subject
->removeTranslation($langcode);
}
public function isTranslatable() {
return $this->subject
->isTranslatable();
}
public function hasRole($rid) {
return in_array($rid, $this
->getRoles());
}
public function addRole($rid) {
$this->subject
->addRole($rid);
}
public function removeRole($rid) {
$this->subject
->removeRole($rid);
}
public function setUsername($username) {
return $this->subject
->setUsername($username);
}
public function getPassword() {
return $this->subject
->getPassword();
}
public function setPassword($password) {
return $this->subject
->setPassword($password);
}
public function setEmail($mail) {
return $this->subject
->setEmail($mail);
}
public function getCreatedTime() {
return $this->subject
->getCreatedTime();
}
public function setLastAccessTime($timestamp) {
return $this->subject
->setLastAccessTime($timestamp);
}
public function getLastLoginTime() {
return $this->subject
->getLastLoginTime();
}
public function setLastLoginTime($timestamp) {
return $this->subject
->setLastLoginTime($timestamp);
}
public function isActive() {
return $this->subject
->isActive();
}
public function isBlocked() {
return $this->subject
->isBlocked();
}
public function activate() {
return $this->subject
->activate();
}
public function block() {
return $this->subject
->block();
}
public function getInitialEmail() {
return $this->subject
->getInitialEmail();
}
public function setExistingPassword($password) {
return $this->subject
->setExistingPassword($password);
}
public function checkExistingPassword(UserInterface $account_unchanged) {
return $this->subject
->checkExistingPassword($account_unchanged);
}
public function getIterator() {
throw new \Exception('Invalid use of getIterator in token authentication.');
}
public function toUrl($rel = 'canonical', array $options = []) {
$this->subject
->toUrl($rel, $options);
}
public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
return $this->subject
->toLink($text, $rel, $options);
}
public function isNewTranslation() {
return $this->subject
->isNewTranslation();
}
public function getLoadedRevisionId() {
return $this->subject
->getLoadedRevisionId();
}
public function updateLoadedRevisionId() {
return $this->subject
->updateLoadedRevisionId();
}
public function wasDefaultRevision() {
return $this->subject
->wasDefaultRevision();
}
public function isLatestRevision() {
return $this->subject
->isLatestRevision();
}
public function isLatestTranslationAffectedRevision() {
return $this->subject
->isLatestTranslationAffectedRevision();
}
public function isRevisionTranslationAffectedEnforced() {
return $this->subject
->isRevisionTranslationAffectedEnforced();
}
public function setRevisionTranslationAffectedEnforced($enforced) {
return $this->subject
->setRevisionTranslationAffectedEnforced($enforced);
}
public function isDefaultTranslationAffectedOnly() {
return $this->subject
->isDefaultTranslationAffectedOnly();
}
public function setSyncing($status) {
$this->subject
->setSyncing($status);
return $this;
}
public function isSyncing() {
return $this->subject
->isSyncing();
}
}