-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.guard.ts
70 lines (60 loc) · 1.88 KB
/
menu.guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
Injectable,
CanActivate,
ExecutionContext,
Logger,
UnauthorizedException,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { GqlExecutionContext } from '@nestjs/graphql';
import { InjectRepository } from '@nestjs/typeorm';
import { MENU_PATH_KEY } from 'src/decorator/menu.decorator';
import { User } from 'src/user/user.model';
import { Repository } from 'typeorm';
@Injectable()
export class MenuGuard implements CanActivate {
private readonly logger = new Logger(MenuGuard.name);
constructor(
private reflector: Reflector,
@InjectRepository(User)
private userRepository: Repository<User>,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const requiredPath = this.reflector.getAllAndOverride<string>(
MENU_PATH_KEY,
[context.getHandler(), context.getClass()],
);
if (!requiredPath) {
return true;
}
const gqlContext = GqlExecutionContext.create(context);
const { req } = gqlContext.getContext();
if (!req.user?.id) {
throw new UnauthorizedException('User is not authenticated');
}
try {
const user = await this.userRepository.findOne({
where: { id: req.user.id },
relations: ['roles', 'roles.menus'],
});
if (!user) {
throw new UnauthorizedException('User not found');
}
const hasMenuAccess = user.roles.some((role) =>
role.menus?.some((menu) => menu.path === requiredPath),
);
if (!hasMenuAccess) {
this.logger.warn(
`User ${user.username} attempted to access menu path: ${requiredPath} without permission`,
);
throw new UnauthorizedException(
'User does not have access to this menu',
);
}
return true;
} catch (error) {
this.logger.error(`Menu access check failed: ${error.message}`);
throw error;
}
}
}