import type { UserModel as OrmUserModel } from '@stacksjs/orm'; /** * Define a new authorization gate * * @example * define('edit-settings', (user) => user?.isAdmin) * define('update-post', (user, post) => user?.id === post.userId) */ export declare function define(ability: string, callback: GateCallback): void; /** * Register a policy for a model * * @example * policy('Post', PostPolicy) * policy(Post, PostPolicy) */ export declare function policy(model: string | { name: string }, policyClass: new () => Policy): void; /** * Register a callback to run before all gate checks * * @example * before((user, _ability) => { * if (user?.isSuperAdmin) return true // Super admins can do anything * return null // Continue to normal checks * }) */ export declare function before(callback: (user: UserModel | null, ability: string, args: any[]) => boolean | null | Promise): void; /** * Register a callback to run after all gate checks */ export declare function after(callback: (user: UserModel | null, ability: string, result: boolean, args: any[]) => boolean | void | Promise): void; /** * Check if the user is allowed to perform an ability * * @example * if (await allows('edit-settings', user)) { ... } * if (await allows('update', user, post)) { ... } */ export declare function allows(ability: string, user: UserModel | null, ...args: any[]): Promise; /** * Check if the user is denied from performing an ability * * @example * if (await denies('delete', user, post)) { ... } */ export declare function denies(ability: string, user: UserModel | null, ...args: any[]): Promise; /** * Check if the user can perform an ability (alias for allows) */ export declare function can(ability: string, user: UserModel | null, ...args: any[]): Promise; /** * Check if the user cannot perform an ability (alias for denies) */ export declare function cannot(ability: string, user: UserModel | null, ...args: any[]): Promise; /** * Check if the user can perform any of the given abilities * * @example * if (await any(['update', 'delete'], user, post)) { ... } */ export declare function any(abilities: string[], user: UserModel | null, ...args: any[]): Promise; /** * Check if the user can perform all of the given abilities * * @example * if (await all(['view', 'update'], user, post)) { ... } */ export declare function all(abilities: string[], user: UserModel | null, ...args: any[]): Promise; /** * Check if the user can perform none of the given abilities */ export declare function none(abilities: string[], user: UserModel | null, ...args: any[]): Promise; /** * Authorize an ability or throw an exception * * @example * await authorize('update', user, post) // Throws if not allowed */ export declare function authorize(ability: string, user: UserModel | null, ...args: any[]): Promise; /** * Get detailed inspection result for an ability check */ export declare function inspect(ability: string, user: UserModel | null, ...args: any[]): Promise; /** * Get a policy instance for a model */ export declare function getPolicyFor(model: T): Policy | null; /** * Check if a gate is defined */ export declare function has(ability: string): boolean; /** * Check if a policy is registered for a model */ export declare function hasPolicy(model: string | { name: string }): boolean; /** * Get all defined gate names */ export declare function abilities(): string[]; /** * Clear all gates and policies (useful for testing) */ export declare function flush(): void; /** * Gate facade for convenient access */ export declare const Gate: { define: typeof define; policy: typeof policy; before: typeof before; after: typeof after; allows: typeof allows; denies: typeof denies; can: typeof can; cannot: typeof cannot; any: typeof any; all: typeof all; none: typeof none; authorize: typeof authorize; inspect: typeof inspect; has: typeof has; hasPolicy: typeof hasPolicy; abilities: typeof abilities; getPolicyFor: typeof getPolicyFor; flush: typeof flush; AuthorizationResponse: typeof AuthorizationResponse; AuthorizationException: typeof AuthorizationException }; /** * Policy class interface */ export declare interface Policy { before?(user: UserModel | null, ability: string): boolean | null | Promise viewAny?(user: UserModel | null): boolean | Promise | AuthorizationResponse view?(user: UserModel | null, model: T): boolean | Promise | AuthorizationResponse create?(user: UserModel | null): boolean | Promise | AuthorizationResponse update?(user: UserModel | null, model: T): boolean | Promise | AuthorizationResponse delete?(user: UserModel | null, model: T): boolean | Promise | AuthorizationResponse restore?(user: UserModel | null, model: T): boolean | Promise | AuthorizationResponse forceDelete?(user: UserModel | null, model: T): boolean | Promise | AuthorizationResponse [key: string]: PolicyMethod | undefined } // Alias the ORM-derived UserModel under the name this module uses internally. // The gate API receives authenticated user objects (rows / instances), // not the User class constructor. declare type UserModel = OrmUserModel; /** * Gate callback function type */ export type GateCallback = (_user: UserModel | null, ..._args: T[]) => boolean | Promise | AuthorizationResponse; /** * Policy method type. The return type intentionally allows `null` so that * a policy's `before()` hook (which returns `null` to delegate to the * underlying ability check) is index-compatible with the catch-all * `[key: string]: PolicyMethod | undefined` signature on `Policy`. */ export type PolicyMethod = (_user: UserModel | null, _model?: T, ..._args: any[]) => boolean | null | Promise | AuthorizationResponse; /** * Authorization response for detailed allow/deny */ export declare class AuthorizationResponse { readonly isAllowed: boolean; readonly message?: string; readonly code?: string; constructor(allowed: boolean, message?: string, code?: string); static allow(message?: string): AuthorizationResponse; static deny(message?: string, code?: string): AuthorizationResponse; allowed(): boolean; denied(): boolean; authorize(): void; } /** * Authorization exception */ export declare class AuthorizationException extends Error { public readonly code?: string; public readonly status?: number; constructor(message?: string, code?: string, status?: number); } export default Gate;