import { GroupTenantLink , SecurityContext } from "~/types" ; import { ContextPlugin } from "@webiny/handler" ; import { TenancyContext } from "@webiny/api-tenancy/types" ; type Context = SecurityContext & TenancyContext ; export interface Config { identityType ? : string ; } export const createGroupAuthorizer = ( config ) => ( { security , tenancy } ) => async ( ) => { const identity = security . getIdentity ( ) ; const tenant = tenancy . getCurrentTenant ( ) ; if ( ! identity || identity . type !== config . identityType ) { return null ; } const tenantLink = await security . getTenantLinkByIdentity < GroupTenantLink > ( { identity : identity . id , tenant : tenant . id } ) ; if ( ! tenantLink || ! tenantLink . data . permissions ) { return null ; } return tenantLink . data . permissions ; } ; export default ( config ) => { return new ContextPlugin < SecurityContext & TenancyContext > ( context => { context . security . addAuthorizer ( createGroupAuthorizer ( config ) ( context ) ) ; } ) ; } ;