import { any, can, cannot } from './gate'; import type { AuthorizationResponse } from './gate'; import type { UserModel as OrmUserModel } from '@stacksjs/orm'; /** * Check if a user can perform an ability * * @example * if (await userCan(user, 'edit-settings')) { ... } * if (await userCan(user, 'update', post)) { ... } */ export declare function userCan(user: UserModel | null, ability: string, ...args: any[]): Promise; /** * Check if a user cannot perform an ability * * @example * if (await userCannot(user, 'delete', post)) { ... } */ export declare function userCannot(user: UserModel | null, ability: string, ...args: any[]): Promise; /** * Check if a user can perform any of the given abilities * * @example * if (await userCanAny(user, ['update', 'delete'], post)) { ... } */ export declare function userCanAny(user: UserModel | null, abilities: string[], ...args: any[]): Promise; /** * Check if a user can perform all of the given abilities * * @example * if (await userCanAll(user, ['view', 'update'], post)) { ... } */ export declare function userCanAll(user: UserModel | null, abilities: string[], ...args: any[]): Promise; /** * Authorize a user or throw an exception * * @example * await authorizeUser(user, 'update', post) // Throws if not allowed */ export declare function authorizeUser(user: UserModel | null, ability: string, ...args: any[]): Promise; /** * Get detailed authorization result * * @example * const result = await inspectUser(user, 'update', post) * if (result.denied()) { * console.log(result.message) * } */ export declare function inspectUser(user: UserModel | null, ability: string, ...args: any[]): Promise; /** * Authorizable trait for user models * * Add authorization methods to a user object * * @example * const authorizedUser = withAuthorization(user) * if (await authorizedUser.can('update', post)) { ... } */ export declare function withAuthorization(user: T): T & AuthorizableMethods; /** * Authorization methods interface */ export declare interface AuthorizableMethods { can(ability: string, ...args: any[]): Promise cannot(ability: string, ...args: any[]): Promise canAny(abilities: string[], ...args: any[]): Promise canAll(abilities: string[], ...args: any[]): Promise authorize(ability: string, ...args: any[]): Promise } // Alias the ORM-derived UserModel under the name the rest of this module // expects. Using the row/instance shape (rather than `typeof User`) lets // callers pass plain authenticated user objects to the gate helpers. declare type UserModel = OrmUserModel; /** * Type for a user with authorization methods */ export type AuthorizableUser = T & AuthorizableMethods;