Skip to content

Commit

Permalink
fixed compiler errors group_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Mar 28, 2024
1 parent 31cc758 commit fd44c19
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions xmtp_mls/src/groups/group_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,25 @@ pub struct PolicySet {
pub remove_member_policy: MembershipPolicies,
pub add_installation_policy: MembershipPolicies,
pub remove_installation_policy: MembershipPolicies,
pub update_group_name_policy: MembershipPolicies,
pub update_allow_list_policy: MembershipPolicies,
}

#[allow(dead_code)]
impl PolicySet {
pub fn new(
add_member_policy: MembershipPolicies,
remove_member_policy: MembershipPolicies,
update_group_name_policy: MembershipPolicies,
update_allow_list_policy: MembershipPolicies,
) -> Self {
Self {
add_member_policy,
remove_member_policy,
add_installation_policy: default_add_installation_policy(),
remove_installation_policy: default_remove_installation_policy(),
update_group_name_policy,
update_allow_list_policy,
}
}

Expand Down Expand Up @@ -295,6 +301,8 @@ impl PolicySet {
Ok(PolicySetProto {
add_member_policy: Some(self.add_member_policy.to_proto()?),
remove_member_policy: Some(self.remove_member_policy.to_proto()?),
update_group_name_policy: Some(self.update_group_name_policy.to_proto()?),
update_allow_list_policy: Some(self.update_allow_list_policy.to_proto()?),
})
}

Expand All @@ -308,6 +316,16 @@ impl PolicySet {
.remove_member_policy
.ok_or(PolicyError::InvalidPolicy)?,
)?,
MembershipPolicies::try_from(
proto
.update_group_name_policy
.ok_or(PolicyError::InvalidPolicy)?,
)?,
MembershipPolicies::try_from(
proto
.update_allow_list_policy
.ok_or(PolicyError::InvalidPolicy)?,
)?,
))
}

Expand All @@ -332,16 +350,22 @@ fn default_remove_installation_policy() -> MembershipPolicies {
MembershipPolicies::deny()
}

/// A policy where any member can add or remove any other member
/// A policy where any member can add or remove any other member or set group name
pub(crate) fn policy_everyone_is_admin() -> PolicySet {
PolicySet::new(MembershipPolicies::allow(), MembershipPolicies::allow())
PolicySet::new(
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::deny())
}

/// A policy where only the group creator can add or remove members
pub(crate) fn policy_group_creator_is_admin() -> PolicySet {
PolicySet::new(
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::deny(),
)
}

Expand Down Expand Up @@ -446,15 +470,25 @@ mod tests {

#[test]
fn test_allow_all() {
let permissions = PolicySet::new(MembershipPolicies::allow(), MembershipPolicies::allow());
let permissions = PolicySet::new(
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
);

let commit = build_validated_commit(Some(true), Some(true), None, None, false);
assert!(permissions.evaluate_commit(&commit));
}

#[test]
fn test_deny() {
let permissions = PolicySet::new(MembershipPolicies::deny(), MembershipPolicies::deny());
let permissions = PolicySet::new(
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
);

let member_added_commit = build_validated_commit(Some(false), None, None, None, false);
assert!(!permissions.evaluate_commit(&member_added_commit));
Expand All @@ -478,6 +512,8 @@ mod tests {
let permissions = PolicySet::new(
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::allow_if_actor_creator(),
);

let commit_with_creator = build_validated_commit(Some(true), Some(true), None, None, true);
Expand All @@ -493,6 +529,8 @@ mod tests {
let permissions = PolicySet::new(
MembershipPolicies::allow_same_member(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
);

let commit_with_same_member = build_validated_commit(Some(true), None, None, None, false);
Expand All @@ -511,6 +549,8 @@ mod tests {
MembershipPolicies::Standard(BasePolicies::Allow),
]),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
);

let member_added_commit = build_validated_commit(Some(true), None, None, None, false);
Expand All @@ -525,6 +565,8 @@ mod tests {
MembershipPolicies::allow(),
]),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
);

let member_added_commit = build_validated_commit(Some(true), None, None, None, false);
Expand All @@ -542,6 +584,14 @@ mod tests {
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::deny(),
]),
MembershipPolicies::and(vec![
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::deny(),
]),
MembershipPolicies::and(vec![
MembershipPolicies::allow_if_actor_creator(),
MembershipPolicies::deny(),
]),
);

let proto = permissions.to_proto().unwrap();
Expand All @@ -559,6 +609,8 @@ mod tests {
let permissions = PolicySet::new(
MembershipPolicies::allow_same_member(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
);

let proto_result = permissions.to_proto();
Expand Down

0 comments on commit fd44c19

Please sign in to comment.