UNPKG

@compugit/react-rbac

Version:

A comprehensive Role-Based Access Control (RBAC) library for React applications with support for groups, roles, permissions, and protected components

171 lines (160 loc) 5.45 kB
import React from 'react'; interface User { id: string; email: string; name: string; groups: Group[]; roles: Role[]; permissions: Permission[]; } interface Group { id: string; name: string; description?: string; roles: Role[]; permissions: Permission[]; } interface Role { id: string; name: string; description?: string; permissions: Permission[]; } interface Permission { id: string; name: string; resource: string; action: string; description?: string; } interface RBACConfig { user: User | null; loading: boolean; error: string | null; } interface AuthorizationOptions { requireAll?: boolean; strict?: boolean; } type AuthorizationMode = "any" | "all"; interface ProtectedElementProps { children: React.ReactNode; fallback?: React.ReactNode; roles?: string[]; permissions?: string[]; groups?: string[]; mode?: AuthorizationMode; requireAll?: boolean; } interface RBACState extends RBACConfig { initialized: boolean; } interface RBACContextType extends RBACState { setUser: (user: User | null) => void; setLoading: (loading: boolean) => void; setError: (error: string | null) => void; updateUserPermissions: (permissions: Permission[]) => void; updateUserRoles: (roles: Role[]) => void; updateUserGroups: (groups: Group[]) => void; clearAuth: () => void; refreshUser: () => Promise<void>; } interface RBACProviderProps { children: React.ReactNode; onUserLoad?: () => Promise<User | null>; onRefreshUser?: () => Promise<User | null>; } declare const RBACProvider: React.FC<RBACProviderProps>; declare const useRBACContext: () => RBACContextType; declare const useRBAC: () => { user: User | null; loading: boolean; error: string | null; initialized: boolean; userPermissions: Permission[]; userRoles: Role[]; userGroups: Group[]; hasPermission: (permissionName: string | string[], mode?: AuthorizationMode) => boolean; hasRole: (roleName: string | string[], mode?: AuthorizationMode) => boolean; hasGroup: (groupName: string | string[], mode?: AuthorizationMode) => boolean; hasAccess: (options: { permissions?: string[]; roles?: string[]; groups?: string[]; mode?: AuthorizationMode; }) => boolean; canAccess: (resource: string, action: string) => boolean; isAuthenticated: () => boolean; isInRole: (roleName: string) => boolean; isInGroup: (groupName: string) => boolean; setUser: (user: User | null) => void; setLoading: (loading: boolean) => void; setError: (error: string | null) => void; updateUserPermissions: (permissions: Permission[]) => void; updateUserRoles: (roles: Role[]) => void; updateUserGroups: (groups: Group[]) => void; clearAuth: () => void; refreshUser: () => Promise<void>; }; declare const useAuth: () => { user: User | null; loading: boolean; error: string | null; initialized: boolean; isAuthenticated: () => boolean; login: (user: User | null) => void; logout: () => void; refresh: () => Promise<void>; }; interface ProtectedRouteProps { children: React.ReactNode; fallback?: React.ReactNode; loadingComponent?: React.ReactNode; unauthorizedComponent?: React.ReactNode; roles?: string[]; permissions?: string[]; groups?: string[]; mode?: AuthorizationMode; requireAuth?: boolean; } declare const ProtectedRoute: React.FC<ProtectedRouteProps>; declare const ProtectedElement: React.FC<ProtectedElementProps>; interface ConditionalRenderProps { children: React.ReactNode; show?: { roles?: string[]; permissions?: string[]; groups?: string[]; mode?: AuthorizationMode; }; hide?: { roles?: string[]; permissions?: string[]; groups?: string[]; mode?: AuthorizationMode; }; fallback?: React.ReactNode; } declare const ConditionalRender: React.FC<ConditionalRenderProps>; interface WithAuthorizationOptions { roles?: string[]; permissions?: string[]; groups?: string[]; mode?: AuthorizationMode; fallback?: React.ComponentType; loading?: React.ComponentType; } declare function withAuthorization<P extends object>(WrappedComponent: React.ComponentType<P>, options?: WithAuthorizationOptions): React.FC<P>; declare class RBACUtils { static getAllUserPermissions(user: User): Permission[]; static getAllUserRoles(user: User): Role[]; static hasPermission(user: User, permissionName: string): boolean; static hasRole(user: User, roleName: string): boolean; static hasGroup(user: User, groupName: string): boolean; static canAccessResource(user: User, resource: string, action: string): boolean; static createPermission(id: string, name: string, resource: string, action: string, description?: string): Permission; static createRole(id: string, name: string, permissions: Permission[], description?: string): Role; static createGroup(id: string, name: string, roles: Role[], permissions: Permission[], description?: string): Group; } export { ConditionalRender, ProtectedElement, ProtectedRoute, RBACProvider, RBACUtils, useAuth, useRBAC, useRBACContext, withAuthorization }; export type { AuthorizationMode, AuthorizationOptions, Group, Permission, ProtectedElementProps, RBACConfig, Role, User };