Skip to content

Commit 974318f

Browse files
committed
Logout other sessions when password is changed
closes pterodactyl#1222
1 parent 1da05a2 commit 974318f

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1616

1717
### Changed
1818
* Attempting to upload a folder via the web file manager will now display a warning telling the user to use SFTP.
19+
* Changing your account password will now log out all other sessions that currently exist for that user.
1920

2021
## v0.7.7 (Derelict Dermodactylus)
2122
### Fixed

app/Http/Controllers/Base/AccountController.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Pterodactyl\Http\Controllers\Base;
44

55
use Pterodactyl\Models\User;
6+
use Illuminate\Auth\AuthManager;
67
use Prologue\Alerts\AlertsMessageBag;
8+
use Illuminate\Contracts\Session\Session;
79
use Pterodactyl\Http\Controllers\Controller;
810
use Pterodactyl\Services\Users\UserUpdateService;
911
use Pterodactyl\Http\Requests\Base\AccountDataFormRequest;
@@ -15,6 +17,11 @@ class AccountController extends Controller
1517
*/
1618
protected $alert;
1719

20+
/**
21+
* @var \Illuminate\Auth\SessionGuard
22+
*/
23+
protected $sessionGuard;
24+
1825
/**
1926
* @var \Pterodactyl\Services\Users\UserUpdateService
2027
*/
@@ -24,12 +31,14 @@ class AccountController extends Controller
2431
* AccountController constructor.
2532
*
2633
* @param \Prologue\Alerts\AlertsMessageBag $alert
34+
* @param \Illuminate\Auth\AuthManager $authManager
2735
* @param \Pterodactyl\Services\Users\UserUpdateService $updateService
2836
*/
29-
public function __construct(AlertsMessageBag $alert, UserUpdateService $updateService)
37+
public function __construct(AlertsMessageBag $alert, AuthManager $authManager, UserUpdateService $updateService)
3038
{
3139
$this->alert = $alert;
3240
$this->updateService = $updateService;
41+
$this->sessionGuard = $authManager->guard();
3342
}
3443

3544
/**
@@ -50,21 +59,26 @@ public function index()
5059
*
5160
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
5261
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
53-
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
5462
*/
5563
public function update(AccountDataFormRequest $request)
5664
{
57-
$data = [];
65+
// Prevent logging this specific session out when the password is changed. This will
66+
// automatically update the user's password anyways, so no need to do anything else here.
5867
if ($request->input('do_action') === 'password') {
59-
$data['password'] = $request->input('new_password');
60-
} elseif ($request->input('do_action') === 'email') {
61-
$data['email'] = $request->input('new_email');
62-
} elseif ($request->input('do_action') === 'identity') {
63-
$data = $request->only(['name_first', 'name_last', 'username']);
68+
$this->sessionGuard->logoutOtherDevices($request->input('new_password'));
69+
} else {
70+
if ($request->input('do_action') === 'email') {
71+
$data = ['email' => $request->input('new_email')];
72+
} elseif ($request->input('do_action') === 'identity') {
73+
$data = $request->only(['name_first', 'name_last', 'username']);
74+
} else {
75+
$data = [];
76+
}
77+
78+
$this->updateService->setUserLevel(User::USER_LEVEL_USER);
79+
$this->updateService->handle($request->user(), $data);
6480
}
6581

66-
$this->updateService->setUserLevel(User::USER_LEVEL_USER);
67-
$this->updateService->handle($request->user(), $data);
6882
$this->alert->success(trans('base.account.details_updated'))->flash();
6983

7084
return redirect()->route('account');

app/Http/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Illuminate\Routing\Middleware\SubstituteBindings;
2020
use Pterodactyl\Http\Middleware\AccessingValidServer;
2121
use Pterodactyl\Http\Middleware\Api\SetSessionDriver;
22+
use Illuminate\Session\Middleware\AuthenticateSession;
2223
use Illuminate\View\Middleware\ShareErrorsFromSession;
2324
use Pterodactyl\Http\Middleware\MaintenanceMiddleware;
2425
use Pterodactyl\Http\Middleware\RedirectIfAuthenticated;
@@ -64,6 +65,7 @@ class Kernel extends HttpKernel
6465
EncryptCookies::class,
6566
AddQueuedCookiesToResponse::class,
6667
StartSession::class,
68+
AuthenticateSession::class,
6769
ShareErrorsFromSession::class,
6870
VerifyCsrfToken::class,
6971
SubstituteBindings::class,

tests/Unit/Http/Controllers/Base/AccountControllerTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Mockery as m;
66
use Pterodactyl\Models\User;
7+
use Illuminate\Auth\AuthManager;
8+
use Illuminate\Auth\SessionGuard;
79
use Prologue\Alerts\AlertsMessageBag;
810
use Pterodactyl\Services\Users\UserUpdateService;
911
use Tests\Unit\Http\Controllers\ControllerTestCase;
@@ -17,6 +19,16 @@ class AccountControllerTest extends ControllerTestCase
1719
*/
1820
protected $alert;
1921

22+
/**
23+
* @var \Illuminate\Auth\AuthManager|\Mockery\Mock
24+
*/
25+
protected $authManager;
26+
27+
/**
28+
* @var \Illuminate\Auth\SessionGuard|\Mockery\Mock
29+
*/
30+
protected $sessionGuard;
31+
2032
/**
2133
* @var \Pterodactyl\Services\Users\UserUpdateService|\Mockery\Mock
2234
*/
@@ -31,6 +43,10 @@ public function setUp()
3143

3244
$this->alert = m::mock(AlertsMessageBag::class);
3345
$this->updateService = m::mock(UserUpdateService::class);
46+
$this->authManager = m::mock(AuthManager::class);
47+
$this->sessionGuard = m::mock(SessionGuard::class);
48+
49+
$this->authManager->shouldReceive('guard')->once()->andReturn($this->sessionGuard);
3450
}
3551

3652
/**
@@ -50,13 +66,11 @@ public function testIndexController()
5066
public function testUpdateControllerForPassword()
5167
{
5268
$this->setRequestMockClass(AccountDataFormRequest::class);
53-
$user = $this->generateRequestUserModel();
5469

5570
$this->request->shouldReceive('input')->with('do_action')->andReturn('password');
5671
$this->request->shouldReceive('input')->with('new_password')->once()->andReturn('test-password');
72+
$this->sessionGuard->shouldReceive('logoutOtherDevices')->once()->with('test-password')->andReturnSelf();
5773

58-
$this->updateService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_USER)->once()->andReturnNull();
59-
$this->updateService->shouldReceive('handle')->with($user, ['password' => 'test-password'])->once()->andReturn(collect());
6074
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
6175

6276
$response = $this->getController()->update($this->request);
@@ -113,6 +127,6 @@ public function testUpdateControllerForIdentity()
113127
*/
114128
private function getController(): AccountController
115129
{
116-
return new AccountController($this->alert, $this->updateService);
130+
return new AccountController($this->alert, $this->authManager, $this->updateService);
117131
}
118132
}

0 commit comments

Comments
 (0)