-
-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent users from updating their own household privileges (#4928)
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
- Loading branch information
1 parent
8cd2da0
commit bf616f9
Showing
7 changed files
with
133 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,49 @@ | ||
from fastapi import HTTPException, status | ||
from pydantic import UUID4 | ||
|
||
from mealie.schema.user.user import PrivateUser | ||
from mealie.schema.response.responses import ErrorResponse | ||
from mealie.schema.user.user import PrivateUser, UserBase | ||
|
||
permission_attrs = ["can_invite", "can_manage", "can_manage_household", "can_organize", "admin"] | ||
|
||
def assert_user_change_allowed(id: UUID4, current_user: PrivateUser): | ||
if current_user.id != id and not current_user.admin: | ||
# only admins can edit other users | ||
raise HTTPException(status.HTTP_403_FORBIDDEN, detail="NOT_AN_ADMIN") | ||
|
||
def _assert_non_admin_user_change_allowed(user_id: UUID4, current_user: PrivateUser, new_data: UserBase): | ||
if current_user.id != user_id: | ||
# User is trying to edit another user | ||
raise HTTPException(status.HTTP_403_FORBIDDEN, ErrorResponse.respond("User cannot edit other users")) | ||
|
||
if any(getattr(current_user, p) != getattr(new_data, p) for p in permission_attrs): | ||
# User is trying to change their own permissions | ||
raise HTTPException( | ||
status.HTTP_403_FORBIDDEN, | ||
ErrorResponse.respond("User cannot change their own permissions"), | ||
) | ||
|
||
if current_user.group != new_data.group: | ||
# prevent a regular user from changing their group | ||
raise HTTPException( | ||
status.HTTP_403_FORBIDDEN, ErrorResponse.respond("User doesn't have permission to change their group") | ||
) | ||
|
||
if current_user.household != new_data.household: | ||
# prevent a regular user from changing their household | ||
raise HTTPException( | ||
status.HTTP_403_FORBIDDEN, | ||
ErrorResponse.respond("User doesn't have permission to change their household"), | ||
) | ||
|
||
|
||
def assert_user_change_allowed(user_id: UUID4, current_user: PrivateUser, new_data: UserBase): | ||
if not current_user.admin: | ||
_assert_non_admin_user_change_allowed(user_id, current_user, new_data) | ||
return | ||
|
||
if current_user.id != user_id: | ||
raise HTTPException(status.HTTP_403_FORBIDDEN, ErrorResponse.respond("Use the Admin API to update other users")) | ||
|
||
# Admin is trying to edit themselves | ||
if any(getattr(current_user, p) != getattr(new_data, p) for p in permission_attrs): | ||
# prevent an admin from excalating their own permissions | ||
raise HTTPException( | ||
status.HTTP_403_FORBIDDEN, ErrorResponse.respond("Admins can't change their own permissions") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters