feat: User Groups admin screen with the users count and privileged codes protected

- controller golem15.user.usergroups behind golem15.users.access_groups
- list with users_count from the list's own query; form with the checkbox
  permission editor stored in user_groups.permissions
- creating a group with a privileged code, changing a code to or from one and
  deleting a privileged group need golem15.users.manage_privileged_groups
- a group delete removes its users_groups rows in the same transaction
- UserGroup gains Fillable, Rules and the read-only UsersCount; PermissionSet.Text
- TestAdminGroups and the D-06 matrix in TestAdminPrivilegedGroups
This commit is contained in:
Jakub Zych
2026-10-05 13:52:26 +02:00
parent 9611708792
commit a3992879db
15 changed files with 987 additions and 1 deletions

View File

@@ -421,4 +421,136 @@ func TestAdminPrivilegedGroups(t *testing.T) {
t.Fatalf("group %s is locked although no code is privileged", strconv.FormatUint(uint64(id), 10))
}
}
// D-06: the group's code carries the privilege, so creating a group with
// a privileged code, changing a code to or from one, and deleting a
// privileged group need the extra permission on the User Groups screen.
// The harness database is shared and may hold several groups coded
// admin, so the matrix runs on two codes of its own next to admin.
priv, second := "priv-"+stamp, "priv2-"+stamp
coded := newAdminEnv(t, func(cfg *compass.Config) {
if err := cfg.Set("golem15.user.privileged_groups", []string{"admin", priv, second}); err != nil {
t.Fatal(err)
}
})
groupsOnly := coded.admin(t, permAccessGroups)
groupsManager := coded.admin(t, permAccessGroups, permManagePrivileged)
privGroup := coded.seedGroup(t, "Privileged "+stamp, priv)
plainCode := "plain-" + stamp
plainGroup := coded.seedGroup(t, "Plain "+stamp, plainCode)
holder := coded.seedUser(t, "holder", nil)
coded.join(t, holder, privGroup, plainGroup)
holds := func(code string) bool {
t.Helper()
held, err := classes.HasGroupCode(t.Context(), coded.gdb, holder.ID, code)
if err != nil {
t.Fatal(err)
}
return held
}
refusedCode := func(rec *httptest.ResponseRecorder) {
t.Helper()
if rec.Code != http.StatusForbidden {
t.Fatalf("status = %d, want 403: %s", rec.Code, rec.Body.String())
}
answer := adminDecode(t, rec)
if answer.Error.Code != "forbidden" || answer.Error.Message != privilegedGroupCodeForbidden {
t.Fatalf("refusal = %s", rec.Body.String())
}
if got := answer.Error.Details["code"]; len(answer.Error.Details) != 1 || len(got) != 1 || got[0] != privilegedGroupCodeForbidden {
t.Fatalf("refusal details = %v", answer.Error.Details)
}
}
sameGroup := func(id uint, name, code string) {
t.Helper()
got := coded.group(t, id)
if got == nil || got.Name != name || got.Code == nil || *got.Code != code {
t.Fatalf("group %d = %+v, want %s (%s)", id, got, name, code)
}
}
codeCount := func(code string) int64 {
t.Helper()
var n int64
if err := coded.gdb.Raw(`SELECT COUNT(*) FROM user_groups WHERE code = ?`, code).Scan(&n).Error; err != nil {
t.Fatal(err)
}
return n
}
groupPut := func(token string, id uint, body map[string]any) *httptest.ResponseRecorder {
t.Helper()
return coded.call(t, http.MethodPut, groupPath(id), token, body)
}
// Without the permission each of the four is refused and changes nothing.
refusedCode(coded.call(t, http.MethodPost, adminAPI(adminGroupsPath), groupsOnly, map[string]any{"name": "Second " + stamp, "code": second}))
if codeCount(second) != 0 {
t.Fatal("a refused create left a privileged group")
}
refusedCode(groupPut(groupsOnly, plainGroup.ID, map[string]any{"name": "Promoted " + stamp, "code": second}))
sameGroup(plainGroup.ID, "Plain "+stamp, plainCode)
refusedCode(groupPut(groupsOnly, privGroup.ID, map[string]any{"name": "Demoted " + stamp, "code": "demoted-" + stamp}))
sameGroup(privGroup.ID, "Privileged "+stamp, priv)
// From one privileged code to another is a change of a privileged code.
refusedCode(groupPut(groupsOnly, privGroup.ID, map[string]any{"name": "Privileged " + stamp, "code": second}))
sameGroup(privGroup.ID, "Privileged "+stamp, priv)
refusedCode(coded.call(t, http.MethodDelete, groupPath(privGroup.ID), groupsOnly, nil))
sameGroup(privGroup.ID, "Privileged "+stamp, priv)
pivot(coded, holder, privGroup, plainGroup)
if !holds(priv) || holds(second) {
t.Fatal("a refused group change moved the privilege")
}
// The default code admin is protected the same way, and a group without
// a code cannot be given a privileged one.
refusedCode(groupPut(groupsOnly, adminGroup.ID, map[string]any{"name": "Admins " + stamp, "code": "was-admin-" + stamp}))
refusedCode(coded.call(t, http.MethodDelete, groupPath(adminGroup.ID), groupsOnly, nil))
sameGroup(adminGroup.ID, "Admins "+stamp, "admin")
refusedCode(groupPut(groupsOnly, uncoded.ID, map[string]any{"name": "Uncoded " + stamp, "code": second}))
if got := coded.group(t, uncoded.ID); got == nil || got.Code != nil {
t.Fatalf("the uncoded group after a refused save = %+v", got)
}
// Everything else on a privileged group needs only the groups
// permission: its name, description and frontend permissions, with the
// code sent back as it is.
if rec := groupPut(groupsOnly, privGroup.ID, map[string]any{"name": "Privileged renamed " + stamp, "code": priv, "description": "Kept code", "permissions": map[string]int{}}); rec.Code != http.StatusOK {
t.Fatalf("name-only update of a privileged group = %d %s", rec.Code, rec.Body.String())
}
sameGroup(privGroup.ID, "Privileged renamed "+stamp, priv)
// Ordinary groups, a case variant of a privileged code included, are
// re-coded and deleted freely.
if rec := groupPut(groupsOnly, plainGroup.ID, map[string]any{"name": "Plain " + stamp, "code": "plain2-" + stamp}); rec.Code != http.StatusOK {
t.Fatalf("ordinary re-code = %d %s", rec.Code, rec.Body.String())
}
plainCode = "plain2-" + stamp
if rec := groupPut(groupsOnly, uncoded.ID, map[string]any{"name": "Uncoded " + stamp, "code": "coded-" + stamp}); rec.Code != http.StatusOK {
t.Fatalf("first code of an uncoded group = %d %s", rec.Code, rec.Body.String())
}
if rec := coded.call(t, http.MethodDelete, groupPath(upper.ID), groupsOnly, nil); rec.Code != http.StatusOK || coded.group(t, upper.ID) != nil {
t.Fatalf("delete of the ADMIN group = %d %s", rec.Code, rec.Body.String())
}
// With the permission all four succeed.
created := coded.call(t, http.MethodPost, adminAPI(adminGroupsPath), groupsManager, map[string]any{"name": "Second " + stamp, "code": second})
if created.Code != http.StatusCreated || codeCount(second) != 1 {
t.Fatalf("manager create of a privileged group = %d %s", created.Code, created.Body.String())
}
if rec := groupPut(groupsManager, privGroup.ID, map[string]any{"name": "Privileged renamed " + stamp, "code": "freed-" + stamp}); rec.Code != http.StatusOK {
t.Fatalf("manager re-code from a privileged code = %d %s", rec.Code, rec.Body.String())
}
if holds(priv) {
t.Fatal("the holder kept the privilege after the group lost its code")
}
if rec := groupPut(groupsManager, plainGroup.ID, map[string]any{"name": "Plain " + stamp, "code": priv}); rec.Code != http.StatusOK {
t.Fatalf("manager re-code to a privileged code = %d %s", rec.Code, rec.Body.String())
}
if !holds(priv) {
t.Fatal("the holder did not gain the privilege with the group's new code")
}
if rec := coded.call(t, http.MethodDelete, groupPath(plainGroup.ID), groupsManager, nil); rec.Code != http.StatusOK || coded.group(t, plainGroup.ID) != nil {
t.Fatalf("manager delete of a privileged group = %d %s", rec.Code, rec.Body.String())
}
pivot(coded, holder, privGroup)
if holds(priv) || coded.reload(t, holder.ID) == nil {
t.Fatal("the delete of the privileged group left the privilege or removed the member")
}
}