feat(webui): act in a selected organisation the caller is a member of, enforced at the dashboard and the State Store

This commit is contained in:
sam committed 2026-10-27 11:06:18 +11:00
1 parent 2cf9d435cc
commit 2fb0084d2c
17 files changed
+356 -69

No files matched your search

+75
View File
@@ -108,6 +108,7 @@ func orgRoutes(s *Server) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /api/orgs", s.handleOrgsList)
mux.HandleFunc("POST /api/orgs", s.handleOrgCreate)
mux.HandleFunc("PUT /api/orgs/current", s.handleOrgSelect)
mux.HandleFunc("GET /api/orgs/{id}", s.handleOrgGet)
mux.HandleFunc("GET /api/orgs/{id}/workspaces", s.handleOrgWorkspacesList)
mux.HandleFunc("POST /api/orgs/{id}/workspaces", s.handleOrgWorkspaceCreate)
@@ -424,3 +425,77 @@ func TestOrgMemberAdd(t *testing.T) {
})
}
}
// memberPrincipals gives every identity memberships in org-sys and acme but
// not beta.
type memberPrincipals struct{}
func (memberPrincipals) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id, Org: org.DefaultOrgID, Memberships: []org.Membership{
{IdentityID: id, OrgID: org.DefaultOrgID, Role: org.RoleDeveloper},
{IdentityID: id, OrgID: "acme", Role: org.RoleDeveloper},
}}, nil
}
// TestOrgSelection holds the selection boundary: a caller acts in another org
// only when it is a member, whether it asks by selecting or by presenting an
// old cookie, and the server answers with the org it really acts in.
func TestOrgSelection(t *testing.T) {
tests := []struct {
name string
method string
path string
body string
cookie string
wantStatus int
wantCookie string // value of the org cookie the response sets, "-" for none
wantBody string
}{
{"selects a member org", http.MethodPut, "/api/orgs/current", `{"id":"acme"}`, "", http.StatusNoContent, "acme", ""},
{"refuses an org it is not a member of", http.MethodPut, "/api/orgs/current", `{"id":"beta"}`, "", http.StatusForbidden, "-", ""},
{"acts in the selected org", http.MethodGet, "/api/orgs", "", "acme", http.StatusOK, "-", `"current":"acme"`},
{"ignores and clears a cookie for a non-member org", http.MethodGet, "/api/orgs", "", "beta", http.StatusOK, "", `"current":"org-sys"`},
{"lists only the orgs it may select", http.MethodGet, "/api/orgs", "", "", http.StatusOK, "-", `"member_orgs":["org-sys","acme"]`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
orgs := newFakeOrgs()
orgs.orgs = append(orgs.orgs, org.Org{ID: "acme"}, org.Org{ID: "beta"})
s := &Server{
Access: allowAll{}, Principals: memberPrincipals{}, Orgs: orgs,
Authenticate: func(_ context.Context, token string) (identity.Identity, error) {
return identity.Identity{ID: identity.IdentityID(token), Kind: identity.KindUser}, nil
},
}
var body io.Reader
if tt.body != "" {
body = strings.NewReader(tt.body)
}
req := httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, body)
req.Header.Set("Authorization", "Bearer dev")
if tt.body != "" {
req.Header.Set("Content-Type", "application/json")
}
if tt.cookie != "" {
req.AddCookie(&http.Cookie{Name: s.cookies().org, Value: tt.cookie})
}
rec := httptest.NewRecorder()
orgRoutes(s).ServeHTTP(rec, req)
if rec.Code != tt.wantStatus {
t.Fatalf("status = %d, body %s", rec.Code, rec.Body.String())
}
set := "-"
for _, c := range rec.Result().Cookies() {
if c.Name == s.cookies().org {
set = c.Value
}
}
if set != tt.wantCookie {
t.Fatalf("org cookie = %q, want %q", set, tt.wantCookie)
}
if !strings.Contains(rec.Body.String(), tt.wantBody) {
t.Fatalf("body = %s, want it to contain %q", rec.Body.String(), tt.wantBody)
}
})
}
}