~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Someone always runs a group, admitting takes admit, and every member holds member

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/membership-fixes
crates/group-host/src/group_join.rs+2−1
@@ -173,7 +173,8 @@ pub async fn admit_member(
if roles.is_empty() {
return Err(invalid("roles names the roles to give"));
}
let assignable = members.assignable(&mine, "role.assign");
// The roles an admitter may give are those their `admit` grants may.
let assignable = members.assignable(&mine, "admit");
let blocked: Vec<&String> = roles.iter().filter(|r| !assignable.contains(*r)).collect();
if !blocked.is_empty() {
return Err(bad(
crates/group-host/src/group_members.rs+20−8
@@ -107,16 +107,21 @@ impl<'a> Members<'a> {
})
}
/// Writes `did`'s membership, holding `roles`.
/// Writes `did`'s membership, holding `roles`, and `member` among them
/// whatever they name: everyone in the group holds it.
pub(crate) async fn put_membership(
&self,
did: &str,
roles: &[String],
) -> Result<(), XrpcError> {
let mut held = roles.to_vec();
if !held.iter().any(|r| r == "member") {
held.insert(0, "member".into());
}
let membership = json!({
"$type": format!("{NS}.membership"),
"member": did,
"roles": objects("role", roles),
"roles": objects("role", &held),
"createdAt": iso(now_millis()),
});
let (collection, key) = (format!("{NS}.membership"), did_rkey(did));
@@ -132,6 +137,13 @@ impl<'a> Members<'a> {
Ok(())
}
/// Whether any member holds one of `roles`.
fn held(&self, roles: &BTreeSet<String>) -> Result<bool, XrpcError> {
let memberships = self.records("membership")?;
let holds = |m: &Value| listed(m, "roles", "role").iter().any(|r| roles.contains(r));
Ok(memberships.iter().any(|(_, m)| holds(m)))
}
/// Refuses a change that would leave nobody able to configure the
/// group: `who` holding `roles` after it, or out of it with none.
fn keeps_a_configurer(&self, who: &str, roles: &[String]) -> Result<(), XrpcError> {
@@ -230,14 +242,14 @@ pub async fn put_role(
return Err(invalid(format!("{flag} is true or false")));
}
}
// The last role able to configure the group keeps that, or nobody
// could run it again.
let configuring = members.configuring();
let last = configuring.len() == 1 && configuring.contains(role);
if last && !actions.iter().any(|a| a == "group.configure") {
// A role giving up configuring the group leaves someone who still
// may, or nobody could run it again.
let mut configuring = members.configuring();
let gives_up = !actions.iter().any(|a| a == "group.configure");
if gives_up && configuring.remove(role) && !members.held(&configuring)? {
return Err(bad(
"LastConfigurer",
"the only role that configures the group keeps that",
"nobody else holds a role that configures the group: give one to someone first",
));
}
let collection = format!("{NS}.role");
crates/group-host/tests/joining.rs+60−0
@@ -353,3 +353,63 @@ async fn a_group_that_approves_its_members_keeps_requests() {
let (_, members) = get(&h, &lxm, &format!("group={did}"), &[auth(&h, &bob, &lxm)]).await;
assert_eq!(members["group"]["viewer"].get("request"), None);
}
#[tokio::test]
async fn admitting_takes_admit_and_gives_member() {
let h = start(&[], false).await;
let [alice, bob, carol] = people(&h).await;
let did = group(&h, &alice, "approval").await;
// A gatekeeper may admit, and give member and rider, and nothing else.
call(
&h,
&alice,
"putRole",
json!({"group": did, "role": "rider", "name": "Rider", "actions": []}),
)
.await;
call(&h, &alice, "putRole", json!({"group": did, "role": "gatekeeper", "name": "Gatekeeper", "actions": ["admit"], "assignable": [{"role": "member"}, {"role": "rider"}]})).await;
h.host
.space_write(
&did,
&group_host::group::members_space(&did),
&[group_host::repo::Write::Create {
collection: format!("{NS}.membership"),
rkey: bob.did.clone(),
value: json!({"$type": format!("{NS}.membership"), "member": bob.did, "roles": [{"role": "member"}, {"role": "gatekeeper"}], "createdAt": "2026-11-03T00:00:00Z"}),
}],
)
.await
.unwrap();
call(&h, &carol, "requestJoin", json!({"group": did})).await;
// Named roles that leave out member still give it.
let said = call(
&h,
&bob,
"admitMember",
json!({"group": did, "did": carol.did, "roles": [{"role": "rider"}]}),
)
.await;
assert_eq!(said, (200, json!({})));
assert_eq!(h.host.roles(&did, &carol.did).unwrap(), ["member", "rider"]);
// A newcomer gets member even when no role marked default is member.
for (role, default) in [("member", false), ("rider", true)] {
call(
&h,
&alice,
"putRole",
json!({"group": did, "role": role, "name": role, "actions": [], "default": default}),
)
.await;
}
let dave = authority(&h, false, true).await;
call(&h, &dave, "requestJoin", json!({"group": did})).await;
let said = call(
&h,
&bob,
"admitMember",
json!({"group": did, "did": dave.did}),
)
.await;
assert_eq!(said.0, 200);
assert_eq!(h.host.roles(&did, &dave.did).unwrap(), ["member", "rider"]);
}
crates/group-host/tests/members.rs+35−0
@@ -521,3 +521,38 @@ async fn members_leave_but_the_last_who_runs_it_stays() {
(400, "InvalidRequest")
);
}
#[tokio::test]
async fn a_role_nobody_holds_keeps_nobody_running_it() {
let h = start(&[], false).await;
let (did, [alice, _, carol]) = group(&h).await;
// A second role that configures, which nobody holds.
let every: Vec<&str> = group_host::rbac::ACTIONS.to_vec();
call(
&h,
&alice,
"putRole",
json!({"group": did, "role": "owner", "name": "Owner", "actions": every}),
)
.await;
// Admin may not give it up then: nobody would be left who runs it.
let said = call(
&h,
&alice,
"putRole",
json!({"group": did, "role": "admin", "name": "Admin", "actions": ["invite"]}),
)
.await;
assert_eq!(error(&said), (400, "LastConfigurer"));
assert!(grant_of(&h, &did, "admin").unwrap().scopes.len() > 1);
// Once someone holds owner, it may.
join(&h, &did, &carol.did, &["member", "owner"]).await;
let said = call(
&h,
&alice,
"putRole",
json!({"group": did, "role": "admin", "name": "Admin", "actions": ["invite"]}),
)
.await;
assert_eq!(said.0, 200);
}
docs/design/membership.md+2−1
@@ -46,7 +46,8 @@ Power over other members needs its own limit. A grant's `assignable` lists
the roles its holders may give or take, and whose holders they may eject.
It counts only through the grant that carries the action: whom a member
may assign comes from their roles granting `role.assign`, whom they may
eject from those granting `eject`.
eject from those granting `eject`, and what they may admit someone with
from those granting `admit`.
A new group has two roles. `admin` grants every action and `repo:*`, and
may assign only `member`. `member` is the default role and grants nothing.