View source
<?php
namespace Drupal\simple_oauth\Entity;
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\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\UserInterface;
class Oauth2Client extends ContentEntityBase implements Oauth2ClientInterface {
use EntityChangedTrait;
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
if (!$translation
->getOwner()) {
$translation
->setOwnerId(0);
}
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['owner_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Authored by'))
->setDescription(new TranslatableMarkup('The username of the client author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\simple_oauth\\Entity\\Oauth2Client::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'hidden',
])
->setDisplayConfigurable('form', TRUE);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Label'))
->setDescription(new TranslatableMarkup('The client label.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
$fields['secret'] = BaseFieldDefinition::create('password')
->setLabel(new TranslatableMarkup('Secret'))
->setDescription(new TranslatableMarkup('The secret key of this client (hashed).'));
$fields['confidential'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Is Confidential?'))
->setDescription(new TranslatableMarkup('A boolean indicating whether the client secret needs to be validated or not.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'boolean',
'weight' => 3,
])
->setDisplayOptions('form', [
'weight' => 3,
])
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE);
$fields['roles'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Scopes'))
->setDescription(new TranslatableMarkup('The roles for this Client. OAuth2 scopes are implemented as Drupal roles.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user_role')
->setSetting('handler', 'default')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setTranslatable(FALSE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => 5,
])
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 5,
]);
return $fields;
}
public function getOwner() {
return $this
->get('owner_id')->entity;
}
public function getOwnerId() {
return $this
->getEntityKey('owner_id');
}
public function setOwnerId($uid) {
$this
->set('owner_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('owner_id', $account
->id());
return $this;
}
public function getDefaultUser() {
return $this
->get('user_id')->entity;
}
public function setDefaultUser(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getSecret() {
return $this
->get('secret')->value;
}
public function setSecret($secret) {
$this
->get('secret')->value = $secret;
return $this;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public function isConfidential() {
return (bool) $this
->get('confidential')->value;
}
}