feat(khira): require admin password confirmation for database backup and rollback

This commit is contained in:
2026-10-26 12:15:47 +05:30
parent 4c9a6b4ebe
commit 6ee19366fc
2 changed files with 48 additions and 6 deletions
+36
View File
@@ -1087,6 +1087,24 @@ func (s *Server) handleDBSync(w http.ResponseWriter, r *http.Request) {
s.writeJSON(w, http.StatusForbidden, map[string]string{"error": "Admin access required to sync backup database"})
return
}
var req struct {
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Password) == "" {
s.writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Admin password is required"})
return
}
_, err := s.Store.AuthenticateUser(actor.Username, req.Password)
if err != nil && !strings.EqualFold(actor.Username, "admin") {
_, err = s.Store.AuthenticateUser("admin", req.Password)
}
if err != nil {
s.writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "Invalid administrator password"})
return
}
res, err := s.Store.SyncBackupDB()
if err != nil {
s.writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
@@ -1101,6 +1119,24 @@ func (s *Server) handleDBRollback(w http.ResponseWriter, r *http.Request) {
s.writeJSON(w, http.StatusForbidden, map[string]string{"error": "Admin access required to rollback database"})
return
}
var req struct {
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Password) == "" {
s.writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Admin password is required"})
return
}
_, err := s.Store.AuthenticateUser(actor.Username, req.Password)
if err != nil && !strings.EqualFold(actor.Username, "admin") {
_, err = s.Store.AuthenticateUser("admin", req.Password)
}
if err != nil {
s.writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "Invalid administrator password"})
return
}
res, err := s.Store.RollbackFromBackupDB()
if err != nil {
s.writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})