Skip to content

Commit

Permalink
permissions: add member policy generator
Browse files Browse the repository at this point in the history
  • Loading branch information
slint committed Jul 30, 2024
1 parent 1498d94 commit a347562
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
32 changes: 26 additions & 6 deletions invenio_communities/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,36 @@ def __init__(self, then_, else_):
"""Initialize."""
field = "record_policy"
super().__init__(
lambda r: (
field_getter=lambda r: (
getattr(r.access, field, None)
if hasattr(r, "access")
else r.get("access", {}).get(field)
), # needed for running permission check at serialization time and avoid db query
f"access.{field}",
"closed",
"open",
then_,
else_,
field_name=f"access.{field}",
then_value="closed",
else_value="open",
then_=then_,
else_=else_,
)


class IfMemberPolicyClosed(IfRestrictedBase):
"""If member policy is closed."""

def __init__(self, then_, else_):
"""Initialize."""
field = "member_policy"
super().__init__(
field_getter=lambda r: (
getattr(r.access, field, None)
if hasattr(r, "access")
else r.get("access", {}).get(field)
), # needed for running permission check at serialization time and avoid db query
field_name=f"access.{field}",
then_value="closed",
else_value="open",
then_=then_,
else_=else_,
)


Expand Down
4 changes: 2 additions & 2 deletions invenio_communities/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
CommunityOwners,
CommunitySelfMember,
IfCommunityDeleted,
IfMemberPolicyClosed,
IfRecordPolicyClosed,
IfRestricted,
ReviewPolicy,
Expand Down Expand Up @@ -190,8 +191,7 @@ class CommunityPermissionPolicy(BasePermissionPolicy):
IfConfig(
"COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS",
then_=[
IfPolicyClosed(
"member_policy",
IfMemberPolicyClosed(
then_=[Disable()],
else_=[AuthenticatedButNotCommunityMembers()],
),
Expand Down

0 comments on commit a347562

Please sign in to comment.