import { ArrayUtil , CommonUtil } from "./utils/" ; import { Access , IAccessInfo , ICondition , IDictionary , Query , IQueryInfo , Permission , AccessControlError , IFunctionCondition } from "./core" ; import { ConditionUtil } from "./conditions" ; classdesc author Onur Yıldırım <onur@cutepilot.com>
 *   license class global class AccessControl { private private _grants : any ; param { Object | Array } grants param { Object } customConditionFns constructor ( grants = { } , customConditionFns = { } ) { ConditionUtil . resetCustomConditionFunctions ( ) ; ConditionUtil . setCustomConditionFunctions ( customConditionFns ) ; this . setGrants ( grants ) ; } return { Object } getGrants ( ) { return this . _grants ; } chainable param { Object | Array } grantsObject returns { AccessControl } setGrants ( grantsObject ) { this . _grants = { } ; let type = CommonUtil . type ( grantsObject ) ; if ( type === "object" ) { this . _grants = CommonUtil . normalizeGrantsObject ( grantsObject ) ; } else if ( type === "array" ) { grantsObject . filter ( ( grant => ! grant . extend || ! grant . extend . length ) ) . forEach ( ( item ) => CommonUtil . commitToGrants ( this . _grants , item ) ) ; grantsObject . filter ( ( item => item . extend && item . extend . length ) ) . forEach ( ( item ) => CommonUtil . extendRole ( this . _grants , item . role , item . extend ) ) ; } return this ; } chainable returns { AccessControl } reset ( ) { this . _grants = { } ; ConditionUtil . resetCustomConditionFunctions ( ) ; return this ; } chainable param { String | Array < String > } roles param { String | Array < String > } extenderRoles returns { AccessControl } throws extendRole ( roles , extenderRoles , condition ? ) { return this . extendRoleSync ( roles , extenderRoles , condition ) ; } extendRoleSync ( roles , extenderRoles , condition ? ) { CommonUtil . extendRoleSync ( this . _grants , roles , extenderRoles , condition ) ; return this ; } chainable param { String | Array < String > } roles returns { AccessControl } removeRoles ( roles ) { let rolesToRemove = ArrayUtil . toStringArray ( roles ) . sort ( ) ; this . _each ( ( role , roleItem ) => { if ( roleItem . $extend ) { rolesToRemove . forEach ( role => { if ( roleItem . $extend [ role ] ) { roleItem . score -= this . _grants [ role ] . score ; delete roleItem . $extend [ role ] ; } } ) ; } } ) ; rolesToRemove . forEach ( ( role ) => { delete this . _grants [ role ] ; } ) ; return this ; } returns { Array < String > } example getRoles ( ) { return Object . keys ( this . _grants ) ; } param { String } role returns { Boolean } hasRole ( role ) { return this . _grants . hasOwnProperty ( role ) ; } returns { IAccessInfo [ ] } async allowedGrants ( query ) { return CommonUtil . getUnionGrantsOfRoles ( this . _grants , query ) ; } allowedGrantsSync ( query ) { return CommonUtil . getUnionGrantsOfRolesSync ( this . _grants , query ) ; } param { IQueryInfo } query returns { String [ ] } async allowingRoles ( query ) { return CommonUtil . getAllowingRoles ( this . _grants , query ) ; } allowingRolesSync ( query ) { return CommonUtil . getAllowingRolesSync ( this . _grants , query ) ; } param { IQueryInfo } query returns { String [ ] } async allowedActions ( query ) { return CommonUtil . getUnionActionsOfRoles ( this . _grants , query ) ; } allowedActionsSync ( query ) { return CommonUtil . getUnionActionsOfRolesSync ( this . _grants , query ) ; } param { IQueryInfo } query returns { String [ ] } async allowedResources ( query ) { return CommonUtil . getUnionResourcesOfRoles ( this . _grants , query ) ; } allowedResourcesSync ( query ) { return CommonUtil . getUnionResourcesOfRolesSync ( this . _grants , query ) ; } name alias function chainable param { String | Array | IQueryInfo } role - A single role (as a string),
     *      a list of roles (as an array) or an  {@link ?api=ac#AccessControl~IQueryInfo|`IQueryInfo` object} *      that fully or partially defines the access to be checked.
     *
     *   returns { Query } - The returned object provides chainable
     *      methods to define and query the access permissions to be checked.
     *      See  {@link ?api=ac#AccessControl~Query|`Query` inner class} .
     *
     *   example can ( role ) { return new Query ( this . _grants , role ) ; } private access ( role ) { return this . can ( role ) ; } param { IQueryInfo } queryInfo A fulfilled  {@link ?api=ac#AccessControl~IQueryInfo|`IQueryInfo` object} .
     *
     *   returns { Permission } - An object that provides properties
     *  and methods that defines the granted access permissions. See
     *   {@link ?api=ac#AccessControl~Permission|`Permission` inner class} .
     *
     *   example async permission ( queryInfo ) { return new Permission ( queryInfo , await CommonUtil . getUnionAttrsOfRoles ( this . _grants , queryInfo ) ) ; } permissionSync ( queryInfo ) { return new Permission ( queryInfo , CommonUtil . getUnionAttrsOfRolesSync ( this . _grants , queryInfo ) ) ; } name alias function chainable param { String | Array < String > | IAccessInfo } role A single role (as a string), a list of roles (as an array) or an
     *          {@link ?api=ac#AccessControl~IAccessInfo|`IAccessInfo` object} *         that fully or partially defines the access to be granted.
     *
     *   return { Access } *          The returned object provides chainable properties to build and
     *          define the access to be granted. See the examples for details.
     *          See  {@link ?api=ac#AccessControl~Access|`Access` inner class} .
     *
     *   example grant ( role ) { return new Access ( this . _grants , role ) ; } /**
     *  Alias of `grant()`.
     */ allow ( role ) { return this . grant ( role ) ; } /**
     * Converts grants object to JSON format
     */ toJSON ( ) { return CommonUtil . toExtendedJSON ( { grants : this . _grants , customConditionFunctions : ConditionUtil . getCustomConditionFunctions ( ) } ) ; } registerConditionFunction ( funtionName , fn ) { ConditionUtil . registerCustomConditionFunction ( funtionName , fn ) ; return this ; } private private _each ( callback ) { CommonUtil . eachKey ( this . _grants , ( role ) => callback ( role , this . _grants [ role ] ) ) ; } private static get Error ( ) : any { return AccessControlError ; } param { Object | Array } data param { Array | String } attributes returns { Object | Array } example static filter ( data , attributes ) { return CommonUtil . filterAll ( data , attributes ) ; } name alias function param { Any } object returns { Boolean } static isACError ( object ) { return object instanceof AccessControlError ; } private static isAccessControlError ( object ) { return AccessControl . isACError ( object ) ; } param aclJSON static fromJSON ( aclJSON ) { let aclObj = CommonUtil . fromExtendedJSON ( aclJSON ) ; return new AccessControl ( aclObj . grants , aclObj . customConditionFunctions ) ; } } export { AccessControl } ;