@tmlmobilidade/types
Version:
291 lines (290 loc) • 11.6 kB
TypeScript
import { z } from 'zod';
export declare const PermissionSchema: z.ZodDiscriminatedUnion<"scope", [z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
scope: z.ZodLiteral<"agencies">;
}, "strip", z.ZodTypeAny, {
scope: "agencies";
action: "create" | "update" | "delete" | "read" | "lock";
}, {
scope: "agencies";
action: "create" | "update" | "delete" | "read" | "lock";
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
scope: z.ZodLiteral<"alerts_scheduled">;
}, "strip", z.ZodTypeAny, {
scope: "alerts_scheduled";
action: "create" | "update" | "delete" | "read" | "lock";
}, {
scope: "alerts_scheduled";
action: "create" | "update" | "delete" | "read" | "lock";
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
resources: z.ZodDefault<z.ZodObject<{
agency_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
}, "strip", z.ZodTypeAny, {
agency_ids: string[];
}, {
agency_ids?: string[] | undefined;
}>>;
scope: z.ZodLiteral<"alerts_realtime">;
}, "strip", z.ZodTypeAny, {
scope: "alerts_realtime";
action: "create" | "update" | "delete" | "read" | "lock";
resources: {
agency_ids: string[];
};
}, {
scope: "alerts_realtime";
action: "create" | "update" | "delete" | "read" | "lock";
resources?: {
agency_ids?: string[] | undefined;
} | undefined;
}>, z.ZodObject<{
action: z.ZodEnum<["acceptance_change_status", "acceptance_justify", "acceptance_lock", "acceptance_read", "analsys_lock", "analysis_lock", "analysis_read", "analysis_reprocess", "analysis_update", "audit_lock", "audit_read", "audit_update"]>;
resources: z.ZodDefault<z.ZodObject<{
agency_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
}, "strip", z.ZodTypeAny, {
agency_ids: string[];
}, {
agency_ids?: string[] | undefined;
}>>;
scope: z.ZodLiteral<"rides">;
}, "strip", z.ZodTypeAny, {
scope: "rides";
action: "acceptance_change_status" | "acceptance_justify" | "acceptance_lock" | "acceptance_read" | "analsys_lock" | "analysis_lock" | "analysis_read" | "analysis_reprocess" | "analysis_update" | "audit_lock" | "audit_read" | "audit_update";
resources: {
agency_ids: string[];
};
}, {
scope: "rides";
action: "acceptance_change_status" | "acceptance_justify" | "acceptance_lock" | "acceptance_read" | "analsys_lock" | "analysis_lock" | "analysis_read" | "analysis_reprocess" | "analysis_update" | "audit_lock" | "audit_read" | "audit_update";
resources?: {
agency_ids?: string[] | undefined;
} | undefined;
}>, z.ZodObject<{
action: z.ZodEnum<["read"]>;
scope: z.ZodLiteral<"sams">;
}, "strip", z.ZodTypeAny, {
scope: "sams";
action: "read";
}, {
scope: "sams";
action: "read";
}>, z.ZodObject<{
action: z.ZodEnum<["create", "read", "lock", "request_approval"]>;
resources: z.ZodDefault<z.ZodObject<{
agency_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
}, "strip", z.ZodTypeAny, {
agency_ids: string[];
}, {
agency_ids?: string[] | undefined;
}>>;
scope: z.ZodLiteral<"gtfs_validations">;
}, "strip", z.ZodTypeAny, {
scope: "gtfs_validations";
action: "create" | "read" | "lock" | "request_approval";
resources: {
agency_ids: string[];
};
}, {
scope: "gtfs_validations";
action: "create" | "read" | "lock" | "request_approval";
resources?: {
agency_ids?: string[] | undefined;
} | undefined;
}>, z.ZodObject<{
action: z.ZodEnum<["read_links", "read_wiki"]>;
scope: z.ZodLiteral<"home">;
}, "strip", z.ZodTypeAny, {
scope: "home";
action: "read_links" | "read_wiki";
}, {
scope: "home";
action: "read_links" | "read_wiki";
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
scope: z.ZodLiteral<"organizations">;
}, "strip", z.ZodTypeAny, {
scope: "organizations";
action: "create" | "update" | "delete" | "read" | "lock";
}, {
scope: "organizations";
action: "create" | "update" | "delete" | "read" | "lock";
}>, z.ZodObject<{
action: z.ZodEnum<["read"]>;
scope: z.ZodLiteral<"performance">;
}, "strip", z.ZodTypeAny, {
scope: "performance";
action: "read";
}, {
scope: "performance";
action: "read";
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "read_controller", "read_pcgi_legacy", "lock", "update", "update_controller", "update_feed_info_dates", "update_gtfs_plan", "update_pcgi_legacy"]>;
resources: z.ZodDefault<z.ZodObject<{
agency_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
}, "strip", z.ZodTypeAny, {
agency_ids: string[];
}, {
agency_ids?: string[] | undefined;
}>>;
scope: z.ZodLiteral<"plans">;
}, "strip", z.ZodTypeAny, {
scope: "plans";
action: "create" | "update" | "delete" | "read" | "lock" | "read_controller" | "read_pcgi_legacy" | "update_controller" | "update_feed_info_dates" | "update_gtfs_plan" | "update_pcgi_legacy";
resources: {
agency_ids: string[];
};
}, {
scope: "plans";
action: "create" | "update" | "delete" | "read" | "lock" | "read_controller" | "read_pcgi_legacy" | "update_controller" | "update_feed_info_dates" | "update_gtfs_plan" | "update_pcgi_legacy";
resources?: {
agency_ids?: string[] | undefined;
} | undefined;
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
scope: z.ZodLiteral<"roles">;
}, "strip", z.ZodTypeAny, {
scope: "roles";
action: "create" | "update" | "delete" | "read" | "lock";
}, {
scope: "roles";
action: "create" | "update" | "delete" | "read" | "lock";
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
resources: z.ZodDefault<z.ZodObject<{
agency_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
municipality_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
}, "strip", z.ZodTypeAny, {
municipality_ids: string[];
agency_ids: string[];
}, {
municipality_ids?: string[] | undefined;
agency_ids?: string[] | undefined;
}>>;
scope: z.ZodLiteral<"stops">;
}, "strip", z.ZodTypeAny, {
scope: "stops";
action: "create" | "update" | "delete" | "read" | "lock";
resources: {
municipality_ids: string[];
agency_ids: string[];
};
}, {
scope: "stops";
action: "create" | "update" | "delete" | "read" | "lock";
resources?: {
municipality_ids?: string[] | undefined;
agency_ids?: string[] | undefined;
} | undefined;
}>, z.ZodObject<{
action: z.ZodEnum<["create", "delete", "read", "lock", "update"]>;
scope: z.ZodLiteral<"users">;
}, "strip", z.ZodTypeAny, {
scope: "users";
action: "create" | "update" | "delete" | "read" | "lock";
}, {
scope: "users";
action: "create" | "update" | "delete" | "read" | "lock";
}>, z.ZodObject<{
action: z.ZodEnum<["create_annotations", "delete_annotations", "read_annotations", "update_annotations", "lock_annotations"]>;
resources: z.ZodDefault<z.ZodObject<{
agency_ids: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
}, "strip", z.ZodTypeAny, {
agency_ids: string[];
}, {
agency_ids?: string[] | undefined;
}>>;
scope: z.ZodLiteral<"dates">;
}, "strip", z.ZodTypeAny, {
scope: "dates";
action: "create_annotations" | "delete_annotations" | "read_annotations" | "update_annotations" | "lock_annotations";
resources: {
agency_ids: string[];
};
}, {
scope: "dates";
action: "create_annotations" | "delete_annotations" | "read_annotations" | "update_annotations" | "lock_annotations";
resources?: {
agency_ids?: string[] | undefined;
} | undefined;
}>]>;
export type Permission = z.infer<typeof PermissionSchema>;
export type ActionsOf<S extends Permission['scope']> = Extract<Permission, {
scope: S;
}>['action'];
export type PermissionCatalogType = {
[S in Permission['scope']]: {
actions: {
[A in ActionsOf<S>]: A;
};
scope: S;
};
};
/**
* Arguments for hasPermissionResource function.
* @param T The type of the resource.
*/
export interface HasPermissionResourceArgs {
action: string;
permissions: Permission[];
resource_key: string;
scope: string;
value: unknown;
}
/**
* PermissionCatalog provides a structured catalog of all available permissions
* in the system, categorized by scope and their respective actions.
* Use it to reference required permissions in components and services.
*/
export declare class PermissionCatalog {
static readonly ALLOW_ALL_FLAG = "allow_all";
/**
* Generates the complete permission catalog by extracting
* scopes and actions from the defined PermissionSchema.
* @return A catalog object mapping scopes to their actions.
*/
static get all(): PermissionCatalogType;
/**
* Get a specific permission from a full list by scope and action.
* @param permissionEntries The full list of permissions of the user.
* @param scope The resource scope of the permission to filter by.
* @param action The action of the permission to filter by.
* @returns The filtered Permission object or undefined if not found.
*/
static get<S extends Permission['scope']>(permissionEntries: Permission[], scope: S, action: ActionsOf<S>): Extract<Permission, {
action: ActionsOf<S>;
scope: S;
}> | undefined;
/**
* Check if a list of permission entries has the requested scope/action pair.
* @param permissionEntries The list of permission entries to check against.
* @param scope The required scope to check.
* @param action The required action to check.
* @returns The permission object or undefined if not found.
*/
static hasPermission<S extends Permission['scope']>(permissionEntries: Permission[], scope: S, action: ActionsOf<S>): boolean;
/**
* Check if a permission exists in a list of permissions, with additional check for a given resource value.
* If a `value` exists in a `resource` of a User `permissions` object that
* matches the given `action` and `scope`. For example, if you want to check if
* a user has access to a specific `agency_id`, you set `value=43` and `resource_key='agency_ids'`.
* If the provided `permissions` object contains the value `43` inside the `scope='plans'`,
* `action='create'` and `resource_key='agency_ids'` the function will return true.
* @param permissions The list of permissions (from a user or request).
* @param value The permission value to check against.
* @param resource_key The key of the resource.
* @param scope The scope of the permission.
* @param action The action of the permission.
* @returns The permission.
*/
static hasPermissionResource({ action, permissions, resource_key, scope, value }: HasPermissionResourceArgs): boolean;
/**
* Sanitizes a list of permissions by removing any entries
* that do not correspond to valid scopes and actions
* defined in the PermissionCatalog.
* @param existingEntries Array of Permission objects to sanitize.
* @return A cleaned array containing only valid permissions.
*/
static sanitize(existingEntries: Permission[]): Permission[];
}