SchulIT /
idp
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Security\Session; |
||||
| 4 | |||||
| 5 | use App\Entity\User; |
||||
| 6 | use DateTimeImmutable; |
||||
| 7 | use Doctrine\DBAL\Connection; |
||||
| 8 | use Doctrine\DBAL\Types\Types; |
||||
| 9 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||||
| 10 | use Symfony\Component\HttpKernel\Event\FinishRequestEvent; |
||||
| 11 | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
||||
| 12 | |||||
| 13 | class HandleActiveSessionSubscriber implements EventSubscriberInterface { |
||||
| 14 | |||||
| 15 | private ?ActiveSession $sessionToSave = null; |
||||
| 16 | |||||
| 17 | public function __construct(private readonly Connection $connection) { } |
||||
| 18 | |||||
| 19 | public function onKernelTerminate(FinishRequestEvent $event): void { |
||||
| 20 | if($this->sessionToSave === null) { |
||||
| 21 | return; |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | $this->connection->insert('session_user', [ |
||||
| 25 | 'user_id' => $this->sessionToSave->userId, |
||||
| 26 | 'session_id' => $event->getRequest()->getSession()->getId(), // use current session id as it seems to be changed during the request... |
||||
| 27 | 'user_agent' => mb_convert_encoding($this->sessionToSave->userAgent, 'UTF-8', 'UTF-8'), // somehow: if the user agent contains non unicode characters, it breaks :-/ |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 28 | 'started_at' => $this->sessionToSave->startedAt, |
||||
| 29 | 'ip_address' => $this->sessionToSave->ipAddress |
||||
| 30 | ], [ |
||||
| 31 | Types::INTEGER, |
||||
| 32 | Types::STRING, |
||||
| 33 | Types::TEXT, |
||||
| 34 | Types::DATETIME_IMMUTABLE, |
||||
| 35 | Types::STRING |
||||
| 36 | ]); |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | public function onInteractiveLogin(InteractiveLoginEvent $event): void { |
||||
| 40 | $user = $event->getAuthenticationToken()?->getUser(); |
||||
| 41 | |||||
| 42 | if(!$user instanceof User) { |
||||
| 43 | return; |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | /* |
||||
| 47 | * We cannot insert the data here because: |
||||
| 48 | * |
||||
| 49 | * (a) there is a lock on the sessions table which seem to prevent foreign key checks |
||||
| 50 | * (b) the session id changes during the request (which also would fail the foreign key check) |
||||
| 51 | */ |
||||
| 52 | |||||
| 53 | $this->sessionToSave = new ActiveSession( |
||||
| 54 | $user->getId(), |
||||
|
0 ignored issues
–
show
It seems like
$user->getId() can also be of type null; however, parameter $userId of App\Security\Session\ActiveSession::__construct() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 55 | $event->getRequest()->getSession()->getId(), |
||||
| 56 | $event->getRequest()->headers->get('User-Agent'), |
||||
| 57 | new DateTimeImmutable(), |
||||
| 58 | $event->getRequest()->getClientIp(), |
||||
| 59 | true |
||||
| 60 | ); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | public static function getSubscribedEvents(): array { |
||||
| 64 | return [ |
||||
| 65 | InteractiveLoginEvent::class => 'onInteractiveLogin', |
||||
| 66 | FinishRequestEvent::class => 'onKernelTerminate' |
||||
| 67 | ]; |
||||
| 68 | } |
||||
| 69 | } |