Skip to content

Commit a70ec3c

Browse files
committed
Pull session from SessionHost where needed
1 parent 5305924 commit a70ec3c

File tree

8 files changed

+32
-2
lines changed

8 files changed

+32
-2
lines changed

src/components/comments/comment.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { isAdmin } from '~/common/session';
1616
import { ResourceLoader, ResourcesHost } from '~/core';
1717
import { type BaseNode, isBaseNode } from '~/core/database/results';
18+
import { SessionHost } from '../authentication';
1819
import { Privileges } from '../authorization';
1920
import { CommentRepository } from './comment.repository';
2021
import {
@@ -39,6 +40,7 @@ export class CommentService {
3940
private readonly privileges: Privileges,
4041
private readonly resources: ResourceLoader,
4142
private readonly resourcesHost: ResourcesHost,
43+
private readonly sessionHost: SessionHost,
4244
private readonly mentionNotificationService: CommentViaMentionNotificationService,
4345
) {}
4446

@@ -123,6 +125,7 @@ export class CommentService {
123125
thread: UnsecuredDto<CommentThread>,
124126
session: Session,
125127
): CommentThread {
128+
const session = this.sessionHost.current;
126129
return {
127130
...thread,
128131
firstComment: this.secureComment(thread.firstComment, session),

src/components/engagement/engagement.rules.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { ILogger, Logger } from '~/core';
1313
import { DatabaseService } from '~/core/database';
1414
import { ACTIVE, INACTIVE } from '~/core/database/query';
15+
import { SessionHost } from '../authentication';
1516
import { withoutScope } from '../authorization/dto';
1617
import { ProjectStep } from '../project/dto';
1718
import {
@@ -35,6 +36,7 @@ const rolesThatCanBypassWorkflow: Role[] = [Role.Administrator];
3536
export class EngagementRules {
3637
constructor(
3738
private readonly db: DatabaseService,
39+
private readonly sessionHost: SessionHost,
3840
// eslint-disable-next-line @seedcompany/no-unused-vars
3941
@Logger('engagement:rules') private readonly logger: ILogger,
4042
) {}
@@ -317,6 +319,7 @@ export class EngagementRules {
317319
currentUserRoles?: Role[],
318320
changeset?: ID,
319321
): Promise<EngagementStatusTransition[]> {
322+
const session = this.sessionHost.current;
320323
if (session.anonymous) {
321324
return [];
322325
}
@@ -356,6 +359,7 @@ export class EngagementRules {
356359
}
357360

358361
async canBypassWorkflow(session: Session) {
362+
const session = this.sessionHost.current;
359363
const roles = session.roles.map(withoutScope);
360364
return intersection(rolesThatCanBypassWorkflow, roles).length > 0;
361365
}
@@ -368,6 +372,7 @@ export class EngagementRules {
368372
) {
369373
// If current user's roles include a role that can bypass workflow
370374
// stop the check here.
375+
const session = this.sessionHost.current;
371376
const currentUserRoles = session.roles.map(withoutScope);
372377
if (intersection(rolesThatCanBypassWorkflow, currentUserRoles).length > 0) {
373378
return;

src/components/project/project.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { isAdmin } from '~/common/session';
2626
import { HandleIdLookup, IEventBus } from '~/core';
2727
import { Transactional } from '~/core/database';
2828
import { type AnyChangesOf } from '~/core/database/changes';
29+
import { SessionHost } from '../authentication';
2930
import { Privileges } from '../authorization';
3031
import { withoutScope } from '../authorization/dto';
3132
import { BudgetService } from '../budget';
@@ -90,6 +91,7 @@ export class ProjectService {
9091
@Inject(forwardRef(() => EngagementService))
9192
private readonly engagementService: EngagementService & {},
9293
private readonly privileges: Privileges,
94+
private readonly sessionHost: SessionHost,
9395
private readonly eventBus: IEventBus,
9496
private readonly repo: ProjectRepository,
9597
private readonly projectChangeRequests: ProjectChangeRequestService,
@@ -140,6 +142,7 @@ export class ProjectService {
140142
);
141143

142144
// Only allow admins to specify department IDs
145+
const session = this.sessionHost.current;
143146
if (input.departmentId && !isAdmin(session.impersonator ?? session)) {
144147
throw UnauthorizedException.fromPrivileges(
145148
'edit',
@@ -267,6 +270,7 @@ export class ProjectService {
267270
);
268271

269272
// Only allow admins to specify department IDs
273+
const session = this.sessionHost.current;
270274
if (
271275
input.departmentId !== undefined &&
272276
!isAdmin(session.impersonator ?? session)

src/components/project/workflow/handlers/project-workflow-notification.handler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
ProjectStepChanged,
1414
type ProjectStepChangedProps,
1515
} from '~/core/email/templates/project-step-changed.template';
16-
import { AuthenticationService } from '../../../authentication';
16+
import { AuthenticationService, SessionHost } from '../../../authentication';
1717
import { ProjectService } from '../../../project';
1818
import { UserService } from '../../../user';
1919
import { type User } from '../../../user/dto';
@@ -31,6 +31,7 @@ export class ProjectWorkflowNotificationHandler
3131
private readonly users: UserService,
3232
private readonly projects: ProjectService,
3333
private readonly emailService: EmailService,
34+
private readonly sessionHost: SessionHost,
3435
private readonly moduleRef: ModuleRef,
3536
@Logger('progress-report:status-change-notifier')
3637
private readonly logger: ILogger,
@@ -40,6 +41,8 @@ export class ProjectWorkflowNotificationHandler
4041
const { previousStep, next, workflowEvent, session } = event;
4142
const transition = typeof next !== 'string' ? next : undefined;
4243

44+
const session = this.sessionHost.current;
45+
4346
// TODO on bypass: keep notifying members? add anyone else?
4447
const notifiers = transition?.notifiers ?? [];
4548

src/components/prompts/prompt-variant-response.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '~/common';
1919
import { ResourceLoader } from '~/core';
2020
import { mapListResults } from '~/core/database/results';
21+
import { SessionHost } from '../authentication';
2122
import {
2223
Privileges,
2324
type UserResourcePrivileges,
@@ -53,6 +54,7 @@ export const PromptVariantResponseListService = <
5354
abstract class PromptVariantResponseListServiceClass {
5455
@Inject(Privileges)
5556
protected readonly privileges: Privileges;
57+
@Inject() protected readonly sessionHost: SessionHost;
5658
@Inject(ResourceLoader)
5759
protected readonly resources: ResourceLoader;
5860
@Inject(repo)
@@ -236,6 +238,7 @@ export const PromptVariantResponseListService = <
236238
await this.repo.submitResponse(input, session);
237239
}
238240

241+
const session = this.sessionHost.current;
239242
const responses = mapKeys.fromList(
240243
response.responses,
241244
(response) => response.variant,

src/components/user/education/education.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type UnsecuredDto,
77
} from '~/common';
88
import { HandleIdLookup } from '~/core';
9+
import { SessionHost } from '../../authentication';
910
import { Privileges } from '../../authorization';
1011
import {
1112
type CreateEducation,
@@ -20,6 +21,7 @@ import { EducationRepository } from './education.repository';
2021
export class EducationService {
2122
constructor(
2223
private readonly privileges: Privileges,
24+
private readonly sessionHost: SessionHost,
2325
private readonly repo: EducationRepository,
2426
) {}
2527

@@ -56,6 +58,7 @@ export class EducationService {
5658
const result = await this.repo.getUserIdByEducation(input.id);
5759
const changes = this.repo.getActualChanges(ed, input);
5860
// TODO move this condition into policies
61+
const session = this.sessionHost.current;
5962
if (result.id !== session.userId) {
6063
this.privileges.for(Education, ed).verifyChanges(changes);
6164
}

src/components/user/unavailability/unavailability.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type UnsecuredDto,
77
} from '~/common';
88
import { HandleIdLookup } from '~/core';
9+
import { SessionHost } from '../../authentication';
910
import { Privileges } from '../../authorization';
1011
import {
1112
type CreateUnavailability,
@@ -20,6 +21,7 @@ import { UnavailabilityRepository } from './unavailability.repository';
2021
export class UnavailabilityService {
2122
constructor(
2223
private readonly privileges: Privileges,
24+
private readonly sessionHost: SessionHost,
2325
private readonly repo: UnavailabilityRepository,
2426
) {}
2527

@@ -59,6 +61,7 @@ export class UnavailabilityService {
5961
const result = await this.repo.getUserIdByUnavailability(input.id);
6062
const changes = this.repo.getActualChanges(unavailability, input);
6163
// TODO move this condition into policies
64+
const session = this.sessionHost.current;
6265
if (result.id !== session.userId) {
6366
this.privileges
6467
.for(Unavailability, unavailability)

src/components/user/user.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { HandleIdLookup, ILogger, Logger } from '~/core';
1414
import { Transactional } from '~/core/database';
1515
import { property } from '~/core/database/query';
16+
import { SessionHost } from '../authentication/session.host';
1617
import { Privileges } from '../authorization';
1718
import { AssignableRoles } from '../authorization/dto/assignable-roles.dto';
1819
import { LocationService } from '../location';
@@ -62,6 +63,7 @@ export class UserService {
6263
private readonly privileges: Privileges,
6364
private readonly locationService: LocationService,
6465
private readonly knownLanguages: KnownLanguageRepository,
66+
private readonly sessionHost: SessionHost,
6567
private readonly userRepo: UserRepository,
6668
@Logger('user:service') private readonly logger: ILogger,
6769
) {}
@@ -73,8 +75,12 @@ export class UserService {
7375
};
7476

7577
async create(input: CreatePerson, session?: Session): Promise<ID> {
76-
if (input.roles && input.roles.length > 0 && session) {
78+
if (
79+
input.roles &&
80+
input.roles.length > 0 &&
7781
// Note: session is only omitted for creating RootUser
82+
this.sessionHost.currentIfInCtx
83+
) {
7884
this.verifyRolesAreAssignable(session, input.roles);
7985
}
8086

0 commit comments

Comments
 (0)