Allow adding custom CAS Attributes

Created on 22 November 2017, over 6 years ago
Updated 8 December 2023, 7 months ago

The CAS attribute include some basic fields but we would like to make it more comprehensive and allow altering the data providing additional CAS attributes to clients.

✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

🇧🇪Belgium jeroenvl

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France FMB Perpinyà, Catalonia, EU

    For those wondering how to use this feature: give your own event a priority lower than 100 (CASAttributeAlterEventSubscriber), get existing attributes ($event->getAttributes()), and then add your own before passing them to $event->setAttributes().

  • 🇦🇺Australia ELC

    @FMB Would you be willing to provide a working example?

    There is a request on #3311038: How to get term name instead of tid in the released attributes? →

    The other possibility was https://www.drupal.org/project/cas_attributes →

  • 🇫🇷France FMB Perpinyà, Catalonia, EU

    @FMB Would you be willing to provide a working example?

    Something like that:

    
    namespace Drupal\mymodule\EventSubscriber;
    
    use Drupal\cas_server\Event\CASAttributesAlterEvent;
    use Drupal\Core\Database\Connection;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    /**
     * Pass profile UUIDs as CAS attributes.
     *
     * @see \Drupal\cas_server\EventSubscriber\CASAttributeAlterEventSubscriber
     */
    class CasAttributes implements EventSubscriberInterface {
    
      /**
       * Connection to database.
       *
       * @var \Drupal\Core\Database\Connection
       */
      protected Connection $database;
    
      /**
       * Constructor.
       *
       * @param \Drupal\Core\Database\Connection $database
       *   Database connection.
       */
      public function __construct(Connection $database) {
        $this->database = $database;
      }
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents() {
        return [
          CASAttributesAlterEvent::CAS_ATTRIBUTES_ALTER_EVENT => [
            'onCasAttributesAlter',
            // Lower than CASAttributeAlterEventSubscriber, so we can add
            // attributes.
            50,
          ],
        ];
      }
    
      /**
       * Actually alter CAS attributes.
       */
      public function onCasAttributesAlter(CASAttributesAlterEvent $event) {
        $attributes = $event->getAttributes();
        $bundles = ['bundle1', 'bundle2'];
        $uuids = $this->database
          ->select('profile', 'p')
          ->fields('p', ['type', 'uuid'])
          ->condition('uid', $event->getUser()->id())
          ->condition('type', $bundles, 'IN')
          ->execute()
          ->fetchAllKeyed();
        $attributes += $uuids;
        $event->setAttributes($attributes);
      }
    
    }
    
    
    The other possibility was https://www.drupal.org/project/cas_attributes →

    This module is installed on the client side. My purpose here was to pass additional CAS attributes from the server to its clients.

Production build 0.69.0 2024