Users Module Specification
1. Purpose & Overview
The module provides custom user identity management, password hashing, 4-tier Role-Based Access Control (RBAC), and authentication backend management for MetaPilot.
users2. Responsibilities
- Manage custom database entity extending Django's
UserandAbstractBaseUser.PermissionsMixin - Maintain enumeration (
UserRole,SUPER_ADMIN,AGENCY_ADMIN,TENANT_ADMIN).TENANT_USER - Manage superuser creation via and CLI scripts.
UserManager.create_superuser
3. Directory Structure
text
services/api/users/
├── admin.py
├── apps.py
├── management/
│ └── commands/
│ └── create_superadmin.py
├── migrations/
├── models.py
├── serializers.py
└── views.py
4. Models & Database Schemas
python
class UserRole(models.TextChoices):
SUPER_ADMIN = 'SUPER_ADMIN', 'Super Admin'
AGENCY_ADMIN = 'AGENCY_ADMIN', 'Agency Admin'
TENANT_ADMIN = 'TENANT_ADMIN', 'Tenant Admin'
TENANT_USER = 'TENANT_USER', 'Tenant User'
class User(AbstractBaseUser, PermissionsMixin):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
email = models.EmailField(unique=True, db_index=True)
role = models.CharField(max_length=20, choices=UserRole.choices, default=UserRole.TENANT_USER)
tenant = models.ForeignKey('tenants.Tenant', on_delete=models.SET_NULL, null=True, blank=True)
agency = models.ForeignKey('tenants.Agency', on_delete=models.SET_NULL, null=True, blank=True)
first_name = models.CharField(max_length=150, blank=True)
last_name = models.CharField(max_length=150, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
5. Services & Business Logic
- : Normalizes email and hashes password with PBKDF2.
UserManager.create_user() - : Sets
UserManager.create_superuser(),is_staff=True, andis_superuser=True.role=UserRole.SUPER_ADMIN
6. Serializers & Data Transfer Objects
- : Handles user serialization and validation for User management endpoints.
UserSerializer - : Validates email uniqueness, password requirements (min 8 chars), and role assignment.
UserCreateSerializer
7. Views & API Endpoints
- : List users scoped by tenant or agency.
GET /api/users/ - : Create user.
POST /api/users/ - : Retrieve user profile.
GET /api/users/{id}/ - : Delete user access.
DELETE /api/users/{id}/
8. Permissions & Role Rules
- : Can manage users across all agencies and tenants.
SUPER_ADMIN - : Can manage users within clients under their agency.
AGENCY_ADMIN - : Can manage users within their own tenant organization.
TENANT_ADMIN
9. Signals & Event Listeners
- : Automatically triggers audit log entries upon user role modification.
post_save
10. Background Tasks & Celery Jobs
- None (User authentication operates synchronously).
11. Data Flow & External Dependencies
- Integrates with for issuing signed JWT access and refresh tokens.
rest_framework_simplejwt
12. Business Rules & Validations
- Email addresses must be unique across the platform.
- Password length must be at least 8 characters.
13. Sequence Diagram (Mermaid)
Rendering diagram...
14. Known Limitations & Technical Debt
- Single primary tenant relationship per user (users cannot switch active tenants without re-authentication).
15. Future Improvements
- Add Multi-Factor Authentication (MFA / 2FA) support using TOTP applications.