View source
<?php
namespace Drupal\access_unpublished\Entity;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\access_unpublished\AccessTokenInterface;
use Drupal\Core\Site\Settings;
use Drupal\user\UserInterface;
class AccessToken extends ContentEntityBase implements AccessTokenInterface {
use EntityChangedTrait;
const DEFAULT_EXPIRATION_PERIOD = 172800;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Access token entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Access token entity.'))
->setReadOnly(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Access token entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\access_unpublished\\Entity\\AccessToken::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['value'] = BaseFieldDefinition::create('string')
->setLabel(t('Token'))
->setDescription(t('The token value.'))
->setSettings([
'max_length' => 128,
'text_processing' => 0,
])
->setRequired(TRUE)
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'hidden',
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['expire'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expire'))
->setDefaultValueCallback(__CLASS__ . '::defaultExpiration')
->setDescription(t('The time when the token expires.'))
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 1,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => 1,
])
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Entity type'))
->setDescription(t('The type of token belongs to.'))
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$fields['entity_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Entity id'))
->setDescription(t('The id of token belongs to.'))
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
public static function defaultExpiration() {
$defaultDuration = \Drupal::config('access_unpublished.settings')
->get('duration') ?? static::DEFAULT_EXPIRATION_PERIOD;
if ($defaultDuration === -1) {
return [
$defaultDuration,
];
}
else {
return [
\Drupal::time()
->getRequestTime() + $defaultDuration,
];
}
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this
->get('value')
->isEmpty()) {
$value = Crypt::hmacBase64(Json::encode($this
->normalize()), Settings::getHashSalt());
$this
->set('value', $value);
}
}
protected function normalize() {
$keys = [
'entity_type',
'expire',
'created',
'entity_id',
];
$values = array_map(function ($item) {
return $this
->get($item)
->getValue();
}, $keys);
return $values;
}
public function isExpired() {
return $this
->get('expire')->value > 0 && $this
->get('expire')->value < \Drupal::time()
->getRequestTime();
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public function getHost() {
if ($this
->get('entity_type')->value && $this
->get('entity_id')->value) {
try {
return \Drupal::entityTypeManager()
->getStorage($this
->get('entity_type')->value)
->load($this
->get('entity_id')->value);
} catch (\Exception $exception) {
return NULL;
}
}
}
}