sergey-telpuk / nestjs-rbac

Awesome RBAC for NestJs
Other
426 stars 36 forks source link

Using RBAC like a Dynamic Service - does not work #83

Closed dmitrye closed 1 year ago

dmitrye commented 1 year ago

Getting the same issue as https://github.com/sergey-telpuk/nestjs-rbac/issues/70.

NestJS: 9.4.0 Typescript: 4.9.5 nestjs-rbac: 1.8.4 TypeORM: 9.0.1

error TS2345: Argument of type 'typeof DynamicRbacService' is not assignable to parameter of type 'new () => IDynamicStorageRbac'.
  Types of construct signatures are incompatible.
    Type 'new (roleRepository: MongoRepository<Role>, permissionRepository: MongoRepository<Permission>) => DynamicRbacService' is not assignable to type 'new () => IDynamicStorageRbac'.

I even tested the exact example from the README and it also fails with the same errors. TypeScript is what's generating the error because the underlying signature does not support parameters in the constructor.

import { Module } from '@nestjs/common';
import { RBAcModule } from 'nestjs-rbac';

@Module({
  imports: [
    RBAcModule.forDynamic(DynamicStorageService),
  ],
  controllers: []
})
export class AppModule {}
// implement dynamic storage
import { IDynamicStorageRbac, IStorageRbac } from 'nestjs-rbac';
@Injectable()
export class  DynamicStorageService implements IDynamicStorageRbac {
  constructor(
    private readonly repository: AnyRepository
  ) {

  }
  async getRbac(): Promise<IStorageRbac> {
//use any persistence storage for getting `RBAC`
      return  await this.repository.getRbac();
  }
}

It's therefore a bug since you're documenting it as a supported feature.

ogheneovo12 commented 1 year ago

what's the update on this

sergey-telpuk commented 1 year ago

@ogheneovo12 fixed v1.9, pls try it out and let me know how it works, thanks

ogheneovo12 commented 1 year ago

So I tried it out and still encountered same errors,

src/app.module.ts:43:27 - error TS2345: Argument of type 'typeof RbacService' is not assignable to parameter of type 'new () => IDynamicStorageRbac'.
  Types of construct signatures are incompatible.
    Type 'new (roleRepository: Model<RoleDocument, {}, {}, {}, any>, permissionRepository: Model<PermisionDocument, {}, {}, {}, any>) => RbacService' is not assignable to type 'new () => IDynamicStorageRbac'.

43     RBAcModule.forDynamic(RbacService),
                             ~~~~~~~~~~~
[7:15:13 AM] Found 1 error. Watching for file changes.

created a fork of the repo, and updated the forDynamic Typings here in #84

installed my fork of the repo with typing fix, the typing error was gone, then a similar issue in #80 came up again

[Nest] 19527  - 05/02/2023, 7:26:26 AM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the RBAcModule (?). Please make sure that the argument ModuleRef at index [0] is available in the RBAcModule context.

looking to see if i can find a fix

sergey-telpuk commented 1 year ago

Hi @ogheneovo12, could you provide a test repo this issue?

ogheneovo12 commented 1 year ago

just tested again and the error is gone

sergey-telpuk commented 1 year ago

fixed in v1.9.1