diff --git a/app/services/system_groups.rb b/app/services/system_groups.rb index 2222a08..37001e1 100644 --- a/app/services/system_groups.rb +++ b/app/services/system_groups.rb @@ -11,8 +11,8 @@ # rows only, so it never touches a membership that was added by hand. # # `attributes` are the user columns that can change a membership: a save that -# changes one syncs the user. The staff, board, contractors and identity team -# groups follow roles instead (see RoleGrants), which sync the user after +# changes one syncs the user. The staff, board, contractors, identity team +# and patch leads groups follow roles instead (see RoleGrants), which sync the user after # each grant or revoke. module SystemGroups Definition = Data.define(:slug, :name, :description, :attributes, :rule) @@ -47,6 +47,14 @@ module SystemGroups Definition.new( slug: "identity-team", name: "Identity Team", description: "Users who run identity verification.", attributes: [], rule: ->(user) { user.identity_team } + ), + # Every patch lead, of any patch. For apps that every lead may use, such + # as Quilt, which then shows each lead only their own patch (the `patches` + # claim). It gives nothing by itself. Only programs that still exist count, + # the same rule as the Knot check (KnotGrants.any_patch_lead?). + Definition.new( + slug: "patch-leads", name: "Patch Leads", description: "Users with the patch lead role, for any patch.", + attributes: [], rule: ->(user) { KnotGrants.any_patch_lead?(user) } ) ].freeze diff --git a/docs/KYC.md b/docs/KYC.md index 45c6039..9806e57 100644 --- a/docs/KYC.md +++ b/docs/KYC.md @@ -431,7 +431,7 @@ flowchart TD I[Immediate rule: board, staff, contractor, patch lead removal, verifier removal] -->|RoleGrants.grant or .revoke| G G -->|insert or set revoked_at| UR[(user_roles)] UR -->|trigger log_user_role_changes| L[(privileged_attribute_changes)] - UR -->|after commit| SG[SystemGroups: staff, board, contractors, identity-team] + UR -->|after commit| SG[SystemGroups: staff, board, contractors, identity-team, patch-leads] SG --> D[Directory API and userinfo claims] ``` diff --git a/docs/OAUTH.md b/docs/OAUTH.md index d6a22b8..8864c51 100644 --- a/docs/OAUTH.md +++ b/docs/OAUTH.md @@ -379,6 +379,21 @@ A superadmin defines the roles of each patch, the same way as app roles (see unexpired group membership. An app reads the keys it knows and ignores the rest, so a new role on a patch never breaks an app. +The claim does not open an app. When an app is restricted, add the +`patch-leads` system group to the app's access list to let every patch lead +sign in. Weave keeps that group in line with the `patch_lead` role: a lead +joins it when the role is granted, for a program in the Knot settings, and +leaves it when the last one is revoked. The app then reads the `patches` claim +to show each lead only their own patches. + +Use the access list, not an app role. An app role on `patch-leads` also opens +the app, but it gives that role to every lead. For Quilt, the `viewer` app +role on this group would let each lead read every patch. + +After the deploy that adds the group, run `bin/rails groups:sync_system` once. +Until then, or until the hourly sync, the group does not exist and existing +leads are not in it. + Give a role that is meant for one app that app's prefix, so another app never reads it by mistake. Quilt reads only `lead` and `quilt:viewer`: a bare `viewer` that a patch app uses does not open the patch's finances in Quilt. diff --git a/spec/services/system_groups_spec.rb b/spec/services/system_groups_spec.rb index 5b8e801..5278645 100644 --- a/spec/services/system_groups_spec.rb +++ b/spec/services/system_groups_spec.rb @@ -61,6 +61,41 @@ def slugs_for(user) = user.reload.groups.pluck(:slug) end end + describe "patch-leads" do + let(:owner) { create(:user, :owner) } + + it "follows the patch lead role, for any patch" do + user = create(:user) + + RoleGrants.grant!(user, "patch_lead", scope: "ganymede", by: owner, reason: "spec") + expect(slugs_for(user)).to include("patch-leads") + expect(user.group_memberships.find_by!(group: group("patch-leads"))).to be_system + + RoleGrants.revoke!(user, "patch_lead", scope: "ganymede", by: owner, reason: "spec") + expect(slugs_for(user)).not_to include("patch-leads") + end + + it "is filled by sync_all for a lead whose role was written without RoleGrants" do + user = create(:user) + UserRole.create!(user:, role: Role.fetch!("patch_lead"), scope: "ganymede", granted_at: Time.current) + expect(slugs_for(user)).not_to include("patch-leads") + + described_class.sync_all + + expect(slugs_for(user)).to include("patch-leads") + end + + it "drops a lead whose program is no longer in the Knot settings" do + user = create(:user) + RoleGrants.grant!(user, "patch_lead", scope: "ganymede", by: owner, reason: "spec") + allow(KnotSettings).to receive(:program_keys).and_return([]) + + described_class.sync_all + + expect(slugs_for(user)).not_to include("patch-leads") + end + end + it "leaves a membership that was added by hand" do user = create(:user) described_class.ensure_groups!