Files
beardrive/internal/webapp/admin.go
T
Snow LeeandClaude Fable 5 baad9e6dc2 feat(hub): signup gating, admin lifecycle, and onboarding UX
Makes a self-hosted hub safe to expose on a public URL and operable
without hand-editing JSON — addressing the blocker/major findings from
the persona usability evaluations.

Signup gating (config auth block, all optional):
- allowed_domains: signup email must match (e.g. only @runbear.io)
- require_verification: email-link activation before sign-in (reuses mailer)
- require_approval: hub admins approve new accounts (admins list)
- brand shown on the sign-in page; allow_signup:false already hid Sign up
Accounts carry a Status (active/unverified/pending); non-active accounts
cannot authenticate.

Admin lifecycle (endpoints + web UI):
- org: rename, member role change, member remove (last-owner guarded),
  invite list + revoke
- project: create (web), rename, delete (from the org panel)
- hub admins: approve/deny pending signups (sidebar bell + panel)
- org-wide public-share audit with revoke

UX: onboarding empty-state (explains invites, paste-invite + create-project)
instead of a blank sidebar; visible "Search ⌘K" button; toasts replace
blocking alert(); responsive layout with an off-canvas sidebar; joining
via #join now survives a logged-out click (token carried through login).
Web uploads are attributed to the signed-in account, not the server.
Login/signup are rate-limited per IP.

Tests: domain/verification/approval gates, auth rate limit, org+project
lifecycle, owner-only guards, invite→join→role→remove over HTTP.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R7Q9ZKSZRTdvrSJkYLUmYs
2026-07-08 22:23:27 -07:00

162 lines
4.9 KiB
Go

package webapp
import (
"encoding/json"
"io"
"net/http"
)
// Administration surfaces: project lifecycle (rename/delete by the owning
// org's owner), hub-admin approval of pending signups, and an org-wide view
// of public share links. All of this is what makes a hub actually
// operable — an admin can offboard, clean up, and audit — without editing
// JSON files on the server by hand.
// projectOwner returns true when the request's account owns the project's org.
func (s *Server) projectOwner(r *http.Request, projectID string) bool {
if s.Orgs == nil || s.Auth == nil {
return true
}
org := s.orgOf(projectID)
if org == "" {
return true
}
return s.Orgs.Role(org, s.requestUser(r).Email) == RoleOwner
}
// handleProjectRename renames a project. Owner of its org only.
func (s *Server) handleProjectRename(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("project")
if s.Projects == nil {
http.Error(w, "this server does not host projects", http.StatusNotFound)
return
}
if _, ok := s.Projects.Get(id); !ok || !s.projectAllowed(r, id) {
http.Error(w, "no such project", http.StatusNotFound)
return
}
if !s.projectOwner(r, id) {
http.Error(w, "only an organization owner can rename a project", http.StatusForbidden)
return
}
var req struct {
Name string `json:"name"`
}
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<16)).Decode(&req); err != nil {
http.Error(w, "bad request: "+err.Error(), http.StatusBadRequest)
return
}
if err := s.Projects.Rename(id, req.Name); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
writeJSON(w, map[string]any{"ok": true})
}
// handleProjectDelete removes a project from the registry. Owner only.
// Storage (blobs, journals) is intentionally left in place.
func (s *Server) handleProjectDelete(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("project")
if s.Projects == nil {
http.Error(w, "this server does not host projects", http.StatusNotFound)
return
}
if _, ok := s.Projects.Get(id); !ok || !s.projectAllowed(r, id) {
http.Error(w, "no such project", http.StatusNotFound)
return
}
if !s.projectOwner(r, id) {
http.Error(w, "only an organization owner can delete a project", http.StatusForbidden)
return
}
if err := s.Projects.Delete(id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, map[string]any{"ok": true})
}
// handleOrgShares lists every live public share across the org's projects,
// so an owner can audit "what have we made public?" in one place. Any org
// member may view; only owners revoke (via the existing per-share endpoint).
func (s *Server) handleOrgShares(w http.ResponseWriter, r *http.Request) {
if s.Shares == nil || s.Orgs == nil {
http.Error(w, "sharing is not enabled on this server", http.StatusNotFound)
return
}
orgID := r.PathValue("org")
if s.Orgs.Role(orgID, s.requestUser(r).Email) == "" {
http.Error(w, "you are not a member of this organization", http.StatusForbidden)
return
}
out := []map[string]any{}
for _, p := range s.Projects.List() {
if p.Org != orgID {
continue
}
for _, sh := range s.Shares.List(p.ID) {
j := shareJSON(r, sh)
j["project_name"] = p.Name
out = append(out, j)
}
}
writeJSON(w, map[string]any{"shares": out})
}
// builtinAuth returns the concrete OSS auth provider, or nil for a swapped
// provider that doesn't support approval.
func (s *Server) builtinAuth() *BuiltinAuth {
a, _ := s.Auth.(*BuiltinAuth)
return a
}
// handleAdminPending lists accounts awaiting approval. Hub admins only.
func (s *Server) handleAdminPending(w http.ResponseWriter, r *http.Request) {
if !s.requestUser(r).Admin {
http.Error(w, "hub admins only", http.StatusForbidden)
return
}
a := s.builtinAuth()
if a == nil {
writeJSON(w, map[string]any{"pending": []any{}})
return
}
writeJSON(w, map[string]any{"pending": a.PendingUsers()})
}
// handleAdminApprove activates a pending account. Hub admins only.
func (s *Server) handleAdminApprove(w http.ResponseWriter, r *http.Request) {
if !s.requestUser(r).Admin {
http.Error(w, "hub admins only", http.StatusForbidden)
return
}
a := s.builtinAuth()
if a == nil {
http.Error(w, "approval is not supported by this auth provider", http.StatusNotFound)
return
}
if err := a.Approve(r.PathValue("id")); err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
writeJSON(w, map[string]any{"ok": true})
}
// handleAdminDeny removes a pending account. Hub admins only.
func (s *Server) handleAdminDeny(w http.ResponseWriter, r *http.Request) {
if !s.requestUser(r).Admin {
http.Error(w, "hub admins only", http.StatusForbidden)
return
}
a := s.builtinAuth()
if a == nil {
http.Error(w, "approval is not supported by this auth provider", http.StatusNotFound)
return
}
if err := a.Deny(r.PathValue("id")); err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
writeJSON(w, map[string]any{"ok": true})
}