Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Seeder Module (Cache Module) #7681

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/core/src/role-permission/role-permission.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 { }
11 changes: 5 additions & 6 deletions packages/core/src/shared/guards/permission.guard.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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 {
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
private readonly _reflector: Reflector,
private readonly _rolePermissionService: RolePermissionService
) {}
) { }

/**
* Checks if the user is authorized based on specified permissions.
Expand All @@ -25,8 +25,7 @@ export class PermissionGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
// Retrieve permissions from metadata
const targets: Array<Function | Type<any>> = [context.getHandler(), context.getClass()];
const permissions =
removeDuplicates(this._reflector.getAllAndOverride<PermissionsEnum[]>(PERMISSIONS_METADATA, targets)) || [];
const permissions = removeDuplicates(this._reflector.getAllAndOverride<PermissionsEnum[]>(PERMISSIONS_METADATA, targets)) || [];

// If no specific permissions are required, consider it authorized
if (isEmpty(permissions)) {
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/shared/guards/tenant-permission.guard.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 { 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 {
Expand Down Expand Up @@ -50,8 +50,7 @@ export class TenantPermissionGuard extends TenantBaseGuard implements CanActivat
// Retrieve permissions from metadata
const targets: Array<Function | Type<any>> = [context.getHandler(), context.getClass()];

const permissions =
removeDuplicates(this._reflector.getAllAndOverride<PermissionsEnum[]>(PERMISSIONS_METADATA, targets)) || [];
const permissions = removeDuplicates(this._reflector.getAllAndOverride<PermissionsEnum[]>(PERMISSIONS_METADATA, targets)) || [];

// Check if permissions are not empty
if (isNotEmpty(permissions)) {
Expand Down
Loading