diff --git a/internal/khira/handlers.go b/internal/khira/handlers.go index 849c10e..e63a16c 100644 --- a/internal/khira/handlers.go +++ b/internal/khira/handlers.go @@ -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()}) diff --git a/web/khira/app.js b/web/khira/app.js index 9c4f9c0..3868d72 100644 --- a/web/khira/app.js +++ b/web/khira/app.js @@ -302,10 +302,13 @@ function adminSyncDB(){ dlg({ title:'Sync Primary DB to Backup DB', sub:'Mirror all primary database tables to the backup database.', - body:'
This will overwrite the backup database with the current primary database state (users, tickets, remarks, attachments, errors). Proceed with synchronization?
', + body:`This will overwrite the backup database with the current primary database state (users, tickets, remarks, attachments, errors).
+Please enter your administrator password to proceed:
+ ${fld('password','Admin Password',{t:'password',req:1})}`, ok:'Sync Backup DB', - go:async()=>{ - const r=await api('/khira/api/admin/db/sync','POST'); + go:async(v)=>{ + if(!v.password){toast('Admin password is required','err');return false} + const r=await api('/khira/api/admin/db/sync','POST',{password:v.password}); if(r._f||r.error){toast(r.error||'Database sync failed','err');return false} toast(r.message||'Backup database synced successfully'); await refresh(); @@ -317,10 +320,13 @@ function adminRollbackDB(){ dlg({ title:'Rollback Primary DB from Backup', sub:'Restore primary database state from the backup database.', - body:'warning WARNING: This will overwrite current primary database records with the latest backup state. Any data created since the last backup sync will be replaced.
Are you sure you want to rollback?
', + body:`warning WARNING: This will overwrite current primary database records with the latest backup state. Any data created since the last backup sync will be replaced.
+Please enter your administrator password to confirm rollback:
+ ${fld('password','Admin Password',{t:'password',req:1})}`, ok:'Rollback to Backup', - go:async()=>{ - const r=await api('/khira/api/admin/db/rollback','POST'); + go:async(v)=>{ + if(!v.password){toast('Admin password is required','err');return false} + const r=await api('/khira/api/admin/db/rollback','POST',{password:v.password}); if(r._f||r.error){toast(r.error||'Rollback failed','err');return false} toast(r.message||'Primary database rolled back successfully'); await refresh();