Skip to content
Snippets Groups Projects
Commit 0c81adc1 authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3440494 by eiriksm: Ensure the user gets their team when they ended up having to log in

parent 0e68d2ac
No related branches found
No related tags found
1 merge request!46Add a subscriber to fix that
Pipeline #144971 passed with warnings
<?php
declare(strict_types=1);
namespace Drupal\violinist_teams\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\UserInterface;
use Drupal\violinist_teams\Controller\InviteController;
use Drupal\violinist_teams\TeamNode;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* A response subscriber to check if we have outstanding invites.
*/
final class ResponseInviteSubscriber implements EventSubscriberInterface {
/**
* Constructs a ResponseInviteSubscriber object.
*/
public function __construct(
private readonly SessionInterface $session,
private readonly EntityTypeManagerInterface $entityTypeManager,
private AccountProxyInterface $currentUser,
) {}
/**
* Kernel response event handler.
*/
public function onKernelResponse(ResponseEvent $event): void {
if (!$this->currentUser->id()) {
return;
}
$request = $event->getRequest();
$data = $request->getSession()->get(InviteController::INVITE_DATA);
if (!$data) {
return;
}
// No matter what the result is here, we want to remove the session data at
// this point.
$request->getSession()->remove(InviteController::INVITE_DATA);
if (empty($data['team_id'])) {
return;
}
if (empty($data['membership_type'])) {
return;
}
$team = $this->entityTypeManager->getStorage('node')->load($data['team_id']);
if (!$team instanceof TeamNode) {
return;
}
$actual_user_object = $this->entityTypeManager->getStorage('user')->load($this->currentUser->id());
if (!$actual_user_object instanceof UserInterface) {
return;
}
$membership_type = $data['membership_type'];
// Add them to the team. Based on what role we have in the parameter.
if ($membership_type === 'admin') {
$team->appendAdmin($actual_user_object)->save();
}
else {
$team->appendMember($actual_user_object)->save();
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
KernelEvents::RESPONSE => ['onKernelResponse'],
];
}
}
......@@ -11,3 +11,12 @@ services:
autowire: true
tags:
- { name: access_check, applies_to: _violinist_teams_access }
violinist_teams.event_subscriber:
class: Drupal\violinist_teams\EventSubscriber\ResponseInviteSubscriber
arguments:
- '@session'
- '@entity_type.manager'
- '@current_user'
tags:
- { name: event_subscriber }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment