User Roles
ClassQuiz has three permission levels:- Regular Users: Can create quizzes, play games, manage their own content
- Moderators: Can review and rate public quizzes
- Admins: Full system access, including user deletion
User Model
Users are defined inclassquiz/db/models.py:37-65 with these key properties:
Authentication Methods
Local Authentication
Users create accounts with email and password:- Passwords are hashed before storage
- Email verification required (
verified: bool) - Verification key sent via email (
verify_key)
OAuth Providers
Supported OAuth providers:- Google
- GitHub
- Custom
Users authenticate via Google OAuth:
auth_type: GOOGLEgoogle_uid: Unique Google identifier- Password not required
Two-Factor Authentication
ClassQuiz supports multiple 2FA methods: TOTP (Time-based One-Time Password):totp_secret: 32-character secret for authenticator apps- Used with Google Authenticator, Authy, etc.
- Hardware security keys (YubiKey, etc.)
- Stored in
FidoCredentialstable - Supports multiple keys per user
- 64-character hex string (
backup_code) - Used when primary 2FA method unavailable
- Generated automatically on account creation
User Sessions
Sessions are tracked in theUserSession model (classquiz/db/models.py:92-109):
Session Management
- Sessions expire after inactivity
- Users can view active sessions
- Admins can revoke sessions
last_seenupdated on each request
API Keys
Users can generate API keys for programmatic access:- Provide full account access
- Don’t expire automatically
- Used with
Authorization: Bearer <key>header - Should be treated as securely as passwords
Admin Operations
Administrators can delete users through the API defined inclassquiz/routers/admin.py.
Delete User by ID
Endpoint:DELETE /api/v1/admin/user/id
Parameters: user_id (UUID)
Authentication: Admin user required
Delete User by Username
Endpoint:DELETE /api/v1/admin/user/username
Parameters: username (string)
Delete User by Email
Endpoint:DELETE /api/v1/admin/user/email
Parameters: email (string)
Cascade Deletion
When a user is deleted, the following related data is automatically removed:- Quizzes: All quizzes created by the user
- Game Results: Historical game session data
- Sessions: Active and expired user sessions
- API Keys: All API keys for the user
- FIDO Credentials: Registered security keys
- QuizTivities: Interactive content created by user
- Storage Items: Uploaded images and files (set to NULL)
- Ratings: Quiz ratings from the user
Storage items are not deleted but have their
user reference set to NULL. This preserves images that may be used in other quizzes.Storage Quotas
Each user has a storage quota for uploaded images:Storage Calculation
- Tracked in
user.storage_used(BigInteger, bytes) - Updated when uploading images
- Includes quiz images, cover images, and question images
- Checked before imports and uploads
classquiz/routers/eximport.py:86-88:
Moderator Tools
Moderators have access to special endpoints for content review. See the Moderation Guide for details.Authentication Guards
ClassQuiz uses FastAPI dependency injection for authentication:Regular User Authentication
Moderator Authentication
Admin Authentication
Best Practices
Account Security
Enforce email verification
Always verify user emails before granting full access. Check
user.verified before allowing quiz creation or game hosting.Encourage 2FA
Prompt users to enable two-factor authentication, especially for accounts with public quizzes.
User Management
- Soft deletes: Consider marking users as inactive instead of hard deletion
- Audit logs: Track admin actions for compliance
- Data export: Provide GDPR-compliant data export
- Storage alerts: Notify users approaching storage limits
User Avatar Management
User avatars are stored as base64-encoded bytes:- Maximum size: 25KB
- Automatically encoded/decoded as base64
- Displayed in game lobbies and quiz listings
Password Management
Password updates require current password verification:auth_type != LOCAL) may not have passwords:
passwordfield is nullablerequire_password: falsefor OAuth-only accounts- Cannot change password if not set
User Queries
Common user management queries:Find users by authentication type
Find unverified users
Check storage usage
Find users with 2FA enabled
Integration Points
Users interact with these system components:- Quizzes:
Quiz.user_idforeign key - Game Results:
GameResults.userforeign key - Storage:
StorageItem.userforeign key - Sessions:
UserSession.userforeign key - Ratings:
Rating.userforeign key - Controllers:
Controller.userforeign key (for game controllers)
ReferentialAction.CASCADE for automatic cleanup on user deletion.