import { SecurityContext } from "@webiny/api-security/types" ; import { ContextPlugin } from "@webiny/handler" ; import { TenancyContext } from "@webiny/api-tenancy/types" ; type Context = TenancyContext & SecurityContext ; export interface GroupAuthorizerConfig { identityType ? : string ; getGroupSlug ( context ) : string ; } export const createGroupAuthorizer = ( config ) => { return new ContextPlugin < Context > ( context => { const { security } = context ; security . addAuthorizer ( async ( ) => { const identity = security . getIdentity ( ) ; const tenant = context . tenancy . getCurrentTenant ( ) ; if ( config . identityType && identity . type !== config . identityType ) { return ; } const groupSlug = config . getGroupSlug ( context ) ; let group = await security . getGroup ( { where : { slug : groupSlug } } ) ; if ( group ) { return group . permissions ; } if ( tenant . parent ) { group = await security . getGroup ( { where : { slug : groupSlug , tenant : tenant . parent } } ) ; return group ? group . permissions : undefined ; } return undefined ; } ) ; } ) ; } ;