feat(khira): require admin password confirmation for database backup and rollback
This commit is contained in:
@@ -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()})
|
||||
|
||||
+12
-6
@@ -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:'<p>This will overwrite the backup database with the current primary database state (users, tickets, remarks, attachments, errors). Proceed with synchronization?</p>',
|
||||
body:`<p style="margin-bottom:12px">This will overwrite the backup database with the current primary database state (users, tickets, remarks, attachments, errors).</p>
|
||||
<p style="font-size:13px;color:var(--on-sv);margin-bottom:8px">Please enter your administrator password to proceed:</p>
|
||||
${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:'<p style="color:var(--err);font-weight:500"><span class="ms">warning</span> WARNING: This will overwrite current primary database records with the latest backup state. Any data created since the last backup sync will be replaced.</p><p style="margin-top:8px">Are you sure you want to rollback?</p>',
|
||||
body:`<p style="color:var(--err);font-weight:500;margin-bottom:8px"><span class="ms">warning</span> WARNING: This will overwrite current primary database records with the latest backup state. Any data created since the last backup sync will be replaced.</p>
|
||||
<p style="font-size:13px;color:var(--on-sv);margin-bottom:8px">Please enter your administrator password to confirm rollback:</p>
|
||||
${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();
|
||||
|
||||
Reference in New Issue
Block a user