- New auth.RequireAdmin middleware (gates by paliad.users.role='admin')
with API/browser-aware reject paths and a fail-closed lookup-error 500.
- Service: AdminCreateUser (onboard from existing auth.users), AdminUpdate
(full profile fields incl. additional_offices), AdminDeleteUser (also
removes project_teams + department_members memberships and clears any
led-Dezernat seat — auth.users is left intact), ListUnonboardedAuthUsers,
IsAdmin (implements auth.AdminLookup).
- Handlers: GET/POST /api/admin/users, GET /api/admin/users/unonboarded,
PATCH/DELETE /api/admin/users/{id}, plus GET /admin/team for the page.
All registered through RequireAdminFunc so non-admins get 403/302.
- Refuses to delete the last remaining admin and rejects role='admin'
assignment via the admin UI (still SQL-only) — same rules as PATCH /api/me.
- /admin/team page: full users table with inline edit (display_name, office,
role, dezernat, additional_offices, lang), trash with confirm, search +
office filters, "Onboard existing account" modal driven by
/api/admin/users/unonboarded, and an Invite button that re-opens the
shared sidebar invite modal.
- Sidebar gains a hidden Admin section that sidebar.ts reveals after a
successful /api/me lookup confirms role='admin' (fails closed on error).
- DE+EN i18n strings for the page, modal and table.
- Tests: require_admin_test.go covers admin-allowed, non-admin 403/302,
unauthenticated 401 and lookup-error fail-closed paths.
155 lines
5.3 KiB
Go
155 lines
5.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"mgit.msbls.de/m/patholo/internal/services"
|
|
)
|
|
|
|
// admin_users.go — backing endpoints for the /admin/team page (t-paliad-050).
|
|
// All four routes are registered behind RequireAdminFunc in handlers.go, so
|
|
// the in-handler logic can assume the caller already passed the admin gate
|
|
// and only the operation itself needs validation.
|
|
|
|
// GET /api/admin/users — full unredacted list of every paliad.users row.
|
|
func handleAdminListUsers(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDB(w) {
|
|
return
|
|
}
|
|
users, err := dbSvc.users.List(r.Context())
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, users)
|
|
}
|
|
|
|
// GET /api/admin/users/unonboarded — auth.users entries without a paliad.users
|
|
// row. Feeds the "direct add" dropdown so an admin can onboard a colleague
|
|
// who logged in but never finished the form.
|
|
func handleAdminListUnonboarded(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDB(w) {
|
|
return
|
|
}
|
|
rows, err := dbSvc.users.ListUnonboardedAuthUsers(r.Context())
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, rows)
|
|
}
|
|
|
|
// POST /api/admin/users — direct-create a paliad.users row for an existing
|
|
// auth.users entry. The recipient email's domain must already match the
|
|
// allowed-email policy (Supabase wouldn't have let them sign up otherwise),
|
|
// but we re-check here so a stale auth.users row from before the policy
|
|
// existed can't sneak through.
|
|
func handleAdminCreateUser(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDB(w) {
|
|
return
|
|
}
|
|
var input services.AdminCreateInput
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
|
return
|
|
}
|
|
if !isAllowedEmailDomain(input.Email) {
|
|
writeJSON(w, http.StatusForbidden, map[string]string{
|
|
"error": "email domain not on the HLC allow-list",
|
|
})
|
|
return
|
|
}
|
|
u, err := dbSvc.users.AdminCreateUser(r.Context(), input)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, services.ErrUserAlreadyOnboarded):
|
|
writeJSON(w, http.StatusConflict, map[string]string{
|
|
"error": "user already onboarded",
|
|
})
|
|
case errors.Is(err, services.ErrAdminBootstrapOnly):
|
|
writeJSON(w, http.StatusForbidden, map[string]string{
|
|
"error": "admin role cannot be assigned via the admin UI",
|
|
})
|
|
case errors.Is(err, services.ErrInvalidInput):
|
|
// AdminCreateUser uses ErrInvalidInput for both bad-shape inputs
|
|
// and the "no auth.users row for this email" case. Surfacing the
|
|
// raw message keeps the form's error display useful without a
|
|
// separate error type for each.
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
default:
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
|
}
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, u)
|
|
}
|
|
|
|
// PATCH /api/admin/users/{id} — mutate any paliad.users row.
|
|
func handleAdminUpdateUser(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDB(w) {
|
|
return
|
|
}
|
|
id, err := uuid.Parse(r.PathValue("id"))
|
|
if err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid id"})
|
|
return
|
|
}
|
|
var input services.AdminUpdateInput
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
|
return
|
|
}
|
|
u, err := dbSvc.users.AdminUpdateUser(r.Context(), id, input)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, services.ErrUserNotOnboarded):
|
|
writeJSON(w, http.StatusNotFound, map[string]string{"error": "user not found"})
|
|
case errors.Is(err, services.ErrAdminBootstrapOnly):
|
|
writeJSON(w, http.StatusForbidden, map[string]string{
|
|
"error": "admin role cannot be assigned via the admin UI",
|
|
})
|
|
case errors.Is(err, services.ErrInvalidInput):
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
default:
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
|
}
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, u)
|
|
}
|
|
|
|
// DELETE /api/admin/users/{id} — remove a paliad.users row + cascade clean-up
|
|
// of project_teams / department_members. auth.users is left intact so the
|
|
// user can re-onboard later if needed.
|
|
func handleAdminDeleteUser(w http.ResponseWriter, r *http.Request) {
|
|
if !requireDB(w) {
|
|
return
|
|
}
|
|
id, err := uuid.Parse(r.PathValue("id"))
|
|
if err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid id"})
|
|
return
|
|
}
|
|
if err := dbSvc.users.AdminDeleteUser(r.Context(), id); err != nil {
|
|
switch {
|
|
case errors.Is(err, services.ErrUserNotOnboarded):
|
|
writeJSON(w, http.StatusNotFound, map[string]string{"error": "user not found"})
|
|
case errors.Is(err, services.ErrInvalidInput):
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
default:
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
|
}
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// handleAdminTeamPage serves the SPA shell for /admin/team.
|
|
func handleAdminTeamPage(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "dist/admin-team.html")
|
|
}
|