2021-01-20 22:52:25 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AdminSetPasswordData struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleAdminSetPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2021-02-17 20:29:20 +01:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var requestData AdminSetPasswordData
|
|
|
|
err = json.Unmarshal(requestBody, &requestData)
|
|
|
|
if err != nil {
|
2021-02-17 20:29:20 +01:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(requestData.Password, "") {
|
2021-02-17 20:29:20 +01:00
|
|
|
errorResponse(w, http.StatusBadRequest, "password is required", err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.app().UpdateUserPassword(username, requestData.Password)
|
|
|
|
if err != nil {
|
2021-02-17 20:29:20 +01:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("AdminSetPassword, username: %s", username)
|
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
2021-01-20 22:52:25 +01:00
|
|
|
}
|