How zy7y/mini-rbac works
1. Overall shape
This repository is a two-part RBAC admin system: a FastAPI backend under backend/ and a Vue 3 frontend under frontend/. The backend exposes authentication and CRUD APIs; the frontend renders the admin UI and enforces client-side route/menu/button visibility based on the logged-in user’s role and permissions.
2. Backend runtime and persistence
The backend is a single Python API service built with FastAPI and run via Uvicorn. It uses Tortoise ORM for data access and stores data in a local SQLite database (backend/mini.db), which makes the project self-contained for local/demo use. docker-compose.yml and backend/Dockerfile indicate it can also be containerized as part of a simple multi-service setup.
3. Backend layering
The backend is organized into clear layers:
- backend/router/ for HTTP endpoints
- backend/service/ for business logic
- backend/models/ for database entities
- backend/schemas/ for request/response validation
- backend/core/ for cross-cutting infrastructure
This is a classic controller/service/model split, with routers delegating almost everything to service classes rather than embedding logic in endpoints.
4. Backend cross-cutting infrastructure
The backend/core/ package holds the shared plumbing that shapes the whole API:
- security.py for password hashing, token/JWT handling, and permission checks
- middleware.py for request-level behavior such as auth or logging interception
- exceptions.py for standardized API errors
- log.py for logging setup
- dbhelper.py, service.py, and utils.py for shared helpers and base abstractions
- events.py for lifecycle hooks such as startup initialization
5. Backend domain model
The core data model is small and purpose-built for RBAC:
- user
- role
- menu
- relation (to connect roles and menus, and likely users/roles depending on implementation)
- common for shared fields/base schema patterns
This mirrors the product scope: users log in, are assigned roles, and roles determine accessible menus and actions.
6. Backend API surface
The routers are split by domain:
- auth.py handles login/logout/current-user/session actions
- user.py handles user management
- role.py handles role management
- menu.py handles menu management
These endpoints likely back the frontend’s admin screens and also enforce server-side authorization, which the README explicitly calls out as one of the main features.
7. Authorization flow
The architecture is centered on RBAC at multiple layers:
- Authentication: login returns a token/session identity
- Role resolution: the backend determines the user’s active role
- Permission projection: menu trees and button permissions are derived from the role
- API enforcement: backend routes are protected so unauthorized requests are rejected even if the UI is bypassed
The README’s “switch role” feature suggests the active role can be changed without changing the account, which then updates the visible UI and permitted APIs.
8. Frontend runtime and state
The frontend is a Vue 3 + Vite application using Ant Design Vue for UI, Pinia for state, and pinia-plugin-persistedstate for keeping auth/user state across refreshes. vue-router handles navigation, and axios-based request utilities wrap backend API calls.
9. Frontend structure
The frontend is organized by responsibility:
- src/views/ contains page-level screens
- src/components/ contains reusable layout/table/chart/modal/markdown pieces
- src/service/ contains API clients for user/role/menu
- src/stores/ contains persisted application state
- src/utils/ contains shared helpers, request setup, directives, and dynamic component loading
- src/router/ defines route structure and guards
This separation suggests the app is built around a dynamic admin shell rather than static pages.
10. Frontend permission-driven UI
The UI is not just a consumer of data; it is part of the permission system. The presence of:
- sider-menu.vue
- select-role.vue
- route guards
- directive helpers
- menu/role/user stores
indicates the frontend constructs its navigation and button availability from backend-provided role/menu data. That supports the repo’s stated goals of menu, route, and button permission control.
11. Main user-facing modules
The primary screens are:
- login/ for authentication
- main/main.vue as the authenticated shell
- dashboard/ for overview content and charts
- system/user, system/role, system/menu for the core CRUD admin functions
- error/404.vue and related back/navigation screens for routing fallback
- test/debug/ for internal verification or demo tooling
The layout components in src/components/layout/ provide the persistent header, breadcrumb, and side navigation around these pages.
12. Deployment and packaging
The repo is designed for both local and containerized execution:
- separate Dockerfiles for backend and frontend
- frontend/nginx.conf for serving the built SPA
- .env.development / .env.production for environment-specific API targets
- docker-compose.yml for end-to-end startup
This means the architecture is split at runtime into a browser client and a backend API service, with Nginx likely serving the compiled frontend in production.
13. Operational and quality boundaries
The backend includes tests/ and a pytest setup, which suggests API/service validation is part of the repo’s structure even if the suite is small. Shared exception/logging/middleware modules indicate that request handling, error translation, and observability are treated as platform concerns rather than being duplicated in each endpoint.
14. How the main data flow works
In practice, the flow is:
- user opens the frontend and logs in
- frontend calls the auth API
- backend validates credentials and returns identity/permission data
- frontend stores auth state and builds routes/menu visibility
- CRUD screens call user/role/menu APIs through the service layer
- backend persists changes through ORM models and enforces access rules on every request
That is the key architectural loop to capture in a diagram: browser UI ↔ FastAPI API ↔ ORM/SQLite, with RBAC logic crossing both layers.
Main entry points: main.py API entry · router Routers · service Services · core Core
Components 20
Backend
FastAPI RBAC API, domain services, and persistence
- API entry · Application bootstrap and server wiring
backend/main.py - Routers · Auth and CRUD route handlers
backend/router - Services · Domain rules and orchestration
backend/service - Models · RBAC database entities and relations
backend/models - Schemas · Request and response contracts
backend/schemas - Core · Security, middleware, logging, events, and helpers
backend/core - Security · Password hashing, tokens, and permission checks
backend/core/security.py - Events · Startup and initialization hooks
backend/core/events.py - SQLite DB · Local mini database backing the admin system
backend/mini.db
Frontend
Vue 3 admin UI, routing, state, and permission-driven screens
- Vue app · Client entry and root application
frontend/src/main.js - Router · Route structure and guards
frontend/src/router/index.js - Stores · Persisted auth, role, and menu state
frontend/src/stores - API client · Backend request wrapper and service calls
frontend/src/utils/request.js - Services · User, role, and menu API clients
frontend/src/service - Admin shell · Header, sider menu, role switcher, and breadcrumb
frontend/src/components/layout - Views · Login, dashboard, system CRUD, and error pages
frontend/src/views - Permission UI · Client-side visibility and dynamic screen composition
frontend/src/utils/directive.js
Deployment
Container and environment wiring for local or bundled startup
- Compose · End-to-end local startup wiring
docker-compose.yml - Backend Docker · Container build for the API service
backend/Dockerfile - Frontend Docker · Container build and Nginx packaging for the SPA
frontend/Dockerfile
Connections 23
- API entry → Routers: registers · Bootstraps the HTTP API surface
- API entry → Core: uses · Applies shared middleware, logging, and startup hooks
- API entry → SQLite DB: connects · Initializes persistence on startup
- Routers → Services: delegates · Endpoints forward work to domain services
- Services → Models: persists via · Reads and writes RBAC entities
- Services → Schemas: validates · Uses request and response contracts
- Core → Security: contains · Security is the main cross-cutting concern
- Core → Events: contains · Lifecycle hooks live in core infrastructure
- Security → Routers: protects · Route authorization depends on shared security checks
- Vue app → Router: mounts · Client app uses route-driven navigation
- Vue app → Stores: hydrates · Application state is persisted across refreshes
- Router → Views: renders · Routes map to page-level screens
- Router → Permission UI: guards · Route access is filtered by permissions
- Views → Admin shell: composes · Authenticated screens use the admin shell
- Admin shell → Permission UI: consumes · Menu, role switcher, and button visibility depend on permissions
- Views → Services: calls · Screens load and mutate domain data through API modules
- Services → API client: wraps · Domain clients use the shared HTTP layer
- API client → Routers: requests · Browser client talks to FastAPI endpoints
- Stores → Permission UI: drives · Stored role and menu state shape visible UI
- Compose → Backend Docker: builds · Compose includes the API container
- Compose → Frontend Docker: builds · Compose includes the frontend container
- Frontend Docker → Vue app: serves · Nginx packages the compiled SPA
- Backend Docker → API entry: runs · Container starts the FastAPI service