Skip to content

Commit

Permalink
PROD-31206 - Implement an EDA event for when a user changes its email…
Browse files Browse the repository at this point in the history
… address
  • Loading branch information
nkoporec authored and ribel committed Nov 13, 2024
1 parent 2ad067d commit ef210a5
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
85 changes: 85 additions & 0 deletions modules/social_features/social_user/asyncapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,87 @@ channels:
type: string
format: uri
description: Canonical URL of the actor user.
userEmailUpdate:
address: com.getopensocial.cms.user.settings.email
messages:
userEmailUpdate:
payload:
allOf:
- $ref: '#/components/schemas/cloudEventsSchema'
- type: object
properties:
data:
type: object
properties:
user:
type: object
properties:
created:
type: string
format: date-time
description: Creation time of the user
updated:
type: string
format: date-time
description: Last update time of the user
status:
type: string
description: Status of the user
displayName:
type: string
description: Display name of the user
email:
type: string
format: email
description: Email of the user
roles:
type: array
items:
type: string
description: List of user roles
timezone:
type: string
description: Timezone of the user
language:
type: string
description: Language preference of the user
href:
type: object
properties:
canonical:
type: string
format: uri
description: Canonical URL for the user profile
actor:
type: object
properties:
application:
type: object
nullable: true
properties:
id:
type: string
description: The UUID of the application.
name:
type: string
description: The name of the application.
user:
type: object
nullable: true
properties:
id:
type: string
description: The UUID of the actor user.
displayName:
type: string
description: The display name of the actor user.
href:
type: object
properties:
canonical:
type: string
format: uri
description: Canonical URL of the actor user.

operations:
onUserCreate:
Expand All @@ -695,3 +776,7 @@ operations:
action: 'receive'
channel:
$ref: '#/channels/userDelete'
onUserEmailUpdate:
action: 'receive'
channel:
$ref: '#/channels/userEmailUpdate'
6 changes: 6 additions & 0 deletions modules/social_features/social_user/social_user.module
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,13 @@ function social_user_user_update(EntityInterface $entity): void {
elseif ($entity->isBlocked() && $original->isActive()) {
\Drupal::service('social_user.eda_handler')->userBlock($entity);
}

// Email change.
if ($original->getEmail() != $entity->getEmail()) {
\Drupal::service('social_user.eda_handler')->userEmailUpdate($entity);
}
}

}

/**
Expand Down
24 changes: 24 additions & 0 deletions modules/social_features/social_user/src/EdaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Drupal\social_eda\Types\User;
use Drupal\social_user\Event\UserEventData;
use Drupal\social_user\Event\UserEventDataLite;
use Drupal\social_user\Event\UserEventEmailData;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\RequestStack;

Expand Down Expand Up @@ -128,6 +129,15 @@ public function userUnblock(UserInterface $user): void {
$this->dispatch($topic_name, $event_type, $user);
}

/**
* User unblock handler.
*/
public function userEmailUpdate(UserInterface $user): void {
$event_type = 'com.getopensocial.cms.user.settings.email';
$topic_name = 'com.getopensocial.cms.user.settings.email';
$this->dispatch($topic_name, $event_type, $user);
}

/**
* User delete handler.
*/
Expand Down Expand Up @@ -184,6 +194,20 @@ public function fromEntity(UserInterface $user, string $event_type): CloudEvent
);
break;

case 'com.getopensocial.cms.user.settings.email':
$user_data = new UserEventEmailData(
created: DateTime::fromTimestamp($user->getCreatedTime())->toString(),
updated: DateTime::fromTimestamp($user->getChangedTime())->toString(),
status: $user->isActive() ? 'active' : 'blocked',
displayName: $user->getDisplayName(),
email: (string) $user->getEmail(),
roles: array_values($user->getRoles()),
timezone: $user->getTimeZone(),
language: $user->getPreferredLangcode(),
href: Href::fromEntity($user),
);
break;

default:
$user_data = new UserEventData(
id: $user->get('uuid')->value,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Drupal\social_user\Event;

use Drupal\social_eda\Types\Href;

/**
* Contains data about an Open Social user.
*/
class UserEventEmailData {

/**
* {@inheritDoc}
*/
public function __construct(
public readonly string $created,
public readonly string $updated,
public readonly string $status,
public readonly string $displayName,
public readonly string $email,
public readonly array $roles,
public readonly string $timezone,
public readonly string $language,
public readonly Href $href,
) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ public function testUserDelete(): void {
$this->assertEquals('com.getopensocial.cms.user.delete', $event->getType());
}

/**
* Test the userEmailUpdate() method.
*
* @covers ::userEmailUpdate
*/
public function testUserEmailUpdate(): void {
// Create the handler instance.
$handler = $this->getMockedHandler();

// Create the event object.
$event = $handler->fromEntity($this->user, 'com.getopensocial.cms.user.settings.email');

// Expect the dispatch method in the dispatcher to be called.
$this->dispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo('com.getopensocial.cms.user.settings.email'),
$this->equalTo($event)
);

// Call the userEmailUpdate method.
$handler->userEmailUpdate($this->user);

// Assert that the correct event is dispatched.
$this->assertEquals('com.getopensocial.cms.user.settings.email', $event->getType());
}

/**
* Returns a mocked handler with dependencies injected.
*
Expand Down

0 comments on commit ef210a5

Please sign in to comment.