# Admin System Documentation ## Overview The NDF Studio admin system provides comprehensive user management capabilities with automatic first-user superuser promotion and a complete admin interface for user management. ## Features ### 1. Automatic First-User Superuser Promotion - **Mechanism**: The first user to register in the system is automatically promoted to superuser status - **Implementation**: Handled in `UserManager._make_first_user_superuser()` method - **Logging**: All superuser promotions are logged with SECURITY level events - **Safety**: Only applies to the very first user in the system ### 2. Admin User Management Interface #### Backend API Endpoints All admin endpoints require superuser privileges and are prefixed with `/auth/admin/`: - `GET /auth/admin/users` - List all users with statistics - `POST /auth/admin/users` - Create new user - `PUT /auth/admin/users/{user_id}` - Update user details - `POST /auth/admin/users/{user_id}/promote` - Promote user to superuser - `POST /auth/admin/users/{user_id}/demote` - Demote superuser to regular user - `DELETE /auth/admin/users/{user_id}` - Delete user - `GET /auth/admin/stats` - Get system statistics #### Frontend Admin Panel - **Location**: Accessible via the "Admin" tab in the Knowledge Base panel - **Access Control**: Only visible to superusers - **Features**: - User listing with status indicators - Create new users - Edit existing users - Promote/demote users - Delete users - System statistics dashboard ## Security Features ### 1. Access Control - All admin endpoints require superuser authentication - Non-superusers receive 403 Forbidden responses - Frontend hides admin panel from non-superusers ### 2. Self-Protection Mechanisms - **Self-Demotion Prevention**: Admins cannot demote themselves - **Self-Deletion Prevention**: Admins cannot delete themselves - **Superuser Deletion Prevention**: Superusers cannot be deleted (configurable) ### 3. Audit Logging All admin actions are logged with SECURITY level events: ```python logger.security( f"Admin '{current_user.username}' promoted user '{user.username}' to superuser", event_type="admin_user_promotion", admin_user_id=str(current_user.id), promoted_user_id=str(user.id), promoted_username=user.username, reason=request.reason ) ``` ## API Reference ### User Management Models ```python class UserPromoteRequest(BaseModel): user_id: str reason: Optional[str] = None class UserDemoteRequest(BaseModel): user_id: str reason: Optional[str] = None class AdminUserCreate(BaseModel): username: str email: str password: str is_superuser: bool = False is_active: bool = True class UserUpdateRequest(BaseModel): username: Optional[str] = None email: Optional[str] = None is_active: Optional[bool] = None is_superuser: Optional[bool] = None ``` ### Endpoint Details #### GET /auth/admin/users **Response:** ```json { "users": [ { "id": "uuid", "username": "string", "email": "string", "is_active": true, "is_superuser": false, "is_verified": true, "created_at": "timestamp" } ], "total": 5, "superusers": 1, "active_users": 4 } ``` #### POST /auth/admin/users **Request:** ```json { "username": "newuser", "email": "newuser@example.com", "password": "securepassword", "is_superuser": false, "is_active": true } ``` **Response:** ```json { "message": "User created successfully", "user": { "id": "uuid", "username": "newuser", "email": "newuser@example.com", "is_active": true, "is_superuser": false } } ``` #### PUT /auth/admin/users/{user_id} **Request:** ```json { "username": "updated_username", "email": "updated@example.com", "is_active": true, "is_superuser": false } ``` #### POST /auth/admin/users/{user_id}/promote **Request:** ```json { "reason": "User needs admin access for project management" } ``` #### POST /auth/admin/users/{user_id}/demote **Request:** ```json { "reason": "User no longer needs admin access" } ``` #### DELETE /auth/admin/users/{user_id} **Response:** ```json { "message": "User 'username' deleted successfully" } ``` #### GET /auth/admin/stats **Response:** ```json { "total_users": 10, "active_users": 8, "inactive_users": 2, "superusers": 2, "regular_users": 8, "system_info": { "first_user_created": true, "has_superusers": true } } ``` ## Frontend Integration ### Admin Panel Component The `AdminPanel` component provides a complete user management interface: - **User List**: Table view with user details and status indicators - **Create User**: Modal form for creating new users - **Edit User**: Modal form for updating user details - **Actions**: Promote, demote, and delete buttons for each user - **Statistics**: Dashboard showing system statistics ### Access Control ```jsx {activeTab === "admin" && userInfo?.is_superuser && (