feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,78 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sea-vhost/internal/auth"
|
||||
"sea-vhost/internal/store"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const realAdministratorKey contextKey = "real_administrator"
|
||||
const impersonationCookie = "sea_impersonation"
|
||||
|
||||
func (s *Server) startImpersonation(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
TenantID uint64 `json:"tenantId"`
|
||||
ProjectID uint64 `json:"projectId"`
|
||||
}
|
||||
if decodeJSON(r, &input) != nil || (input.TenantID == 0) == (input.ProjectID == 0) {
|
||||
writeError(w, 400, "请选择一个用户或主机")
|
||||
return
|
||||
}
|
||||
target, err := s.store.ImpersonationTarget(r.Context(), input.TenantID, input.ProjectID)
|
||||
if err != nil {
|
||||
writeError(w, 404, "目标账号不存在或不可用")
|
||||
return
|
||||
}
|
||||
parent, err := r.Cookie("sea_session")
|
||||
if err != nil {
|
||||
writeError(w, 401, "管理员登录已失效")
|
||||
return
|
||||
}
|
||||
token, err := auth.NewSessionToken(s.cfg.SessionSecret)
|
||||
if err != nil {
|
||||
writeError(w, 500, "无法建立代入会话")
|
||||
return
|
||||
}
|
||||
administrator := currentUser(r)
|
||||
expires := time.Now().Add(30 * time.Minute)
|
||||
if err = s.store.CreateImpersonation(r.Context(), parent.Value, token, administrator.ID, target.ID, expires); err != nil {
|
||||
writeError(w, 500, "无法建立代入会话")
|
||||
return
|
||||
}
|
||||
target, err = s.store.ImpersonationUser(r.Context(), parent.Value, token, administrator.ID)
|
||||
if err != nil {
|
||||
writeError(w, 500, "无法核对代入权限")
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: impersonationCookie, Value: token, Path: "/", HttpOnly: true, Secure: s.cfg.CookieSecure, SameSite: http.SameSiteStrictMode, Expires: *target.ImpersonationExpiresAt, MaxAge: int(time.Until(*target.ImpersonationExpiresAt).Seconds())})
|
||||
s.store.Log(r.Context(), target.TenantID, administrator.ID, "start", "impersonation", strconv.FormatUint(target.ID, 10), requestClientIP(r), true, "管理员快捷进入 "+target.Role+" 账号 "+target.LoginName)
|
||||
writeJSON(w, 200, map[string]any{"user": target})
|
||||
}
|
||||
|
||||
func (s *Server) stopImpersonation(w http.ResponseWriter, r *http.Request) {
|
||||
administrator, ok := r.Context().Value(realAdministratorKey).(store.User)
|
||||
if !ok {
|
||||
administrator = currentUser(r)
|
||||
}
|
||||
if administrator.Role != "admin" {
|
||||
writeError(w, 403, "需要原管理员会话")
|
||||
return
|
||||
}
|
||||
parent, _ := r.Cookie("sea_session")
|
||||
child, _ := r.Cookie(impersonationCookie)
|
||||
if parent != nil && child != nil {
|
||||
if err := s.store.StopImpersonation(r.Context(), parent.Value, child.Value); err != nil {
|
||||
writeError(w, 500, "无法退出代入会话")
|
||||
return
|
||||
}
|
||||
}
|
||||
s.clearImpersonation(w)
|
||||
s.store.Log(context.WithoutCancel(r.Context()), nil, administrator.ID, "stop", "impersonation", "", requestClientIP(r), true, "管理员退出代入模式")
|
||||
writeJSON(w, 200, map[string]any{"user": administrator})
|
||||
}
|
||||
func (s *Server) clearImpersonation(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{Name: impersonationCookie, Value: "", Path: "/", HttpOnly: true, Secure: s.cfg.CookieSecure, SameSite: http.SameSiteStrictMode, MaxAge: -1})
|
||||
}
|
||||
Reference in new issue
Block a user