Commit 09f05b70 authored by Bojan Bogdanovic's avatar Bojan Bogdanovic Committed by Mateu Aguiló Bosch
Browse files

Issue #3310801 by bojan_dev, m.stenta, e0ipso: Add client_id field to the consumer entity

parent 1b812ca5
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
 */
function consumers_install() {
  Consumer::create([
    'client_id' => 'default_consumer',
    'label' => 'Default Consumer',
    'description' => 'This is the default consumer. This was created programmatically when the Consumers module was first installed. Feel free to edit, or delete this.',
    'is_default' => TRUE,
@@ -137,3 +138,35 @@ function consumers_update_8107() {
    $definition_update_manager->installFieldStorageDefinition('image', 'consumer', 'consumer', $image_field);
  }
}

/**
 * Add field 'client_id'.
 */
function consumers_update_8108() {
  $field_definition = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Client ID'))
    ->setDescription(new TranslatableMarkup('The client ID associated with this consumer.'))
    ->setRequired(TRUE)
    ->setRevisionable(TRUE)
    ->addConstraint('UniqueField')
    ->setSetting('max_length', 255)
    ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -4,
    ])
    ->setDisplayConfigurable('form', TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('client_id', 'consumer', 'consumers', $field_definition);
}

/**
 * Set uuid as client_id for existing consumers.
 */
function consumers_update_8109() {
  $consumers = \Drupal::entityTypeManager()->getStorage('consumer')->loadMultiple();
  foreach ($consumers as $consumer) {
    $consumer->set('client_id', $consumer->uuid());
    $consumer->save();
  }
}
+6 −1
Original line number Diff line number Diff line
@@ -14,13 +14,18 @@ use Drupal\Core\Access\AccessResult;
 */
class AccessControlHandler extends EntityAccessControlHandler {

  /**
   * The entity id.
   *
   * @var string
   */
  public static $name = 'consumer';

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
    /** @var \Drupal\consumers\Entity\Consumer $entity */
    /** @var \Drupal\consumers\Entity\ConsumerInterface $entity */
    $admin_permission = $this->entityType->getAdminPermission();
    if ($account->hasPermission($admin_permission)) {
      return AccessResult::allowed()->cachePerPermissions();
+3 −2
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ namespace Drupal\consumers;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Url;

/**
 * Defines a class to build a listing of Access Token entities.
@@ -15,6 +14,7 @@ class ConsumerListBuilder extends EntityListBuilder {
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['client_id'] = $this->t('Client ID');
    $header['uuid'] = $this->t('UUID');
    $header['label'] = $this->t('Label');
    $header['is_default'] = $this->t('Is Default?');
@@ -28,7 +28,8 @@ class ConsumerListBuilder extends EntityListBuilder {
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    /* @var $entity \Drupal\consumers\Entity\Consumer */
    /** @var \Drupal\consumers\Entity\ConsumerInterface $entity */
    $row['client_id'] = $entity->getClientId();
    $row['uuid'] = $entity->uuid();
    $row['label'] = $entity->toLink();
    $ops = [
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ class ConsumerStorage extends SqlContentEntityStorage {
   * {@inheritdoc}
   */
  public function restore(EntityInterface $entity) {
    /** @var \Drupal\consumers\Entity\Consumer $entity */
    /** @var \Drupal\consumers\Entity\ConsumerInterface $entity */
    // Special handling for the secret field added by simple_oauth, make sure that it is not hashed again.
    if ($entity->hasField('secret')) {
      $entity->get('secret')->pre_hashed = TRUE;
+22 −4
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\EntityOwnerTrait;

/**
@@ -55,7 +54,7 @@ use Drupal\user\EntityOwnerTrait;
 *   }
 * )
 */
class Consumer extends ContentEntityBase implements EntityOwnerInterface {
class Consumer extends ContentEntityBase implements ConsumerInterface {

  use EntityChangedTrait;
  use EntityOwnerTrait;
@@ -96,6 +95,18 @@ class Consumer extends ContentEntityBase implements EntityOwnerInterface {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields += static::ownerBaseFieldDefinitions($entity_type);

    $fields['client_id'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Client ID'))
      ->setDescription(new TranslatableMarkup('The client ID associated with this consumer. This is an arbitrary unique field, like a machine name.'))
      ->setRequired(TRUE)
      ->setRevisionable(TRUE)
      ->addConstraint('UniqueField')
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Label'))
      ->setDescription(new TranslatableMarkup('The consumer label.'))
@@ -209,7 +220,7 @@ class Consumer extends ContentEntityBase implements EntityOwnerInterface {
        static::setDefaultTo(FALSE),
        $default_entities
      );
      $invalid_entities = array_filter($default_entities, function (Consumer $consumer) {
      $invalid_entities = array_filter($default_entities, function (ConsumerInterface $consumer) {
        return !$consumer->access('update', NULL, TRUE)->isAllowed();
      });
      if (count($invalid_entities)) {
@@ -228,10 +239,17 @@ class Consumer extends ContentEntityBase implements EntityOwnerInterface {
   * @return \Closure
   */
  protected static function setDefaultTo($value) {
    return function (Consumer $consumer) use ($value) {
    return function (ConsumerInterface $consumer) use ($value) {
      $consumer->set('is_default', $value);
      return $consumer;
    };
  }

  /**
   * {@inheritdoc}
   */
  public function getClientId(): string {
    return $this->get('client_id')->value;
  }

}
Loading