import { type ArrayOrValue, type IterableOrValue, type Maybe, type SetIncludesMode } from '@dereekb/util'; /** * A granted role for a model. * * @semanticType * @semanticTopic identifier * @semanticTopic string * @semanticTopic dereekb-model:auth */ export type GrantedRole = string; /** * Communicates that the current context is a system admin. */ export type GrantedSysAdminRole = typeof GRANTED_SYS_ADMIN_ROLE_KEY; export declare const GRANTED_SYS_ADMIN_ROLE_KEY = "sysadmin"; /** * Communicates that the current context is an admin/owner. */ export type GrantedOwnerRole = typeof GRANTED_OWNER_ROLE_KEY; export declare const GRANTED_OWNER_ROLE_KEY = "owner"; /** * Communicates that the current context is an admin. */ export type GrantedAdminRole = typeof GRANTED_ADMIN_ROLE_KEY; export declare const GRANTED_ADMIN_ROLE_KEY = "admin"; /** * Checks whether the given role represents an admin-level permission (either "admin" or "owner"). * * @param role - The role to check. * @returns True if the role is an admin or owner role. * * @example * ```typescript * isGrantedAdminLevelRole('admin'); // true * isGrantedAdminLevelRole('owner'); // true * isGrantedAdminLevelRole('read'); // false * ``` */ export declare function isGrantedAdminLevelRole(role: GrantedRole): boolean; /** * Communicates that the current context has read access to a model. */ export type GrantedReadRole = typeof GRANTED_READ_ROLE_KEY; export declare const GRANTED_READ_ROLE_KEY = "read"; /** * Communicates that the current context has update access to a model. */ export type GrantedUpdateRole = typeof GRANTED_UPDATE_ROLE_KEY; export declare const GRANTED_UPDATE_ROLE_KEY = "update"; /** * Communicates that the current context has delete access to a model. */ export type GrantedDeleteRole = typeof GRANTED_DELETE_ROLE_KEY; export declare const GRANTED_DELETE_ROLE_KEY = "delete"; export type GrantedCrudRoles = GrantedReadRole | GrantedUpdateRole | GrantedDeleteRole; export declare const FULL_ACCESS_ROLE_KEY = "__FULL__"; /** * Communicates that the current context has full access to a model. */ export type GrantedFullAccessGrantedRole = typeof FULL_ACCESS_ROLE_KEY; export declare const NO_ACCESS_ROLE_KEY = "__EMPTY__"; export type NoAccessGrantedRole = typeof NO_ACCESS_ROLE_KEY; export type NoAccessRoleMap = { [NO_ACCESS_ROLE_KEY]: true; }; /** * Creates a {@link GrantedRoleMap} that explicitly denies all access. * * @returns A role map with only the no-access marker set. * * @example * ```typescript * const map = noAccessRoleMap(); * isNoAccessRoleMap(map); // true * ``` */ export declare function noAccessRoleMap(): GrantedRoleMap; /** * Type guard that checks whether a role map is a no-access map. * * @param input - The role map to check. * @returns True if the map has the no-access marker. */ export declare function isNoAccessRoleMap(input: GrantedRoleMap | NoAccessRoleMap): input is NoAccessRoleMap; export type FullAccessRoleMap = { [FULL_ACCESS_ROLE_KEY]: true; }; /** * Creates a {@link GrantedRoleMap} that grants full access to all roles. * * @returns A role map with the full-access marker set. * * @example * ```typescript * const map = fullAccessRoleMap(); * isFullAccessRoleMap(map); // true * ``` */ export declare function fullAccessRoleMap(): GrantedRoleMap; /** * Type guard that checks whether a role map is a full-access map. * * @param input - The role map to check. * @returns True if the map has the full-access marker. */ export declare function isFullAccessRoleMap(input: GrantedRoleMap | FullAccessRoleMap): input is FullAccessRoleMap; export type GrantedRoleMap = NoAccessRoleMap | FullAccessRoleMap | GrantedRoleKeysMap; export type GrantedRoleKeysMap = { [key in R]?: Maybe; }; /** * Values keyed by roles. */ export type GrantedRoleTruthMapObject = Partial<{ [K in R]: T; }>; /** * Output that removes the keys of all roles that are not contained. */ export type GrantedRoleTruthMap> = M extends GrantedRoleTruthMapObject ? Partial<{ [K in R]: T; }> : never; /** * Reader used for parsing/reading role values from a given context. */ export interface GrantedRoleMapReader { /** * Returns true if no access has been given. */ hasNoAccess(): boolean; /** * Creates a truth map from the input. * * @param values */ truthMap>(input: M): GrantedRoleTruthMap; /** * Returns true if the role is granted. */ hasRole(role: R): boolean; /** * Returns true if the roles are granted. */ hasRoles(setIncludes: SetIncludesMode, roles: ArrayOrValue | IterableOrValue): boolean; /** * Returns true if the map explicitly contains the role. */ containsRoles(setIncludes: SetIncludesMode, roles: ArrayOrValue | IterableOrValue): boolean; } /** * Creates a {@link GrantedRoleMapReader} for querying granted roles from a role map. * * The reader handles full-access and no-access maps as special cases, and provides methods * for checking individual roles or sets of roles. * * @param map - The granted role map to read from. * @returns A reader instance for querying the map. * * @example * ```typescript * const roleMap: GrantedRoleMap = { read: true, first: true }; * const reader = grantedRoleMapReader(roleMap); * * reader.hasRole('read'); // true * reader.hasRole('delete'); // false * reader.containsRoles('any', ['read']); // true * reader.containsRoles('all', ['read', 'delete']); // false * ``` */ export declare function grantedRoleMapReader(map: GrantedRoleMap): GrantedRoleMapReader; export declare class GrantedRoleMapReaderInstance implements GrantedRoleMapReader { private readonly _map; constructor(map: GrantedRoleMap); hasNoAccess(): boolean; truthMap>(input: M): GrantedRoleTruthMap; hasRole(role: R): boolean; hasRoles(setIncludes: SetIncludesMode, inputRoles: IterableOrValue): boolean; containsRoles(setIncludes: SetIncludesMode, inputRoles: IterableOrValue): boolean; containsAnyRole(roles: ArrayOrValue): boolean; containsEachRole(roles: ArrayOrValue): boolean; } /** * Converts an array of role strings into a {@link GrantedRoleKeysMap} where each role is mapped to the given boolean value. * * @param roles - Role keys to include in the resulting map. * @param value - Truthiness to assign to every role entry (defaults to true). * @returns Map keyed by role with the supplied truthiness for each entry. * * @example * ```typescript * const map = grantedRoleKeysMapFromArray(['read', 'update']); * // { read: true, update: true } * ``` */ export declare function grantedRoleKeysMapFromArray(roles: R[], value?: boolean): GrantedRoleKeysMap;