HKUDS/DeepTutor · error · HTTPException

You cannot change your own role

Error message

You cannot change your own role

What it means

HTTP 400 raised by the PUT /users/{username}/role (update_user_role) handler in deeptutor/api/routers/auth.py:859 when an admin attempts to change their own role. Prevents an admin from demoting themselves (potentially leaving the system with no admin); role changes are only permitted for other users.

Solutions

  1. Have a different admin perform the role change
  2. Disable the role editor for the current user's row in the admin UI
  3. No retry will succeed from the same account
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at deeptutor/api/routers/auth.py:859 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HKUDS/DeepTutor@3e82f13042 (2026-08-27). Data as JSON: /api/errors/8683235b7b55487f. Report an issue: GitHub.

Appendix: source

Thrown at deeptutor/api/routers/auth.py:859

    user_id = str(info.get("id") or "") if info else ""
    if user_id and _USER_ID_RE.match(user_id):
        from deeptutor.multi_user.identity import delete_avatar_file

        delete_avatar_file(user_id)

    logger.info(f"Admin '{current.username if current else 'local'}' deleted user '{username}'")
    return {"ok": True}


@router.put("/users/{username}/role", status_code=status.HTTP_200_OK)
async def update_user_role(
    username: str,
    body: SetRoleRequest,
    current: TokenPayload = Depends(require_admin),
) -> dict:
    """Change a user's role. Admins cannot change their own role."""
    if current and username == current.username:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="You cannot change your own role",
        )

    updated = set_role(username, body.role)
    if not updated:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")

    logger.info(
        f"Admin '{current.username if current else 'local'}' set '{username}' role to {body.role!r}"
    )
    return {"ok": True, "username": username, "role": body.role}

View on GitHub (pinned to 3e82f13042)