How rs-labhub/document_management_system works
Project Type and Purpose
• This is a full-stack Next.js (App Router) document management system (DMS) with fine-grained authorization powered by Permit.io.
• Users can sign in, browse/create/edit/delete documents based on roles (Admin, Editor, Viewer) and ownership.
High-Level Architecture Components
a. Frontend (Browser)
• Next.js React pages & components under /app and /components.
• shadcn/ui + Tailwind CSS for responsive UI.
• Client-side hooks (hooks/use-toast.ts, hooks/use-mobile.tsx) and context (lib/auth-context.tsx) drive local state, theming, and UI gating (hasPermission).
b. Next.js Server
• App Router (server and client components) in app/*.
• Server Actions (app/actions.ts) handle form submissions and calls to business logic (create/update/delete).
• Middleware (middleware.ts) for authentication, session handling, and request routing.
• Authorization layer (lib/permit.ts) wrapping Permit.io SDK to check permissions before database operations.
c. Database (Persistence)
• A document store (e.g., PostgreSQL, MongoDB, etc.) accessed from server actions (not shown in file tree but implied).
d. External Service – Permit.io
• Policy Decision Point (PDP) URL and SDK token configured via environment variables.
• Centralized policy management (RBAC + ABAC).
e. Static Assets & CDN
• /public folder hosts logo, demo video thumbnail, and other static files.
• Served directly by Next.js or via CDN in production.
Component Interactions and Data Flows
1. User opens browser → Next.js Server serves React page with initial data.
2. User performs an action (e.g., “Edit Document”):
• Client calls a server action or API route.
• Server action invokes checkPermission(userId, action, resourceType, attributes) → calls Permit.io PDP.
• PDP returns allow/deny → server either proceeds or rejects with error.
• If allowed, server interacts with the database to CRUD the document.
• Response flows back to client; UI updates.
3. Client-side gating: UI components call hasPermission() hook (using lib/auth-context.tsx + client SDK) to show/hide buttons or links.
Architectural Patterns and Design Principles
• Separation of Concerns: UI layer (components/ui/*), auth logic (lib/permit.ts), state/context (lib/auth-context.tsx), and server actions (app/actions.ts).
• Externalized Authorization: All permission checks are delegated to Permit.io, keeping policy definitions out of app code.
• Server Components vs Client Components: Next.js App Router structure, with server-rendered pages for secure data fetching, and client components/hooks for interactivity.