mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 06:40:54 +03:00
REST API: remove merging of fields on updates
we use PUT verb not PATCH. We keep merging only to allow to preserve hidden/encrypted fields. This is a backward incompatible change, but is necessary to avoid unexpected issues. You have to pass complete objects on updates. Fixes #1088 Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -116,13 +116,8 @@ func updateAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
adminID := admin.ID
|
||||
username = admin.Username
|
||||
totpConfig := admin.Filters.TOTPConfig
|
||||
recoveryCodes := admin.Filters.RecoveryCodes
|
||||
admin.Filters.TOTPConfig = dataprovider.AdminTOTPConfig{}
|
||||
admin.Filters.RecoveryCodes = nil
|
||||
err = render.DecodeJSON(r.Body, &admin)
|
||||
var updatedAdmin dataprovider.Admin
|
||||
err = render.DecodeJSON(r.Body, &updatedAdmin)
|
||||
if err != nil {
|
||||
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
||||
return
|
||||
@@ -139,24 +134,28 @@ func updateAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if claims.isCriticalPermRemoved(admin.Permissions) {
|
||||
if claims.isCriticalPermRemoved(updatedAdmin.Permissions) {
|
||||
sendAPIResponse(w, r, errors.New("you cannot remove these permissions to yourself"), "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if admin.Status == 0 {
|
||||
if updatedAdmin.Status == 0 {
|
||||
sendAPIResponse(w, r, errors.New("you cannot disable yourself"), "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if admin.Role != claims.Role {
|
||||
if updatedAdmin.Role != claims.Role {
|
||||
sendAPIResponse(w, r, errors.New("you cannot add/change your role"), "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
admin.ID = adminID
|
||||
admin.Username = username
|
||||
admin.Filters.TOTPConfig = totpConfig
|
||||
admin.Filters.RecoveryCodes = recoveryCodes
|
||||
if err := dataprovider.UpdateAdmin(&admin, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role); err != nil {
|
||||
updatedAdmin.ID = admin.ID
|
||||
updatedAdmin.Username = admin.Username
|
||||
if updatedAdmin.Password == "" {
|
||||
updatedAdmin.Password = admin.Password
|
||||
}
|
||||
updatedAdmin.Filters.TOTPConfig = admin.Filters.TOTPConfig
|
||||
updatedAdmin.Filters.RecoveryCodes = admin.Filters.RecoveryCodes
|
||||
err = dataprovider.UpdateAdmin(&updatedAdmin, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
||||
if err != nil {
|
||||
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user