import { PbContext , PbSecurityPermission } from "../../../types" ; import { NotAuthorizedError } from "@webiny/api-security" ; import hasRwd from "./hasRwd" ; export default async < TPermission extends PbSecurityPermission = PbSecurityPermission > ( context , name , check ) : Promise < TPermission > => { await context . i18nContent . checkI18NContentPermission ( ) ; const pbPagePermission = await context . security . getPermission < TPermission > ( name ) ; if ( ! pbPagePermission ) { throw new NotAuthorizedError ( ) ; } if ( check . rwd && ! hasRwd ( pbPagePermission , check . rwd ) ) { throw new NotAuthorizedError ( ) ; } if ( check . pw && ! hasPw < TPermission > ( pbPagePermission , check . pw ) ) { throw new NotAuthorizedError ( ) ; } return pbPagePermission ; } ; const hasPw = < TPermission = Record < string , any > > ( permission , pw ) => { const isCustom = Object . keys ( permission ) . length > 1 ; if ( ! isCustom ) { return true ; } if ( typeof permission [ "pw" ] !== "string" ) { return false ; } return permission [ "pw" ] . includes ( pw ) ; } ;