File size: 2,048 Bytes
dc11950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d2a779
dc11950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * Permission utilities for role-based access control
 * Maps frontend roles to backend permission actions
 */

export type UserRole = "viewer" | "editor" | "admin" | "owner";

/**
 * Permission actions that match backend definitions
 */
type PermissionAction = 
  | "manage_rules"      // Admin/Owner only
  | "ingest_documents"  // Editor/Admin/Owner
  | "delete_documents"  // Admin/Owner only
  | "view_analytics";   // Admin/Owner only

/**
 * Permission matrix matching backend access_control.py
 */
const PERMISSIONS: Record<PermissionAction, UserRole[]> = {
  manage_rules: ["admin", "owner"],
  ingest_documents: ["editor", "admin", "owner"],
  delete_documents: ["admin", "owner"],
  view_analytics: ["viewer", "editor", "admin", "owner"],
};

/**
 * Check if a role has permission for an action
 */
export function hasPermission(role: UserRole, action: PermissionAction): boolean {
  const allowedRoles = PERMISSIONS[action];
  return allowedRoles.includes(role);
}

/**
 * Check if user can manage rules (admin/owner only)
 */
export function canManageRules(role: UserRole): boolean {
  return hasPermission(role, "manage_rules");
}

/**
 * Check if user can ingest documents (editor/admin/owner)
 */
export function canIngestDocuments(role: UserRole): boolean {
  return hasPermission(role, "ingest_documents");
}

/**
 * Check if user can delete documents (admin/owner only)
 */
export function canDeleteDocuments(role: UserRole): boolean {
  return hasPermission(role, "delete_documents");
}

/**
 * Check if user can view analytics (admin/owner only)
 */
export function canViewAnalytics(role: UserRole): boolean {
  return hasPermission(role, "view_analytics");
}

/**
 * Check if user has admin-level access (admin or owner)
 */
export function isAdminOrOwner(role: UserRole): boolean {
  return role === "admin" || role === "owner";
}

/**
 * Check if user has editor-level access or higher
 */
export function isEditorOrAbove(role: UserRole): boolean {
  return role === "editor" || role === "admin" || role === "owner";
}