sergey-telpuk / nestjs-rbac

Awesome RBAC for NestJs
Other
421 stars 36 forks source link

Nest can't resolve dependencies of the StorageRbacService #80

Closed necm1 closed 1 year ago

necm1 commented 1 year ago

Hi!

I was trying to use this package, but I'm facing following error:

[Nest] 9736  - 22.10.2022, 03:55:23   ERROR [ExceptionHandler] 
Nest can't resolve dependencies of the StorageRbacService (?, PermissionService). 
Please make sure that the argument ModuleRef at index [0] is available in the RBAcModule context.

Potential solutions:
- If ModuleRef is a provider, is it part of the current RBAcModule?
- If ModuleRef is exported from a separate @Module, is that module imported within RBAcModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })

It seems like Nest cannot resolve following line: https://github.com/sergey-telpuk/nestjs-rbac/blob/master/src/services/storage.rbac.service.ts#L10

PermissionService:

import { IDynamicStorageRbac, IStorageRbac } from 'nestjs-rbac';

/**
 * @class PermissionService
 * @implements {IDynamicStorageRbac}
 */
export class PermissionService implements IDynamicStorageRbac {
    /**
     * RBAC is not made for database, so we needed to create a workaround
     * for it to work with database.
     *
     * @public
     * @static
     * @type {IStorageRbac}
     */
    public static rbac: IStorageRbac;

    /**
     * @public
     * @async
     * @returns {Promise<IStorageRbac>}
     */
    public async getRbac(): Promise<IStorageRbac> {
        return PermissionService.rbac;
    }
}

Actually I didn't make it injectable, because I cannot even use dependency injection inside it. It tries to create a new instance of PermissionService while the other 2 paramters are optional for providers & imports. It doesn't really make sene for me or it's just not implemented yet.

PermissionService.rbac has following value (it's actually filled with values from the database):

PermissionService.rbac = {
        return {
            roles: [],
            permissions: {},
            grants: {},
            filters: {},
        };
}

Module:

import { Module } from '@nestjs/common';
import { RBAcModule } from 'nestjs-rbac';
import { DatabaseModule } from '../database/database.module';
import { RoleRepository } from './role.repository';
import { PermissionService } from './service/permission.service';

@Module({
    imports: [RBAcModule.forDynamic(PermissionService), DatabaseModule],
    providers: [RoleRepository],
})
/**
 * @class RoleModule
 */
export class RoleModule {}
sergey-telpuk commented 1 year ago

Hi @necm1 thanks for using rbac. I suppose it should be injectable, see example below:


@Injectable() <------------------------- IT'S REQUIRED
export class AsyncService implements IDynamicStorageRbac {
  constructor(
    @Optional() @Inject('ICacheRBAC')
    private readonly cache?: ICacheRBAC,
  ){

  }
  async getRbac(): Promise<IStorageRbac> {
    return new Promise((resolve) => {
      resolve(RBAC);
    });
  }
Vitaliy-Svinchyak commented 1 year ago

Hello! I am facing the same issue in the same usecase, but my DynamicRbacService is already marked @Injectable(). Could you please suggest what am I doing wrong?

My UserModule(is not the root one):

@Module({
  imports: [
    RBAcModule.forDynamic(DynamicRbacService,),
  ],
  providers: [DynamicRbacService, AccessDefinitionRepository],
})
export class UserModule {}

where DynamicRbacService is:

@Injectable()
export class DynamicRbacService implements IDynamicStorageRbac {
  constructor(private readonly repository: AccessDefinitionRepository) {}

Error is this:

Nest can't resolve dependencies of the StorageRbacService (?, DynamicRbacService). Please make sure that the argument ModuleRef at index [0] is available in the RBAcModule context.
sergey-telpuk commented 1 year ago

Hello @Vitaliy-Svinchyak, I got this issue. It's hell, because all my tests work nice. but if we import rbac from node_modules, this issued occurs. I'll think why. Thanks. You can use rbac with fromDynamic as own library. It means to move from node_modules to local

sergey-telpuk commented 1 year ago

@Vitaliy-Svinchyak @necm1 Hello, good news v1.8.4 - works fine but pay attention to supporting version, Support: NestJS ^8.0.0 || ^9.0.0. Thanks!

Vitaliy-Svinchyak commented 1 year ago

@sergey-telpuk Thank you a lot!

Vitaliy-Svinchyak commented 1 year ago

Funny thing is that this fix is working fine with npm. But with yarn when nodeLinker: "pnp" is enabled it is failing with the same issue. But nvm, I will just switch to the nodeLinker: "node-modules". Thank you for fix!

ogheneovo12 commented 1 year ago

Same thing, still encountering the error

sergey-telpuk commented 1 year ago

83

sergey-telpuk commented 1 year ago

Hi @ogheneovo12 fixed v1.9