Skip to content

Commit 3136fdf

Browse files
committed
Migrate CanImpersonate Event/Hook
1 parent ab66635 commit 3136fdf

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/components/authorization/authorization.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
SessionExtraInfoResolver,
88
} from './authorization.resolver';
99
import { BetaFeaturesGranter } from './dto/beta-features.dto';
10-
import { CanImpersonateHandler } from './handler/can-impersonate.handler';
10+
import { CanImpersonateViaPrivilegesHandler } from './handler/can-impersonate-via-privileges.handler';
1111
import * as Policies from './policies';
1212
import { PolicyModule } from './policy/policy.module';
1313

@@ -19,7 +19,7 @@ import { PolicyModule } from './policy/policy.module';
1919
LoginExtraInfoResolver,
2020
RegisterExtraInfoResolver,
2121
SessionExtraInfoResolver,
22-
CanImpersonateHandler,
22+
CanImpersonateViaPrivilegesHandler,
2323
...Object.values(Policies),
2424
AssignableRolesGranter,
2525
BetaFeaturesGranter,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { CanImpersonateHook } from '~/core/authentication/hooks/can-impersonate.hook';
3+
import { OnHook } from '~/core/hooks';
4+
import { AssignableRoles } from '../dto/assignable-roles.dto';
5+
import { Privileges } from '../policy';
6+
7+
@Injectable()
8+
export class CanImpersonateViaPrivilegesHandler {
9+
constructor(private readonly privileges: Privileges) {}
10+
11+
@OnHook(CanImpersonateHook)
12+
canImpersonate({ session, allow }: CanImpersonateHook) {
13+
const p = this.privileges.for(AssignableRoles);
14+
const granted = session.roles.values().every((role) => p.can('edit', role));
15+
allow.vote(granted);
16+
}
17+
}

src/components/authorization/handler/can-impersonate.handler.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type PollVoter } from '~/common';
22
import { type Session } from '../session/session.dto';
33

4-
export class CanImpersonateEvent {
4+
export class CanImpersonateHook {
55
constructor(readonly session: Session, readonly allow: PollVoter<boolean>) {}
66
}

src/core/authentication/session/session.manager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
ServerException,
1010
UnauthorizedException,
1111
} from '~/common';
12-
import { IEventBus } from '~/core/events';
12+
import { Hooks } from '~/core/hooks';
1313
import { ILogger, Logger } from '~/core/logger';
1414
import { SystemAgentRepository } from '../../../components/user/system-agent.repository';
1515
import { AuthenticationRepository } from '../authentication.repository';
16-
import { CanImpersonateEvent } from '../events/can-impersonate.event';
16+
import { CanImpersonateHook } from '../hooks/can-impersonate.hook';
1717
import { JwtService } from '../jwt.service';
1818
import { NoSessionException } from './no-session.exception';
1919
import { Session } from './session.dto';
@@ -26,7 +26,7 @@ import { SessionHost } from './session.host';
2626
export class SessionManager {
2727
constructor(
2828
private readonly agents: SystemAgentRepository,
29-
private readonly events: IEventBus,
29+
private readonly hooks: Hooks,
3030
private readonly jwt: JwtService,
3131
private readonly sessionHost: SessionHost,
3232
private readonly repo: AuthenticationRepository,
@@ -105,11 +105,11 @@ export class SessionManager {
105105
if (impersonatee) {
106106
const allowImpersonation = new Poll();
107107
await this.sessionHost.withSession(requesterSession, async () => {
108-
const event = new CanImpersonateEvent(
108+
const event = new CanImpersonateHook(
109109
requesterSession,
110110
allowImpersonation,
111111
);
112-
await this.events.publish(event);
112+
await this.hooks.run(event);
113113
});
114114
if (!(allowImpersonation.plurality && !allowImpersonation.vetoed)) {
115115
// Don't expose what the requester is unable to do as this could leak

0 commit comments

Comments
 (0)