2021-01-20 22:52:25 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-09-13 12:18:40 +02:00
|
|
|
"io"
|
2021-01-20 22:52:25 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-09-13 12:18:40 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-06-11 14:24:51 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/audit"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-01-20 22:52:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AdminSetPasswordData struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleAdminSetPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
|
2022-09-13 12:18:40 +02:00
|
|
|
requestBody, err := io.ReadAll(r.Body)
|
2021-01-20 22:52:25 +01:00
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var requestData AdminSetPasswordData
|
|
|
|
err = json.Unmarshal(requestBody, &requestData)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "adminSetPassword", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelAuth, auditRec)
|
|
|
|
auditRec.AddMeta("username", username)
|
|
|
|
|
2021-01-20 22:52:25 +01:00
|
|
|
if !strings.Contains(requestData.Password, "") {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("password is required"))
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:03:42 +02:00
|
|
|
err = a.app.UpdateUserPassword(username, requestData.Password)
|
2021-01-20 22:52:25 +01:00
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2021-01-20 22:52:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
a.logger.Debug("AdminSetPassword, username: %s", mlog.String("username", username))
|
2021-01-20 22:52:25 +01:00
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.Success()
|
2021-01-20 22:52:25 +01:00
|
|
|
}
|