Skip to content

Commit

Permalink
Raise Impersonated event (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Morland committed May 8, 2020
1 parent 111be13 commit 4c509fd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"replace": {
"flagrow/impersonate": "*"
},
"suggest": {
"fof/moderator-notes": "Allows audit logging when users are impersonated"
},
"autoload": {
"psr-4": {
"FoF\\Impersonate\\": "src/"
Expand Down
11 changes: 9 additions & 2 deletions src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Flarum\Http\SessionAuthenticator;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\User;
use FoF\Impersonate\Events\Impersonated;
use Illuminate\Events\Dispatcher;
use Illuminate\Contracts\Session\Session;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -28,13 +30,15 @@ class LoginController implements RequestHandlerInterface

protected $authenticator;
protected $rememberer;
protected $bus;

public $serializer = UserSerializer::class;

public function __construct(SessionAuthenticator $authenticator, Rememberer $rememberer)
public function __construct(SessionAuthenticator $authenticator, Rememberer $rememberer, Dispatcher $bus)
{
$this->authenticator = $authenticator;
$this->rememberer = $rememberer;
$this->bus = $bus;
}

/**
Expand All @@ -45,21 +49,24 @@ public function __construct(SessionAuthenticator $authenticator, Rememberer $rem
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = $request->getAttribute('actor');
$id = array_get($request->getQueryParams(), 'id');

/**
* @var $user User
*/
$user = User::findOrFail($id);

$this->assertCan($request->getAttribute('actor'), 'fofCanImpersonate', $user);
$this->assertCan($actor, 'fofCanImpersonate', $user);

/**
* @var $session Session
*/
$session = $request->getAttribute('session');
$this->authenticator->logIn($session, $user->id);

$this->bus->dispatch(new Impersonated($actor, $user));

return $this->rememberer->forget(new JsonResponse(true));
}
}
33 changes: 33 additions & 0 deletions src/Events/Impersonated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of fof/impersonate.
*
* Copyright (c) 2020 FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Impersonate\Events;

use Flarum\User\User;

class Impersonated
{
/**
* @var User
*/
public $actor;

/**
* @var User
*/
public $user;

public function __construct(User $actor, User $user)
{
$this->actor = $actor;
$this->user = $user;
}
}

0 comments on commit 4c509fd

Please sign in to comment.