Skip to content

Commit 8deac9c

Browse files
chore: Refactor getting the session
1 parent f6b1331 commit 8deac9c

File tree

8 files changed

+19
-36
lines changed

8 files changed

+19
-36
lines changed

src/auth/CurrentUser.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ public static function authenticate(string $session_token, string $scope): ?mode
146146
return $user;
147147
}
148148

149+
/**
150+
* Return whether the user is logged-in or not.
151+
*/
152+
public static function isLoggedIn(): bool
153+
{
154+
return self::$instance !== null && self::$session !== null;
155+
}
156+
149157
/**
150158
* Return the logged-in user if any.
151159
*/
@@ -172,10 +180,17 @@ public static function require(): models\User
172180
}
173181

174182
/**
175-
* Return the current session if any.
183+
* Return the current session.
184+
*
185+
* @throws MissingCurrentUserError
186+
* If the user is not logged-in.
176187
*/
177-
public static function session(): ?models\Session
188+
public static function session(): models\Session
178189
{
190+
if (!self::$session) {
191+
throw new MissingCurrentUserError('Session does not exist.');
192+
}
193+
179194
return self::$session;
180195
}
181196

@@ -190,11 +205,6 @@ public static function session(): ?models\Session
190205
public static function requireConfirmedPassword(): void
191206
{
192207
$session = self::session();
193-
194-
if (!$session) {
195-
throw new MissingCurrentUserError();
196-
}
197-
198208
if (!$session->isPasswordConfirmed()) {
199209
throw new PasswordNotConfirmedError();
200210
}

src/controllers/my/Security.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function update(Request $request): Response
8282
models\Token::delete($user->reset_token);
8383
}
8484
$session = auth\CurrentUser::session();
85-
assert($session !== null);
8685
models\Session::deleteByUserId($user->id, $session->id);
8786
}
8887

@@ -147,8 +146,6 @@ public function confirm(Request $request): Response
147146
}
148147

149148
$session = auth\CurrentUser::session();
150-
assert($session !== null);
151-
152149
$session->confirmPassword();
153150

154151
return Response::found($form->redirect_to);

src/controllers/my/Sessions.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public function index(Request $request): Response
3333
auth\CurrentUser::requireConfirmedPassword();
3434

3535
$session = auth\CurrentUser::session();
36-
assert($session !== null);
3736

3837
$sessions = models\Session::listBy([
3938
'user_id' => $user->id,
@@ -86,7 +85,6 @@ public function delete(Request $request): Response
8685
$response = Response::redirect('sessions');
8786

8887
$current_session = auth\CurrentUser::session();
89-
assert($current_session !== null);
9088

9189
if ($session->id === $current_session->id) {
9290
auth\CurrentUser::deleteSession();

src/forms/BaseForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function csrfErrorMessage(): string
2626

2727
public function csrfSessionId(): string
2828
{
29-
$session = auth\CurrentUser::session();
30-
if ($session) {
29+
if (auth\CurrentUser::isLoggedIn()) {
30+
$session = auth\CurrentUser::session();
3131
return $session->id;
3232
} else {
3333
return $this->_csrfSessionId();

tests/auth/CurrentUserTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public function testAuthenticate(): void
7878
$this->assertNotNull($current_user);
7979
$this->assertSame($user->id, $current_user->id);
8080
$current_session = CurrentUser::session();
81-
$this->assertNotNull($current_session);
8281
$this->assertSame($session->id, $current_session->id);
8382
}
8483

@@ -98,8 +97,6 @@ public function testAuthenticateDoesNotLogInIfTokenIsExpired(): void
9897

9998
$current_user = CurrentUser::get();
10099
$this->assertNull($current_user);
101-
$current_session = CurrentUser::session();
102-
$this->assertNull($current_session);
103100
}
104101

105102
public function testAuthenticateDoesNotLogInIfTokenIsInvalidated(): void
@@ -119,8 +116,6 @@ public function testAuthenticateDoesNotLogInIfTokenIsInvalidated(): void
119116

120117
$current_user = CurrentUser::get();
121118
$this->assertNull($current_user);
122-
$current_session = CurrentUser::session();
123-
$this->assertNull($current_session);
124119
}
125120

126121
public function testAuthenticateDoesNotLogInIfScopeDoesNotMatch(): void
@@ -139,8 +134,6 @@ public function testAuthenticateDoesNotLogInIfScopeDoesNotMatch(): void
139134

140135
$current_user = CurrentUser::get();
141136
$this->assertNull($current_user);
142-
$current_session = CurrentUser::session();
143-
$this->assertNull($current_session);
144137
}
145138

146139
public function testAuthenticateFailsWithSupportUser(): void

tests/controllers/api/v1/SessionsTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function testCreateCreatesASessionAndReturnsAToken(): void
3434
$current_user = auth\CurrentUser::get();
3535
$session = auth\CurrentUser::session();
3636
$this->assertNotNull($current_user);
37-
$this->assertNotNull($session);
3837
$this->assertSame($user->id, $current_user->id);
3938
$this->assertSame($app_name, $session->name);
4039
$this->assertApiResponse($response, [
@@ -63,9 +62,7 @@ public function testCreateFailsIfAppNameIsMissing(): void
6362

6463
$this->assertResponseCode($response, 400);
6564
$current_user = auth\CurrentUser::get();
66-
$session = auth\CurrentUser::session();
6765
$this->assertNull($current_user);
68-
$this->assertNull($session);
6966
$this->assertApiError(
7067
$response,
7168
'app_name',
@@ -94,9 +91,7 @@ public function testCreateFailsIfEmailIsInvalid(): void
9491

9592
$this->assertResponseCode($response, 400);
9693
$current_user = auth\CurrentUser::get();
97-
$session = auth\CurrentUser::session();
9894
$this->assertNull($current_user);
99-
$this->assertNull($session);
10095
$this->assertApiError(
10196
$response,
10297
'@base',
@@ -125,9 +120,7 @@ public function testCreateFailsIfPasswordIsInvalid(): void
125120

126121
$this->assertResponseCode($response, 400);
127122
$current_user = auth\CurrentUser::get();
128-
$session = auth\CurrentUser::session();
129123
$this->assertNull($current_user);
130-
$this->assertNull($session);
131124
$this->assertApiError(
132125
$response,
133126
'@base',
@@ -140,8 +133,6 @@ public function testDeleteInvalidatesCurrentSession(): void
140133
$user = $this->login();
141134
$session = auth\CurrentUser::session();
142135

143-
$this->assertNotNull($session);
144-
145136
$response = $this->apiRun('DELETE', '/api/v1/session');
146137

147138
$this->assertResponseCode($response, 200);

tests/controllers/my/SecurityTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ public function testUpdateDeletesExistingSessionsExceptCurrentOne(): void
156156
'user_id' => $user->id,
157157
]);
158158

159-
$this->assertNotNull($current_session);
160159
$this->assertSame(2, models\Session::count());
161160

162161
$response = $this->appRun('POST', '/my/security', [
@@ -421,7 +420,6 @@ public function testConfirmSetsConfirmedPasswordAtAndRedirects(): void
421420

422421
$this->assertResponseCode($response, 302, '/my/security');
423422
$session = auth\CurrentUser::session();
424-
$this->assertNotNull($session);
425423
$now = \Minz\Time::now();
426424
$this->assertEquals($now, $session->confirmed_password_at);
427425
}
@@ -461,7 +459,6 @@ public function testConfirmFailsIfCsrfIsInvalid(): void
461459
$this->assertResponseContains($response, 'A security verification failed');
462460
$this->assertResponseTemplateName($response, 'my/security/confirmation.phtml');
463461
$session = auth\CurrentUser::session();
464-
$this->assertNotNull($session);
465462
$this->assertNull($session->confirmed_password_at);
466463
}
467464

@@ -483,7 +480,6 @@ public function testConfirmFailsIfPasswordIsInvalid(): void
483480
$this->assertResponseContains($response, 'The password is incorrect.');
484481
$this->assertResponseTemplateName($response, 'my/security/confirmation.phtml');
485482
$session = auth\CurrentUser::session();
486-
$this->assertNotNull($session);
487483
$this->assertNull($session->confirmed_password_at);
488484
}
489485
}

tests/controllers/my/SessionsTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ public function testDeleteLogsOutIfGivenSessionIsCurrentSession(): void
8989
$user = $this->login(confirmed_password_at: $confirmed_at);
9090
$session = auth\CurrentUser::session();
9191

92-
$this->assertNotNull($session);
93-
9492
$response = $this->appRun('POST', "/my/sessions/{$session->id}/deletion", [
9593
'csrf_token' => $this->csrfToken(forms\security\DeleteSession::class),
9694
]);

0 commit comments

Comments
 (0)