-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat(api): Add the admin only endpoint to update the user role #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |||||||||||||||||||||||||||||||||||||||
| from app.crud import UserCRUD | ||||||||||||||||||||||||||||||||||||||||
| from app.models import User, UserRole | ||||||||||||||||||||||||||||||||||||||||
| from app.schemas.login import TokenPayload | ||||||||||||||||||||||||||||||||||||||||
| from app.schemas.users import Cred, CredHash, UserCreate | ||||||||||||||||||||||||||||||||||||||||
| from app.schemas.users import Cred, CredHash, RoleUpdate, UserCreate | ||||||||||||||||||||||||||||||||||||||||
| from app.services.telemetry import telemetry_client | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| router = APIRouter() | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -85,6 +85,33 @@ async def update_user_password( | |||||||||||||||||||||||||||||||||||||||
| return await users.update(user_id, CredHash(hashed_password=pwd)) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| @router.patch("/{user_id}/role", status_code=status.HTTP_200_OK, summary="Updates a user's role") | ||||||||||||||||||||||||||||||||||||||||
| async def update_user_role( | ||||||||||||||||||||||||||||||||||||||||
| payload: RoleUpdate, | ||||||||||||||||||||||||||||||||||||||||
| user_id: int = Path(..., gt=0), | ||||||||||||||||||||||||||||||||||||||||
| users: UserCRUD = Depends(get_user_crud), | ||||||||||||||||||||||||||||||||||||||||
| token_payload: TokenPayload = Security(get_jwt, scopes=[UserRole.ADMIN]), | ||||||||||||||||||||||||||||||||||||||||
| ) -> User: | ||||||||||||||||||||||||||||||||||||||||
| """Promote or demote a user between the `agent` role and the `user` role. | ||||||||||||||||||||||||||||||||||||||||
| Admins are out of scope: neither the requester nor the target user can have their admin role changed here. | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Beware that the role is baked into the access tokens that were already issued, and those are long-lived | ||||||||||||||||||||||||||||||||||||||||
| (see `JWT_EXPIRE_MINUTES`). The new role only applies to tokens minted afterwards, so the user has to log in | ||||||||||||||||||||||||||||||||||||||||
| again for the change to take effect. | ||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||
| telemetry_client.capture( | ||||||||||||||||||||||||||||||||||||||||
| token_payload.sub, event="user-role", properties={"user_id": user_id, "role": payload.role} | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| if user_id == token_payload.sub: | ||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status.HTTP_403_FORBIDDEN, "Admins cannot change their own role : it can lead to deadlock") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| user = cast(User, await users.get(user_id, strict=True)) | ||||||||||||||||||||||||||||||||||||||||
| if user.role == UserRole.ADMIN: | ||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status.HTTP_403_FORBIDDEN, "Cannot change an admin's role") | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+102
to
+110
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A 403/404 currently emits a
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return await users.update(user_id, payload) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| @router.delete("/{user_id}", status_code=status.HTTP_200_OK, summary="Delete a user") | ||||||||||||||||||||||||||||||||||||||||
| async def delete_user( | ||||||||||||||||||||||||||||||||||||||||
| user_id: int = Path(..., gt=0), | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,12 +9,12 @@ | |
|
|
||
| from app.crud.base import BaseCRUD | ||
| from app.models import User | ||
| from app.schemas.users import CredHash | ||
| from app.schemas.users import CredHash, RoleUpdate | ||
|
|
||
| __all__ = ["UserCRUD"] | ||
|
|
||
|
|
||
| class UserCRUD(BaseCRUD[User, User, CredHash]): | ||
| class UserCRUD(BaseCRUD[User, User, Union[CredHash, RoleUpdate]]): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. juste voc |
||
| def __init__(self, session: AsyncSession) -> None: | ||
| super().__init__(session, User) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User tokens use
JWT_UNLIMITED(365 d), notJWT_EXPIRE_MINUTES(60, never used for user tokens).As written, someone would lower
JWT_EXPIRE_MINUTESexpecting a shorter window and nothing wouldhappen. (Same slip in the PR description — the "one year" figure is right.)