-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow members to remove other members from groups
Extend the remove-member-from-group API (`DELETE /groups/{id}/members/{user}`) to allow group members to remove _other_ members (not just themselves) from groups, assuming the authenticated member has the necessary role in the group. Currently the only valid call to this API is `DELETE /groups/{id}/members/me` (i.e. `{user}` is the literal string `me`) to remove yourself from a group. While keeping the `me` alias this commit also enables `{user}` to be either your own or someone else's userid, and will allow or deny the request according to these rules: * Any group member can remove themselves from a group with either `me` or their own userid * Only owners can remove other owners and admins * Only owners and admins can remove moderators * Owners, admins and moderators can remove plain members * Plain members, people who aren't members of the group at all, and unauthenticated requests can't remove anyone * Also, it'll 404 if either the group or the target user doesn't exist, or if the target user isn't a member of the group * There's also a separate code path for when the userid is invalid (can't be parsed in `acct:{username}@{authority}` format), 404s
- Loading branch information
Showing
14 changed files
with
565 additions
and
101 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from dataclasses import dataclass | ||
|
||
from pyramid.httpexceptions import HTTPNotFound | ||
|
||
from h.exceptions import InvalidUserId | ||
from h.models import Group, GroupMembership, User | ||
|
||
|
||
@dataclass | ||
class GroupMembershipContext: | ||
group: Group | ||
user: User | ||
membership: GroupMembership | None | ||
|
||
|
||
def group_membership_api_factory(request) -> GroupMembershipContext: | ||
user_service = request.find_service(name="user") | ||
group_service = request.find_service(name="group") | ||
group_members_service = request.find_service(name="group_members") | ||
|
||
userid = request.matchdict["userid"] | ||
pubid = request.matchdict["pubid"] | ||
|
||
def get_user() -> User | None: | ||
if userid == "me": | ||
if request.authenticated_userid: | ||
return user_service.fetch(request.authenticated_userid) | ||
|
||
return None | ||
|
||
try: | ||
return user_service.fetch(userid) | ||
except InvalidUserId: | ||
return None | ||
|
||
user = get_user() | ||
|
||
if not user: | ||
raise HTTPNotFound(f"User not found: {userid}") | ||
|
||
group = group_service.fetch(pubid) | ||
|
||
if not group: | ||
raise HTTPNotFound(f"Group not found: {pubid}") | ||
|
||
membership = group_members_service.get(group, user) | ||
|
||
if not membership and request.method != "POST": | ||
raise HTTPNotFound(f"Membership not found: ({pubid}, {userid})") | ||
|
||
return GroupMembershipContext(group=group, user=user, membership=membership) |
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
Oops, something went wrong.