feat: add role-based admin support for multiple admin users

- Add Role model and user_roles association table (many-to-many)
- Add password_hash column to User model (nullable, for future bcrypt)
- Keep is_admin boolean for backward compatibility
- Add is_admin_effective property: true if legacy is_admin OR 'admin' role
- Update auth dependency (get_current_admin_user) to use is_admin_effective
- Update bootstrap login to auto-create 'admin' role and assign on login
- Update Google OAuth login to use is_admin_effective for token creation
- Add migrate_roles.py: idempotent migration script to backfill roles
- Add unit tests for Role, User, association table, and is_admin_effective
This commit is contained in:
Hermes Agent
2026-07-30 04:54:30 +00:00
parent a36fd92ad4
commit 0eca079f28
5 changed files with 220 additions and 8 deletions
+5 -2
View File
@@ -98,8 +98,11 @@ async def get_current_user(
async def get_current_admin_user(
current_user: User = Depends(get_current_user),
) -> User:
"""Raise 403 if the current user is not an admin."""
if not current_user.is_admin:
"""Raise 403 if the current user is not an admin.
Checks both the legacy is_admin flag and the new role-based admin role.
"""
if not current_user.is_admin_effective:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin access required")
return current_user