harness-drone/handler/api/users/find.go

54 lines
1.6 KiB
Go
Raw Normal View History

2019-03-05 23:16:58 +00:00
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2019-02-19 23:56:41 +00:00
package users
import (
"net/http"
2020-02-07 17:19:32 +00:00
"strconv"
2019-02-19 23:56:41 +00:00
2019-03-05 23:16:58 +00:00
"github.com/drone/drone/core"
2019-02-19 23:56:41 +00:00
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/logger"
"github.com/go-chi/chi"
)
// HandleFind returns an http.HandlerFunc that writes json-encoded
// user account information to the the response body.
func HandleFind(users core.UserStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
login := chi.URLParam(r, "user")
user, err := users.FindLogin(r.Context(), login)
if err != nil {
2020-02-07 17:19:32 +00:00
// the client can make a user request by providing
// the user id as opposed to the username. If a
2021-06-07 10:20:55 +00:00
// numeric user id is provided as input, attempt
2020-02-07 17:19:32 +00:00
// to lookup the user by id.
if id, _ := strconv.ParseInt(login, 10, 64); id != 0 {
user, err = users.Find(r.Context(), id)
if err == nil {
render.JSON(w, user, 200)
return
}
}
2019-02-19 23:56:41 +00:00
render.NotFound(w, err)
logger.FromRequest(r).Debugln("api: cannot find user")
} else {
render.JSON(w, user, 200)
}
}
}