- Implemented Create and Edit pages for products with forms. - Added validation and permissions handling for product actions. - Introduced new product model and related types for better structure. - Enhanced sidebar registry to remove auth dependency for link resolution. - Updated routes to handle precognitive requests for product forms.
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { ModelPolicy, ModelWithPolicies } from "@/domain/model-policies";
|
|
import str from "@/hooks/use-string";
|
|
|
|
export default class AuthService {
|
|
private globalPolicies: Record<string, Record<string, boolean>> = {};
|
|
|
|
public setGlobalPolicies(
|
|
policies: Record<string, Record<string, boolean>>,
|
|
): void {
|
|
this.globalPolicies = policies;
|
|
}
|
|
|
|
/**
|
|
* Vérifie une permission globale (create, viewAny)
|
|
*/
|
|
public canGlobal(action: string, modelClass: string): boolean {
|
|
const key = AuthService.getPolicyKey(modelClass);
|
|
return (this.globalPolicies[key]?.[action] as boolean) ?? false;
|
|
}
|
|
|
|
/**
|
|
* Vérifie une permission contextuelle sur une instance (update, delete, etc.)
|
|
*/
|
|
public static canContextual(
|
|
action: keyof ModelPolicy,
|
|
model: ModelWithPolicies,
|
|
): boolean {
|
|
return model.policies?.[action] ?? false;
|
|
}
|
|
|
|
/**
|
|
* Vérifie intelligemment une permission globale ou contextuelle
|
|
*/
|
|
public can(action: string, subject: string | ModelWithPolicies): boolean {
|
|
// * Global action on a model class
|
|
if (typeof subject === "string") {
|
|
return this.canGlobal(action, subject);
|
|
}
|
|
|
|
// * Global action on a model instance
|
|
if (action === "create" || action === "viewAny") {
|
|
return this.canGlobal(action, subject.modelClass);
|
|
}
|
|
|
|
// * Contextual action on a model instance
|
|
return AuthService.canContextual(action as keyof ModelPolicy, subject);
|
|
}
|
|
|
|
private static getPolicyKey(modelClass: string): string {
|
|
const parts = modelClass.split("\\");
|
|
const basename = parts.at(-1) || modelClass;
|
|
return str(basename).camelCase().plural().value();
|
|
}
|
|
}
|