import { TenancyContext , Tenant } from "@webiny/api-tenancy/types" ; import { Response } from "@webiny/handler-graphql" ; import { getDefaultTenant as baseGetDefaultTenant } from "./getDefaultTenant" ; import { SecurityContext } from "~/types" ; import { GraphQLSchemaPlugin } from "@webiny/handler-graphql/plugins" ; import { NotAuthorizedError } from "~/index" ; export { getDefaultTenant } from "./getDefaultTenant" ; type Context = SecurityContext & TenancyContext ; export interface MultiTenancyAppConfig { /**
     * NOTE: This parameter is only relevant in multi-tenant environments.
     */ verifyIdentityToTenantLink ? : boolean ; } export interface MultiTenancyGraphQLConfig { /**
     * NOTE: This parameter is only relevant in multi-tenant environments.
     */ getDefaultTenant ? ( context ) : Promise < Tenant > ; } export const applyMultiTenancyPlugins = ( config , context ) => { if ( config . verifyIdentityToTenantLink !== false ) { context . security . onAfterLogin . subscribe ( async ( { identity } ) => { const tenant = context . tenancy . getCurrentTenant ( ) ; const link = await context . security . getTenantLinkByIdentity ( { identity : identity . id , tenant : tenant . id } ) ; if ( link ) { return ; } if ( tenant . parent ) { const parentLink = await context . security . getTenantLinkByIdentity ( { identity : identity . id , tenant : tenant . parent } ) ; if ( parentLink ) { return ; } } throw new NotAuthorizedError ( { message : `You're not authorized to access this tenant!` , code : "NOT_AUTHORIZED" } ) ; } ) ; } } ; export const applyMultiTenancyGraphQLPlugins = ( config , context ) => { const getDefaultTenant = config . getDefaultTenant || baseGetDefaultTenant ; context . plugins . register ( new GraphQLSchemaPlugin < Context > ( { typeDefs : `
                extend interface SecurityIdentity {
                    currentTenant: Tenant
                    defaultTenant: Tenant
                }

                extend type TenancyQuery {
                    getDefaultTenant: TenantResponse
                }
            ` , resolvers : { TenancyQuery : { async getDefaultTenant ( _ , __ , context ) { return new Response ( await getDefaultTenant ( context ) ) ; } } , SecurityIdentity : { defaultTenant ( _ , __ , context ) { return getDefaultTenant ( context ) ; } , currentTenant ( _ , __ , context ) { return context . tenancy . getCurrentTenant ( ) ; } } } } ) ) ; } ;