Angular (2/4) something similar to Symfony (2/3) tagged services

66 views Asked by At

I'm trying to achieve something similar to Symfony's tagged services.

What is it going to be used for: I want to reproduce "voters" into my front-logic (yes, im aware that frontend technology is not going to give me 100% sure of real permission, that's the reason i need such logic to "translate" backend into frontend).

What i want to get on success: A signle service that contains array of configured services (by constructor, setter or whatever). The best would be also possibility to use this module in any sub-module(s) using forChild static method, so the service will contain voters from both main & sub module(s).

What i've done so far: I've created an provider in my module

export const PERMISSION_VOTERS = new InjectionToken<PermissionCheckerService>('PermissionCheckerService');

@NgModule({...})
export class SecurityModule {
    /**
     * @param {Array<IPermissionVoter>} config
     * @returns {ModuleWithProviders}
     */
    static forRoot (config: {voters: Array<Type<IPermissionVoter>>} = {voters: []}): ModuleWithProviders {
        return {
            ngModule:  SecurityModule,
            providers: [
                <ClassProvider>{
                    useClass: PermissionCheckerService,
                    provide:  PERMISSION_VOTERS,
                    useValue: config,
                    multi:    true
                }
            ],
        };
    }
}

Then, i've created the PermissionCheckerService, and IsGrantedComponent

Somehow, i've managed to get instance of PermissionCheckerService into my IsGrantedComponent but i can't understand, why array of voters is empty, even if i do provide them as follows:

    SecurityModule.forRoot({
        voters: [UserPermissionVoter]
    }),

I'm afraid that i missunderstood documentation somewhere (were reading documentation and trying to compare "similar" logic from 3rd party lib's).

0

There are 0 answers