Add admin role with user management (create/delete users)
First registered user becomes admin automatically. Admins see a "Manage Users" button in the dashboard header that opens a new /admin page for listing, creating, and deleting users. Backend enforces admin-only access on /admin/* routes. Startup migration adds the is_admin column to existing SQLite databases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ class UserCreate(BaseModel):
|
||||
class Token(BaseModel):
|
||||
access_token: str
|
||||
token_type: str
|
||||
is_admin: bool
|
||||
|
||||
|
||||
@router.post("/register", status_code=status.HTTP_201_CREATED)
|
||||
@@ -24,9 +25,11 @@ def register(user_in: UserCreate, db: Session = Depends(get_db)):
|
||||
existing = db.query(models.User).filter(models.User.username == user_in.username).first()
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Username already taken")
|
||||
no_users_yet = db.query(models.User).first() is None
|
||||
user = models.User(
|
||||
username=user_in.username,
|
||||
password=hash_password(user_in.password),
|
||||
is_admin=no_users_yet,
|
||||
)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
@@ -39,4 +42,4 @@ def login(user_in: UserCreate, db: Session = Depends(get_db)):
|
||||
if not user or not verify_password(user_in.password, user.password):
|
||||
raise HTTPException(status_code=401, detail="Invalid credentials")
|
||||
token = create_access_token({"sub": user.username})
|
||||
return {"access_token": token, "token_type": "bearer"}
|
||||
return {"access_token": token, "token_type": "bearer", "is_admin": user.is_admin}
|
||||
|
||||
Reference in New Issue
Block a user