Staff roles (viewer, operator, billing) and the console's Audit view
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f8fcecd640
commit
b60cf45adf
19 files changed
+352
-24
No files matched your search
@@ -20,7 +20,7 @@ func (p *AccountPages) AdminPage(dashboard http.HandlerFunc) http.HandlerFunc {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
if !staffMay(user, r) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -442,7 +442,7 @@ func (p *AccountPages) adminOnly(
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
if !staffMay(user, r) {
|
||||
// 404 rather than 403: a 403 confirms the page exists.
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
|
||||
+5
-2
@@ -17,8 +17,10 @@ type Nav struct {
|
||||
// Current is the page being shown, so its link is marked rather than
|
||||
// offered: "cluster", "people", "quotas", "usage", "mirror", "chat", "keys".
|
||||
Current string
|
||||
// Admin shows the administration links.
|
||||
// Admin shows the administration links, and Role which of them: a
|
||||
// billing role sees usage and quotas only.
|
||||
Admin bool
|
||||
Role string
|
||||
// Mirror and Quotas are whether those pages were mounted at all; usage is
|
||||
// mounted with quotas.
|
||||
Mirror, Quotas bool
|
||||
@@ -37,7 +39,8 @@ type navKey struct{}
|
||||
func (p *AccountPages) navFor(r *http.Request, user store.User, current string) Nav {
|
||||
n := Nav{
|
||||
Current: current,
|
||||
Admin: user.IsAdmin,
|
||||
Admin: user.Staff(),
|
||||
Role: user.StaffRole(),
|
||||
Mirror: p.hasMirror,
|
||||
Quotas: p.hasQuotas,
|
||||
Guard: p.hasGuard,
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// staffMay reports whether a person may do this to an administration page.
|
||||
//
|
||||
// An administrator may do anything. A viewer may look at every page and
|
||||
// change nothing. An operator may also run the cluster from them: models
|
||||
// through the mirror, and a node's engines. Billing may look at usage and
|
||||
// quotas. Everything that touches people, the tree, limits or keys stays
|
||||
// with administrators.
|
||||
func staffMay(u store.User, r *http.Request) bool {
|
||||
if u.IsAdmin {
|
||||
return true
|
||||
}
|
||||
read := r.Method == http.MethodGet || r.Method == http.MethodHead
|
||||
path := r.URL.Path
|
||||
switch u.Role {
|
||||
case store.RoleViewer:
|
||||
return read
|
||||
case store.RoleOperator:
|
||||
return read || strings.HasPrefix(path, "/admin/mirror/") ||
|
||||
strings.HasPrefix(path, "/admin/nodes/") && strings.HasSuffix(path, "/engines")
|
||||
case store.RoleBilling:
|
||||
return read && (path == "/admin/usage" || path == "/admin/quotas")
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
func TestEachStaffRoleMayDoWhatItSays(t *testing.T) {
|
||||
cases := []struct {
|
||||
role, method, path string
|
||||
may bool
|
||||
}{
|
||||
{store.RoleViewer, "GET", "/admin/users", true},
|
||||
{store.RoleViewer, "POST", "/admin/users/u1/suspend", false},
|
||||
{store.RoleViewer, "POST", "/admin/mirror/fetch", false},
|
||||
{store.RoleOperator, "GET", "/admin", true},
|
||||
{store.RoleOperator, "POST", "/admin/mirror/fetch", true},
|
||||
{store.RoleOperator, "POST", "/admin/nodes/n1/engines", true},
|
||||
{store.RoleOperator, "POST", "/admin/quotas", false},
|
||||
{store.RoleOperator, "POST", "/admin/tenancy/org", false},
|
||||
{store.RoleBilling, "GET", "/admin/usage", true},
|
||||
{store.RoleBilling, "GET", "/admin/quotas", true},
|
||||
{store.RoleBilling, "GET", "/admin/users", false},
|
||||
{store.RoleBilling, "POST", "/admin/quotas", false},
|
||||
{"", "GET", "/admin", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
u := store.User{Role: c.role}
|
||||
if got := staffMay(u, httptest.NewRequest(c.method, c.path, nil)); got != c.may {
|
||||
t.Errorf("%s %s %s: %v", c.role, c.method, c.path, got)
|
||||
}
|
||||
}
|
||||
if !staffMay(store.User{IsAdmin: true}, httptest.NewRequest("POST", "/admin/users/u1/delete", nil)) {
|
||||
t.Error("an administrator may do anything")
|
||||
}
|
||||
}
|
||||
|
||||
func TestABillingRoleIsShownUsageAndQuotasOnly(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
render(rec, "admin_guard", guardPageData{Nav: Nav{Admin: true, Role: store.RoleBilling}})
|
||||
body := rec.Body.String()
|
||||
if !strings.Contains(body, `href="/admin/usage"`) || strings.Contains(body, `href="/admin/users"`) {
|
||||
t.Errorf("menu: %s", body)
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,11 @@
|
||||
<a href="/org"{{if eq .Current "org"}} aria-current="page"{{end}}>Organization</a>
|
||||
<span class="menu-gap" aria-hidden="true"></span>
|
||||
{{- end}}
|
||||
{{- if .Admin}}
|
||||
{{- if and .Admin (eq .Role "billing")}}
|
||||
<a href="/admin/usage"{{if eq .Current "usage"}} aria-current="page"{{end}}>Usage</a>
|
||||
<a href="/admin/quotas"{{if eq .Current "quotas"}} aria-current="page"{{end}}>Quotas</a>
|
||||
<span class="menu-gap" aria-hidden="true"></span>
|
||||
{{- else if .Admin}}
|
||||
<a href="/admin"{{if eq .Current "cluster"}} aria-current="page"{{end}}>Cluster</a>
|
||||
{{- /* Limits and usage are tabs of the people page, not pages of their own. */}}
|
||||
<a href="/admin/users"{{if eq .Current "people"}} aria-current="page"{{end}}>People</a>
|
||||
|
||||
Reference in new issue
Block a user