75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"chat-agent-system/internal/auth"
|
|
)
|
|
|
|
// handleUserDirectory GET /api/v1/users/directory
|
|
// 任何已登录用户可读取 active 用户轻量目录(立项选人 / 负责人选择器)。
|
|
func (s *Server) handleUserDirectory(w http.ResponseWriter, r *http.Request) {
|
|
if _, ok := s.requirePrincipal(w, r); !ok {
|
|
return
|
|
}
|
|
if r.Method != http.MethodGet {
|
|
writeSafeError(w, r, http.StatusMethodNotAllowed, "method not allowed")
|
|
return
|
|
}
|
|
users, err := s.authSvc().ListUserDirectory(r.Context())
|
|
if err != nil {
|
|
writeDomainError(w, r, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
writeJSONResponse(w, http.StatusOK, map[string]any{"users": users})
|
|
}
|
|
|
|
// handleAdminUpdateProfile PATCH /api/v1/admin/users/{id}/profile
|
|
func (s *Server) handleAdminUpdateProfile(w http.ResponseWriter, r *http.Request) {
|
|
actor, ok := s.requireSystemAdmin(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
if r.Method != http.MethodPatch {
|
|
writeSafeError(w, r, http.StatusMethodNotAllowed, "method not allowed")
|
|
return
|
|
}
|
|
targetID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil || targetID <= 0 {
|
|
writeSafeError(w, r, http.StatusBadRequest, "无效的用户ID")
|
|
return
|
|
}
|
|
var body struct {
|
|
Name *string `json:"name,omitempty"`
|
|
Department *string `json:"department,omitempty"`
|
|
Email *string `json:"email,omitempty"`
|
|
EmployeeID *string `json:"employee_id,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
writeSafeError(w, r, http.StatusBadRequest, "无效的请求体")
|
|
return
|
|
}
|
|
if body.Name == nil && body.Department == nil && body.Email == nil && body.EmployeeID == nil {
|
|
writeSafeError(w, r, http.StatusBadRequest, "至少提供一个可更新字段")
|
|
return
|
|
}
|
|
if body.Name != nil && strings.TrimSpace(*body.Name) == "" {
|
|
writeSafeError(w, r, http.StatusBadRequest, "姓名不能为空")
|
|
return
|
|
}
|
|
updated, err := s.authSvc().AdminUpdateUserProfile(r.Context(), targetID, auth.ProfilePatch{
|
|
Name: body.Name,
|
|
Department: body.Department,
|
|
Email: body.Email,
|
|
EmployeeID: body.EmployeeID,
|
|
}, actor.ID)
|
|
if err != nil {
|
|
writeDomainError(w, r, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
writeJSONResponse(w, http.StatusOK, updated)
|
|
}
|