Skip to content

Commit b3b426b

Browse files
committed
Try moving some Session info computeds into Session getters
1 parent 3e6bd5d commit b3b426b

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/core/authentication/identity.service.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { forwardRef, Inject, Injectable } from '@nestjs/common';
2-
import { type ID, type Role, UnauthenticatedException } from '~/common';
2+
import { type ID, type Role } from '~/common';
33
import { type IRequest } from '../http/types';
44
import { SessionHost } from './session/session.host';
55
import type { SessionInitiator } from './session/session.initiator';
@@ -44,9 +44,7 @@ export class Identity {
4444
* Manually verify the current requestor is logged in.
4545
*/
4646
verifyLoggedIn(session?: Identity['current']) {
47-
if ((session ?? this.current).anonymous) {
48-
throw new UnauthenticatedException('User is not logged in');
49-
}
47+
(session ?? this.current).verifyLoggedIn();
5048
}
5149

5250
/**
@@ -56,7 +54,7 @@ export class Identity {
5654
* Prefer using Auth Policies / {@link Privileges}`.for.can()`
5755
*/
5856
get isAdmin() {
59-
return this.current.roles.includes('global:Administrator');
57+
return this.current.isAdmin;
6058
}
6159

6260
/**
@@ -68,9 +66,7 @@ export class Identity {
6866
*/
6967
get isImpersonatorAdmin() {
7068
const session = this.current;
71-
return (session.impersonator ?? session).roles.includes(
72-
'global:Administrator',
73-
);
69+
return (session.impersonator ?? session).isAdmin;
7470
}
7571

7672
/**
@@ -80,7 +76,7 @@ export class Identity {
8076
* Prefer using Auth Policies / {@link Privileges}`.for.can()`
8177
*/
8278
isSelf(id: ID<'User'>) {
83-
return id === this.current.userId;
79+
return this.current.isSelf(id);
8480
}
8581

8682
async asUser<R>(user: ID<'User'>, fn: () => Promise<R>): Promise<R> {

src/core/authentication/session/session.dto.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type DateTime } from 'luxon';
2-
import { DataObject } from '~/common';
2+
import { DataObject, UnauthenticatedException } from '~/common';
33
import { type ID } from '~/common/id-field';
44
import { type ScopedRole } from '../../../components/authorization/dto';
55

@@ -31,4 +31,21 @@ export class Session extends RawSession {
3131
with(next: Partial<RawSession>): Session {
3232
return Object.assign(Session.defaultValue(Session), this, next);
3333
}
34+
35+
/**
36+
* Manually verify the current requestor is logged in.
37+
*/
38+
verifyLoggedIn() {
39+
if (this.anonymous) {
40+
throw new UnauthenticatedException('User is not logged in');
41+
}
42+
}
43+
44+
get isAdmin() {
45+
return this.roles.includes('global:Administrator');
46+
}
47+
48+
isSelf(id: ID<'User'>) {
49+
return id === this.userId;
50+
}
3451
}

0 commit comments

Comments
 (0)