@flusys/nestjs-storage 6.1.2 → 6.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -87,8 +87,6 @@ function _ts_param(paramIndex, decorator) {
87
87
  decorator(target, key, paramIndex);
88
88
  };
89
89
  }
90
- const PERMISSION_CACHE_KEY = 'user_action_permission';
91
- const VIEW_PRIVATE_ACTION = 'storage.file.viewPrivate';
92
90
  const URL_EXPIRY_SECONDS = 3600;
93
91
  const DEFAULT_SELECT_FIELDS = [
94
92
  'id',
@@ -297,9 +295,11 @@ let FileManagerService = class FileManagerService extends _classes.ApiService {
297
295
  });
298
296
  return;
299
297
  }
300
- const cacheKey = this.storageConfig.isCompanyFeatureEnabled() ? `${PERMISSION_CACHE_KEY}_${user.id}_${user.companyId}` : `${PERMISSION_CACHE_KEY}_${user.id}`;
301
- const actions = await this.cacheManager.get(cacheKey);
302
- const canViewPrivate = actions?.some((a)=>a.url === VIEW_PRIVATE_ACTION) ?? false;
298
+ const canViewPrivate = await this.permissionCacheService.hasPermission({
299
+ userId: user.id,
300
+ companyId: user.companyId,
301
+ branchId: user.branchId
302
+ }, _classes.FILE_PERMISSIONS.VIEW_PRIVATE_ACTION);
303
303
  if (!canViewPrivate) {
304
304
  query.andWhere('file_manager.isPrivate = :isPrivate', {
305
305
  isPrivate: false
@@ -360,8 +360,8 @@ let FileManagerService = class FileManagerService extends _classes.ApiService {
360
360
  storageConfigId: file.storageConfigId || undefined
361
361
  };
362
362
  }
363
- constructor(cacheManager, utilsService, uploadService, storageConfig, dataSourceProvider){
364
- super('file_manager', cacheManager, utilsService, FileManagerService.name, true, 'storage', undefined, dataSourceProvider), _define_property(this, "cacheManager", void 0), _define_property(this, "utilsService", void 0), _define_property(this, "uploadService", void 0), _define_property(this, "storageConfig", void 0), _define_property(this, "dataSourceProvider", void 0), this.cacheManager = cacheManager, this.utilsService = utilsService, this.uploadService = uploadService, this.storageConfig = storageConfig, this.dataSourceProvider = dataSourceProvider;
363
+ constructor(cacheManager, utilsService, uploadService, storageConfig, dataSourceProvider, permissionCacheService){
364
+ super('file_manager', cacheManager, utilsService, FileManagerService.name, true, 'storage', undefined, dataSourceProvider), _define_property(this, "cacheManager", void 0), _define_property(this, "utilsService", void 0), _define_property(this, "uploadService", void 0), _define_property(this, "storageConfig", void 0), _define_property(this, "dataSourceProvider", void 0), _define_property(this, "permissionCacheService", void 0), this.cacheManager = cacheManager, this.utilsService = utilsService, this.uploadService = uploadService, this.storageConfig = storageConfig, this.dataSourceProvider = dataSourceProvider, this.permissionCacheService = permissionCacheService;
365
365
  }
366
366
  };
367
367
  FileManagerService = _ts_decorate([
@@ -373,12 +373,14 @@ FileManagerService = _ts_decorate([
373
373
  _ts_param(2, (0, _common.Inject)(_uploadservice.UploadService)),
374
374
  _ts_param(3, (0, _common.Inject)(_storageconfigservice.StorageConfigService)),
375
375
  _ts_param(4, (0, _common.Inject)(_storagedatasourceprovider.StorageDataSourceProvider)),
376
+ _ts_param(5, (0, _common.Inject)(_modules.SharedPermissionCacheService)),
376
377
  _ts_metadata("design:type", Function),
377
378
  _ts_metadata("design:paramtypes", [
378
379
  typeof _classes.HybridCache === "undefined" ? Object : _classes.HybridCache,
379
380
  typeof _modules.UtilsService === "undefined" ? Object : _modules.UtilsService,
380
381
  typeof _uploadservice.UploadService === "undefined" ? Object : _uploadservice.UploadService,
381
382
  typeof _storageconfigservice.StorageConfigService === "undefined" ? Object : _storageconfigservice.StorageConfigService,
382
- typeof _storagedatasourceprovider.StorageDataSourceProvider === "undefined" ? Object : _storagedatasourceprovider.StorageDataSourceProvider
383
+ typeof _storagedatasourceprovider.StorageDataSourceProvider === "undefined" ? Object : _storagedatasourceprovider.StorageDataSourceProvider,
384
+ typeof _modules.SharedPermissionCacheService === "undefined" ? Object : _modules.SharedPermissionCacheService
383
385
  ])
384
386
  ], FileManagerService);
@@ -42,6 +42,7 @@ describe('FileManagerService', ()=>{
42
42
  let mockStorageConfig;
43
43
  let mockUploadService;
44
44
  let mockCacheManager;
45
+ let mockSharedPermissionCacheService;
45
46
  beforeEach(async ()=>{
46
47
  mockFileRepo = (0, _repositorymock.createMockRepository)();
47
48
  mockFileRepo.findBy = jest.fn();
@@ -71,6 +72,9 @@ describe('FileManagerService', ()=>{
71
72
  get: jest.fn(),
72
73
  set: jest.fn()
73
74
  };
75
+ mockSharedPermissionCacheService = {
76
+ hasPermission: jest.fn().mockResolvedValue(false)
77
+ };
74
78
  const module = await _testing.Test.createTestingModule({
75
79
  providers: [
76
80
  _filemanagerservice.FileManagerService,
@@ -93,6 +97,10 @@ describe('FileManagerService', ()=>{
93
97
  {
94
98
  provide: _storagedatasourceprovider.StorageDataSourceProvider,
95
99
  useValue: mockDataSourceProvider
100
+ },
101
+ {
102
+ provide: _modules.SharedPermissionCacheService,
103
+ useValue: mockSharedPermissionCacheService
96
104
  }
97
105
  ]
98
106
  }).compile();
@@ -331,11 +339,7 @@ describe('FileManagerService', ()=>{
331
339
  });
332
340
  });
333
341
  it('hides private files from an authenticated user lacking the viewPrivate permission', async ()=>{
334
- mockCacheManager.get.mockResolvedValue([
335
- {
336
- url: 'storage.file.someOtherAction'
337
- }
338
- ]);
342
+ mockSharedPermissionCacheService.hasPermission.mockResolvedValue(false);
339
343
  const query = {
340
344
  andWhere: jest.fn().mockReturnThis()
341
345
  };
@@ -345,11 +349,7 @@ describe('FileManagerService', ()=>{
345
349
  });
346
350
  });
347
351
  it('does not restrict private files for a user holding the viewPrivate permission', async ()=>{
348
- mockCacheManager.get.mockResolvedValue([
349
- {
350
- url: 'storage.file.viewPrivate'
351
- }
352
- ]);
352
+ mockSharedPermissionCacheService.hasPermission.mockResolvedValue(true);
353
353
  const query = {
354
354
  andWhere: jest.fn().mockReturnThis()
355
355
  };
@@ -25,8 +25,8 @@ function _ts_param(paramIndex, decorator) {
25
25
  decorator(target, key, paramIndex);
26
26
  };
27
27
  }
28
- import { HybridCache, ApiService } from '@flusys/nestjs-shared/classes';
29
- import { UtilsService } from '@flusys/nestjs-shared/modules';
28
+ import { ApiService, FILE_PERMISSIONS, HybridCache } from '@flusys/nestjs-shared/classes';
29
+ import { SharedPermissionCacheService, UtilsService } from '@flusys/nestjs-shared/modules';
30
30
  import { applyCompanyFilter } from '@flusys/nestjs-shared/utils';
31
31
  import { BadRequestException, Inject, Injectable, NotFoundException, Scope } from '@nestjs/common';
32
32
  import { Brackets, In } from 'typeorm';
@@ -36,8 +36,6 @@ import { FileLocationEnum } from '../enums/file-location.enum';
36
36
  import { StorageConfigService } from './storage-config.service';
37
37
  import { StorageDataSourceProvider } from './storage-datasource.provider';
38
38
  import { UploadService } from './upload.service';
39
- const PERMISSION_CACHE_KEY = 'user_action_permission';
40
- const VIEW_PRIVATE_ACTION = 'storage.file.viewPrivate';
41
39
  const URL_EXPIRY_SECONDS = 3600;
42
40
  const DEFAULT_SELECT_FIELDS = [
43
41
  'id',
@@ -246,9 +244,11 @@ export class FileManagerService extends ApiService {
246
244
  });
247
245
  return;
248
246
  }
249
- const cacheKey = this.storageConfig.isCompanyFeatureEnabled() ? `${PERMISSION_CACHE_KEY}_${user.id}_${user.companyId}` : `${PERMISSION_CACHE_KEY}_${user.id}`;
250
- const actions = await this.cacheManager.get(cacheKey);
251
- const canViewPrivate = actions?.some((a)=>a.url === VIEW_PRIVATE_ACTION) ?? false;
247
+ const canViewPrivate = await this.permissionCacheService.hasPermission({
248
+ userId: user.id,
249
+ companyId: user.companyId,
250
+ branchId: user.branchId
251
+ }, FILE_PERMISSIONS.VIEW_PRIVATE_ACTION);
252
252
  if (!canViewPrivate) {
253
253
  query.andWhere('file_manager.isPrivate = :isPrivate', {
254
254
  isPrivate: false
@@ -309,8 +309,8 @@ export class FileManagerService extends ApiService {
309
309
  storageConfigId: file.storageConfigId || undefined
310
310
  };
311
311
  }
312
- constructor(cacheManager, utilsService, uploadService, storageConfig, dataSourceProvider){
313
- super('file_manager', cacheManager, utilsService, FileManagerService.name, true, 'storage', undefined, dataSourceProvider), _define_property(this, "cacheManager", void 0), _define_property(this, "utilsService", void 0), _define_property(this, "uploadService", void 0), _define_property(this, "storageConfig", void 0), _define_property(this, "dataSourceProvider", void 0), this.cacheManager = cacheManager, this.utilsService = utilsService, this.uploadService = uploadService, this.storageConfig = storageConfig, this.dataSourceProvider = dataSourceProvider;
312
+ constructor(cacheManager, utilsService, uploadService, storageConfig, dataSourceProvider, permissionCacheService){
313
+ super('file_manager', cacheManager, utilsService, FileManagerService.name, true, 'storage', undefined, dataSourceProvider), _define_property(this, "cacheManager", void 0), _define_property(this, "utilsService", void 0), _define_property(this, "uploadService", void 0), _define_property(this, "storageConfig", void 0), _define_property(this, "dataSourceProvider", void 0), _define_property(this, "permissionCacheService", void 0), this.cacheManager = cacheManager, this.utilsService = utilsService, this.uploadService = uploadService, this.storageConfig = storageConfig, this.dataSourceProvider = dataSourceProvider, this.permissionCacheService = permissionCacheService;
314
314
  }
315
315
  }
316
316
  FileManagerService = _ts_decorate([
@@ -322,12 +322,14 @@ FileManagerService = _ts_decorate([
322
322
  _ts_param(2, Inject(UploadService)),
323
323
  _ts_param(3, Inject(StorageConfigService)),
324
324
  _ts_param(4, Inject(StorageDataSourceProvider)),
325
+ _ts_param(5, Inject(SharedPermissionCacheService)),
325
326
  _ts_metadata("design:type", Function),
326
327
  _ts_metadata("design:paramtypes", [
327
328
  typeof HybridCache === "undefined" ? Object : HybridCache,
328
329
  typeof UtilsService === "undefined" ? Object : UtilsService,
329
330
  typeof UploadService === "undefined" ? Object : UploadService,
330
331
  typeof StorageConfigService === "undefined" ? Object : StorageConfigService,
331
- typeof StorageDataSourceProvider === "undefined" ? Object : StorageDataSourceProvider
332
+ typeof StorageDataSourceProvider === "undefined" ? Object : StorageDataSourceProvider,
333
+ typeof SharedPermissionCacheService === "undefined" ? Object : SharedPermissionCacheService
332
334
  ])
333
335
  ], FileManagerService);
@@ -1,5 +1,5 @@
1
1
  import { Test } from '@nestjs/testing';
2
- import { UtilsService } from '@flusys/nestjs-shared/modules';
2
+ import { SharedPermissionCacheService, UtilsService } from '@flusys/nestjs-shared/modules';
3
3
  import { createMockRepository } from '@test-utils/mocks/repository.mock';
4
4
  import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
5
5
  import { FileManagerService } from './file-manager.service';
@@ -38,6 +38,7 @@ describe('FileManagerService', ()=>{
38
38
  let mockStorageConfig;
39
39
  let mockUploadService;
40
40
  let mockCacheManager;
41
+ let mockSharedPermissionCacheService;
41
42
  beforeEach(async ()=>{
42
43
  mockFileRepo = createMockRepository();
43
44
  mockFileRepo.findBy = jest.fn();
@@ -67,6 +68,9 @@ describe('FileManagerService', ()=>{
67
68
  get: jest.fn(),
68
69
  set: jest.fn()
69
70
  };
71
+ mockSharedPermissionCacheService = {
72
+ hasPermission: jest.fn().mockResolvedValue(false)
73
+ };
70
74
  const module = await Test.createTestingModule({
71
75
  providers: [
72
76
  FileManagerService,
@@ -89,6 +93,10 @@ describe('FileManagerService', ()=>{
89
93
  {
90
94
  provide: StorageDataSourceProvider,
91
95
  useValue: mockDataSourceProvider
96
+ },
97
+ {
98
+ provide: SharedPermissionCacheService,
99
+ useValue: mockSharedPermissionCacheService
92
100
  }
93
101
  ]
94
102
  }).compile();
@@ -327,11 +335,7 @@ describe('FileManagerService', ()=>{
327
335
  });
328
336
  });
329
337
  it('hides private files from an authenticated user lacking the viewPrivate permission', async ()=>{
330
- mockCacheManager.get.mockResolvedValue([
331
- {
332
- url: 'storage.file.someOtherAction'
333
- }
334
- ]);
338
+ mockSharedPermissionCacheService.hasPermission.mockResolvedValue(false);
335
339
  const query = {
336
340
  andWhere: jest.fn().mockReturnThis()
337
341
  };
@@ -341,11 +345,7 @@ describe('FileManagerService', ()=>{
341
345
  });
342
346
  });
343
347
  it('does not restrict private files for a user holding the viewPrivate permission', async ()=>{
344
- mockCacheManager.get.mockResolvedValue([
345
- {
346
- url: 'storage.file.viewPrivate'
347
- }
348
- ]);
348
+ mockSharedPermissionCacheService.hasPermission.mockResolvedValue(true);
349
349
  const query = {
350
350
  andWhere: jest.fn().mockReturnThis()
351
351
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flusys/nestjs-storage",
3
- "version": "6.1.2",
3
+ "version": "6.1.3",
4
4
  "description": "Modular storage package with optional AWS S3, Azure Blob, and SFTP providers",
5
5
  "main": "cjs/index.js",
6
6
  "module": "fesm/index.js",
@@ -129,7 +129,7 @@
129
129
  }
130
130
  },
131
131
  "dependencies": {
132
- "@flusys/nestjs-core": "6.1.2",
133
- "@flusys/nestjs-shared": "6.1.2"
132
+ "@flusys/nestjs-core": "6.1.3",
133
+ "@flusys/nestjs-shared": "6.1.3"
134
134
  }
135
135
  }
@@ -1,7 +1,7 @@
1
- import { HybridCache, ApiService } from '@flusys/nestjs-shared/classes';
1
+ import { ApiService, HybridCache } from '@flusys/nestjs-shared/classes';
2
2
  import { DeleteDto, FilterAndPaginationDto } from '@flusys/nestjs-shared/dtos';
3
3
  import { ILoggedUserInfo } from '@flusys/nestjs-shared/interfaces';
4
- import { UtilsService } from '@flusys/nestjs-shared/modules';
4
+ import { SharedPermissionCacheService, UtilsService } from '@flusys/nestjs-shared/modules';
5
5
  import { EntityTarget, QueryRunner, SelectQueryBuilder } from 'typeorm';
6
6
  import { CreateFileManagerDto, FilesResponseDto, GetFilesRequestDto, UpdateFileManagerDto } from '../dtos';
7
7
  import { FileManager, FileManagerBase } from '../entities';
@@ -15,7 +15,8 @@ export declare class FileManagerService extends ApiService<CreateFileManagerDto,
15
15
  protected uploadService: UploadService;
16
16
  private readonly storageConfig;
17
17
  private readonly dataSourceProvider;
18
- constructor(cacheManager: HybridCache, utilsService: UtilsService, uploadService: UploadService, storageConfig: StorageConfigService, dataSourceProvider: StorageDataSourceProvider);
18
+ private readonly permissionCacheService;
19
+ constructor(cacheManager: HybridCache, utilsService: UtilsService, uploadService: UploadService, storageConfig: StorageConfigService, dataSourceProvider: StorageDataSourceProvider, permissionCacheService: SharedPermissionCacheService);
19
20
  protected resolveEntity(): EntityTarget<FileManagerBase>;
20
21
  convertSingleDtoToEntity(dto: CreateFileManagerDto | UpdateFileManagerDto, user: ILoggedUserInfo | null): Promise<FileManagerBase>;
21
22
  getSelectQuery(query: SelectQueryBuilder<FileManagerBase>, _user: ILoggedUserInfo | null, select?: string[]): Promise<{