harness-drone/handler/api/user/update.go

43 lines
1.1 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package user
import (
"encoding/json"
"net/http"
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/handler/api/request"
"github.com/drone/drone/logger"
"github.com/drone/drone/core"
)
// HandleUpdate returns an http.HandlerFunc that processes an http.Request
// to update the current user account.
func HandleUpdate(users core.UserStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
viewer, _ := request.UserFrom(r.Context())
in := new(core.User)
err := json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequest(w, err)
logger.FromRequest(r).WithError(err).
Debugln("api: cannot unmarshal request body")
return
}
viewer.Email = in.Email
err = users.Update(r.Context(), viewer)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).WithError(err).
Warnln("api: cannot update user")
} else {
render.JSON(w, viewer, 200)
}
}
}