Skip to content

Commit

Permalink
PROD-31314: Implement "user is pending" event for the EDA
Browse files Browse the repository at this point in the history
  • Loading branch information
tregismoreira authored and denis-getopensocial committed Nov 20, 2024
1 parent ca83106 commit b1f8fb6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
19 changes: 14 additions & 5 deletions modules/social_features/social_user/social_user.module
Original file line number Diff line number Diff line change
Expand Up @@ -727,21 +727,30 @@ function social_user_profile_insert(ProfileInterface $profile): void {
if ($user instanceof UserInterface) {
$current_request = \Drupal::requestStack()->getCurrentRequest();
$user_settings = \Drupal::config('user.settings');
$trigger = FALSE;

// If user is created from the route `user.register`.
if ($current_request && $current_request->get('_route') == 'user.register') {
// Admin approval is required.
if ($user_settings->get('register') != 'visitors_admin_approval') {
$trigger = TRUE;
$eda_handler = 'userCreate';
}
else {
$eda_handler = 'userPending';
}
}
else {
$trigger = TRUE;
$eda_handler = 'userCreate';
}

if ($trigger && !str_starts_with($user->getAccountName(), '__DELETED_USER')) {
\Drupal::service('social_user.eda_handler')->userCreate($user);
// Call the EDA method dynamically if the account name doesn't start
// with '__DELETED_USER'.
if (!str_starts_with($user->getAccountName(), '__DELETED_USER')) {
$eda_service = \Drupal::service('social_user.eda_handler');

// Check if the method exists before calling it.
if (method_exists($eda_service, $eda_handler)) {
$eda_service->{$eda_handler}($user);
}
}
}
}
Expand Down
33 changes: 30 additions & 3 deletions modules/social_features/social_user/src/EdaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public function userCreate(UserInterface $user): void {
);
}

/**
* Pending user handler.
*/
public function userPending(UserInterface $user): void {
$this->dispatch(
topic_name: $this->topicName,
event_type: "{$this->namespace}.cms.user.pending",
user: $user,
);
}

/**
* Profile update handler.
*/
Expand Down Expand Up @@ -222,13 +233,29 @@ public function fromEntity(UserInterface $user, string $event_type): CloudEvent
$organization = $profile->get('field_profile_organization')->value;
}

// Determine status value.
if ($user->isActive()) {
$status = 'active';
}
else {
$user_settings = $this->configFactory->get('user.settings');

// If admin approval is required.
if ($user_settings->get('register') == 'visitors_admin_approval') {
$status = 'pending';
}
else {
$status = 'blocked';
}
}

// Apply variant of the payload to some event types.
if (preg_match('/\.cms\.user\.(login|logout|block|unblock|delete)$/', $event_type)) {
$user_data = new UserEventDataLite(
id: $user->get('uuid')->value,
created: DateTime::fromTimestamp($user->getCreatedTime())->toString(),
updated: DateTime::fromTimestamp($user->getChangedTime())->toString(),
status: $user->isActive() ? 'active' : 'blocked',
status: $status,
displayName: $user->getDisplayName(),
roles: array_values($user->getRoles()),
timezone: $user->getTimeZone(),
Expand All @@ -240,7 +267,7 @@ public function fromEntity(UserInterface $user, string $event_type): CloudEvent
$user_data = new UserEventEmailData(
created: DateTime::fromTimestamp($user->getCreatedTime())->toString(),
updated: DateTime::fromTimestamp($user->getChangedTime())->toString(),
status: $user->isActive() ? 'active' : 'blocked',
status: $status,
displayName: $user->getDisplayName(),
email: (string) $user->getEmail(),
roles: array_values($user->getRoles()),
Expand All @@ -254,7 +281,7 @@ public function fromEntity(UserInterface $user, string $event_type): CloudEvent
id: $user->get('uuid')->value,
created: DateTime::fromTimestamp($user->getCreatedTime())->toString(),
updated: DateTime::fromTimestamp($user->getChangedTime())->toString(),
status: $user->isActive() ? 'active' : 'blocked',
status: $status,
displayName: $user->getDisplayName(),
firstName: $first_name ?? '',
lastName: $last_name ?? '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ protected function setUp(): void {
$languageManagerMock->getCurrentLanguage()->willReturn($languageMock->reveal());

// Mock the configuration for `social_eda.settings.namespaces`.
$configMock = $this->prophesize(ConfigInterface::class);
$configMock->get('namespace')->willReturn('com.getopensocial');
$configSocialEdaMock = $this->prophesize(ConfigInterface::class);
$configSocialEdaMock->get('namespace')->willReturn('com.getopensocial');

// Mock the configuration for `user.settings.register`.
$configUserSettingsMock = $this->prophesize(ConfigInterface::class);
$configUserSettingsMock->get('register')->willReturn('visitors_admin_approval');

$configFactoryMock = $this->prophesize(ConfigFactoryInterface::class);
$configFactoryMock->get('social_eda.settings')->willReturn($configMock->reveal());
$configFactoryMock->get('social_eda.settings')->willReturn($configSocialEdaMock->reveal());
$configFactoryMock->get('user.settings')->willReturn($configUserSettingsMock->reveal());
$this->configFactory = $configFactoryMock->reveal();

// Set up Drupal's container.
Expand Down Expand Up @@ -289,6 +294,33 @@ public function testUserCreate(): void {
$this->assertEquals('com.getopensocial.cms.user.create', $event->getType());
}

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

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

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

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

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

/**
* Test the profileUpdate() method.
*
Expand Down

0 comments on commit b1f8fb6

Please sign in to comment.