From f60fe3eb51303af2d141daf47a065cc25648cde6 Mon Sep 17 00:00:00 2001 From: RAHUL RATHORE <41804588+rahul-rocket@users.noreply.github.com> Date: Mon, 18 Mar 2024 13:19:00 +0530 Subject: [PATCH 1/2] fix: missing cache module for role permissions --- packages/core/src/role-permission/role-permission.module.ts | 6 ++++-- packages/core/src/shared/guards/tenant-permission.guard.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/core/src/role-permission/role-permission.module.ts b/packages/core/src/role-permission/role-permission.module.ts index b6fa5084211..2f064cfc10e 100644 --- a/packages/core/src/role-permission/role-permission.module.ts +++ b/packages/core/src/role-permission/role-permission.module.ts @@ -2,6 +2,7 @@ import { forwardRef, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { CqrsModule } from '@nestjs/cqrs'; import { RouterModule } from '@nestjs/core'; +import { CacheModule } from '@nestjs/cache-manager'; import { MikroOrmModule } from '@mikro-orm/nestjs'; import { RolePermissionController } from './role-permission.controller'; import { RolePermission } from './role-permission.entity'; @@ -17,10 +18,11 @@ import { TypeOrmRolePermissionRepository } from './repository'; forwardRef(() => TypeOrmModule.forFeature([RolePermission])), forwardRef(() => MikroOrmModule.forFeature([RolePermission])), forwardRef(() => RoleModule), - CqrsModule + CqrsModule, + CacheModule.register({ isGlobal: true }) ], controllers: [RolePermissionController], providers: [RolePermissionService, TypeOrmRolePermissionRepository], - exports: [TypeOrmModule, MikroOrmModule, RolePermissionService, TypeOrmRolePermissionRepository] + exports: [TypeOrmModule, MikroOrmModule, CacheModule, RolePermissionService, TypeOrmRolePermissionRepository] }) export class RolePermissionModule { } diff --git a/packages/core/src/shared/guards/tenant-permission.guard.ts b/packages/core/src/shared/guards/tenant-permission.guard.ts index 81d470f5dba..283fbc0525c 100644 --- a/packages/core/src/shared/guards/tenant-permission.guard.ts +++ b/packages/core/src/shared/guards/tenant-permission.guard.ts @@ -3,11 +3,11 @@ import { Reflector } from '@nestjs/core'; import { PermissionsEnum, RolesEnum } from '@gauzy/contracts'; import { environment as env } from '@gauzy/config'; import { isNotEmpty, PERMISSIONS_METADATA, removeDuplicates } from '@gauzy/common'; +import { Cache } from 'cache-manager'; +import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { RequestContext } from './../../core/context'; import { TenantBaseGuard } from './tenant-base.guard'; import { RolePermissionService } from '../../role-permission/role-permission.service'; -import { Cache } from 'cache-manager'; -import { CACHE_MANAGER } from '@nestjs/cache-manager'; @Injectable() export class TenantPermissionGuard extends TenantBaseGuard implements CanActivate { From bbb6d8012cf39aa87b512a4a55ca014a9ca6b3b0 Mon Sep 17 00:00:00 2001 From: RAHUL RATHORE <41804588+rahul-rocket@users.noreply.github.com> Date: Mon, 18 Mar 2024 13:29:57 +0530 Subject: [PATCH 2/2] fix: reorder module imports --- packages/core/src/shared/guards/permission.guard.ts | 11 +++++------ .../core/src/shared/guards/tenant-permission.guard.ts | 7 +++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/core/src/shared/guards/permission.guard.ts b/packages/core/src/shared/guards/permission.guard.ts index b3fdedeaf52..d891a143d20 100644 --- a/packages/core/src/shared/guards/permission.guard.ts +++ b/packages/core/src/shared/guards/permission.guard.ts @@ -1,13 +1,13 @@ -import { environment as env } from '@gauzy/config'; import { CanActivate, ExecutionContext, Inject, Injectable, Type } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; +import { CACHE_MANAGER } from '@nestjs/cache-manager'; +import { Cache } from 'cache-manager'; import { verify } from 'jsonwebtoken'; +import { environment as env } from '@gauzy/config'; import { isEmpty, PERMISSIONS_METADATA, removeDuplicates } from '@gauzy/common'; import { PermissionsEnum } from '@gauzy/contracts'; import { RequestContext } from './../../core/context'; import { RolePermissionService } from '../../role-permission/role-permission.service'; -import { Cache } from 'cache-manager'; -import { CACHE_MANAGER } from '@nestjs/cache-manager'; @Injectable() export class PermissionGuard implements CanActivate { @@ -15,7 +15,7 @@ export class PermissionGuard implements CanActivate { @Inject(CACHE_MANAGER) private cacheManager: Cache, private readonly _reflector: Reflector, private readonly _rolePermissionService: RolePermissionService - ) {} + ) { } /** * Checks if the user is authorized based on specified permissions. @@ -25,8 +25,7 @@ export class PermissionGuard implements CanActivate { async canActivate(context: ExecutionContext): Promise { // Retrieve permissions from metadata const targets: Array> = [context.getHandler(), context.getClass()]; - const permissions = - removeDuplicates(this._reflector.getAllAndOverride(PERMISSIONS_METADATA, targets)) || []; + const permissions = removeDuplicates(this._reflector.getAllAndOverride(PERMISSIONS_METADATA, targets)) || []; // If no specific permissions are required, consider it authorized if (isEmpty(permissions)) { diff --git a/packages/core/src/shared/guards/tenant-permission.guard.ts b/packages/core/src/shared/guards/tenant-permission.guard.ts index 283fbc0525c..05fae8a82a3 100644 --- a/packages/core/src/shared/guards/tenant-permission.guard.ts +++ b/packages/core/src/shared/guards/tenant-permission.guard.ts @@ -1,10 +1,10 @@ import { Injectable, CanActivate, ExecutionContext, Type, Inject } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; +import { Cache } from 'cache-manager'; +import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { PermissionsEnum, RolesEnum } from '@gauzy/contracts'; import { environment as env } from '@gauzy/config'; import { isNotEmpty, PERMISSIONS_METADATA, removeDuplicates } from '@gauzy/common'; -import { Cache } from 'cache-manager'; -import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { RequestContext } from './../../core/context'; import { TenantBaseGuard } from './tenant-base.guard'; import { RolePermissionService } from '../../role-permission/role-permission.service'; @@ -50,8 +50,7 @@ export class TenantPermissionGuard extends TenantBaseGuard implements CanActivat // Retrieve permissions from metadata const targets: Array> = [context.getHandler(), context.getClass()]; - const permissions = - removeDuplicates(this._reflector.getAllAndOverride(PERMISSIONS_METADATA, targets)) || []; + const permissions = removeDuplicates(this._reflector.getAllAndOverride(PERMISSIONS_METADATA, targets)) || []; // Check if permissions are not empty if (isNotEmpty(permissions)) {