Files
Agent-System/internal/server/me_settings_handlers.go
T
weichenandCursor 90f77eac93 feat(authz): implement six-phase permissions, settings and workflow v2
Close P0 auth gaps for lineage/runs, stage ABAC fail-closed, and
project:create; add IAM migrations 00021-23, me settings APIs, schema v2
manifests, orchestrator skeleton, and gate A regression coverage.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 02:50:23 +00:00

88 lines
2.4 KiB
Go

package server
import (
"encoding/json"
"net/http"
)
type userProfilePatch struct {
Name *string `json:"name,omitempty"`
Email *string `json:"email,omitempty"`
Locale *string `json:"locale,omitempty"`
Timezone *string `json:"timezone,omitempty"`
}
// handleMeProfile GET/PATCH /api/v1/me/profile
func (s *Server) handleMeProfile(w http.ResponseWriter, r *http.Request) {
user, ok := s.requirePrincipal(w, r)
if !ok {
return
}
switch r.Method {
case http.MethodGet:
writeJSONResponse(w, http.StatusOK, map[string]any{
"id": user.ID, "username": user.Username, "name": user.Name,
"email": user.Email, "locale": user.Locale, "timezone": user.Timezone,
"department": user.Department, "employee_id": user.EmployeeID, "position": user.Position,
})
case http.MethodPatch:
var patch userProfilePatch
if err := json.NewDecoder(r.Body).Decode(&patch); err != nil {
http.Error(w, "invalid body", http.StatusBadRequest)
return
}
updated := *user
if patch.Name != nil {
updated.Name = *patch.Name
}
if patch.Email != nil {
updated.Email = *patch.Email
}
if patch.Locale != nil {
updated.Locale = *patch.Locale
}
if patch.Timezone != nil {
updated.Timezone = *patch.Timezone
}
saved, err := s.authSvc.UpdateUserProfile(r.Context(), &updated)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
writeJSONResponse(w, http.StatusOK, saved)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
// handleMePreferences GET/PUT /api/v1/me/preferences
func (s *Server) handleMePreferences(w http.ResponseWriter, r *http.Request) {
user, ok := s.requirePrincipal(w, r)
if !ok {
return
}
switch r.Method {
case http.MethodGet:
prefs, err := s.authSvc.GetUserPreferences(r.Context(), user.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSONResponse(w, http.StatusOK, prefs)
case http.MethodPut:
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, "invalid body", http.StatusBadRequest)
return
}
prefs, err := s.authSvc.SaveUserPreferences(r.Context(), user.ID, body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSONResponse(w, http.StatusOK, prefs)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}