Skip to content

Commit

Permalink
Merge pull request #88 from arati-tekdi/main
Browse files Browse the repository at this point in the history
Commented role validation logic
  • Loading branch information
Shubham4026 authored Feb 25, 2025
2 parents 2291b29 + 4e1b6b8 commit 3e7a3b7
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/common/guards/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,43 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {

async validate(request: any, payload: any) {
let userPrivileges;
const ttl = this.configService.get('TTL');
//try {
const tenantId = request.headers['tenantid'];
if (!tenantId?.trim()) {
throw new BadRequestException('Tenant id not found');
}
request.userId = payload.sub;
const requiredPermissions = request.requiredPermissions;
// const ttl = this.configService.get('TTL');
// //try {
// const tenantId = request.headers['tenantid'];
// if (!tenantId?.trim()) {
// throw new BadRequestException('Tenant id not found');
// }
// request.userId = payload.sub;
// const requiredPermissions = request.requiredPermissions;

const cachedData: UserPrivilegeRoleDto = await this.cacheService.get(
payload.sub,
);
if (!cachedData) {
const userPrivilegesAndRoles: any =
await this.permissionService.getUserPrivilegesAndRoles(
payload.sub,
tenantId,
);
if (userPrivilegesAndRoles.length == 0) {
throw new UnauthorizedException(
'User does not have any privileges in the Tenant',
);
}
userPrivileges = userPrivilegesAndRoles['privileges'][tenantId]
? userPrivilegesAndRoles['privileges'][tenantId]
: [];
this.cacheService.set(payload.sub, userPrivilegesAndRoles, ttl);
} else {
userPrivileges = cachedData.privileges[tenantId]
? cachedData.privileges[tenantId]
: [];
}
if (!userPrivileges && userPrivileges.length == 0) {
throw new UnauthorizedException(
'User does not have any privileges in the Tenant',
);
}
// const cachedData: UserPrivilegeRoleDto = await this.cacheService.get(
// payload.sub,
// );
// if (!cachedData) {
// const userPrivilegesAndRoles: any =
// await this.permissionService.getUserPrivilegesAndRoles(
// payload.sub,
// tenantId,
// );
// if (userPrivilegesAndRoles.length == 0) {
// throw new UnauthorizedException(
// 'User does not have any privileges in the Tenant',
// );
// }
// userPrivileges = userPrivilegesAndRoles['privileges'][tenantId]
// ? userPrivilegesAndRoles['privileges'][tenantId]
// : [];
// this.cacheService.set(payload.sub, userPrivilegesAndRoles, ttl);
// } else {
// userPrivileges = cachedData.privileges[tenantId]
// ? cachedData.privileges[tenantId]
// : [];
// }
// if (!userPrivileges && userPrivileges.length == 0) {
// throw new UnauthorizedException(
// 'User does not have any privileges in the Tenant',
// );
// }
this.middlewareLogger.log(
`user : ${payload.sub - payload.username} userPrivileges: ${userPrivileges}`,
);
Expand Down

0 comments on commit 3e7a3b7

Please sign in to comment.