2014-06-04 21:25:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/server/database"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/server/session"
|
|
|
|
"github.com/gorilla/pat"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsersHandler struct {
|
2014-06-12 23:41:04 +00:00
|
|
|
users database.UserManager
|
2014-06-04 21:25:38 +00:00
|
|
|
sess session.Session
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func NewUsersHandler(users database.UserManager, sess session.Session) *UsersHandler {
|
2014-06-04 21:25:38 +00:00
|
|
|
return &UsersHandler{users, sess}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsers gets all users.
|
|
|
|
// GET /api/users
|
|
|
|
func (h *UsersHandler) GetUsers(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
// get the user form the session
|
|
|
|
user := h.sess.User(r)
|
|
|
|
if user == nil || !user.Admin {
|
|
|
|
return notAuthorized{}
|
|
|
|
}
|
|
|
|
// get all users
|
|
|
|
users, err := h.users.List()
|
|
|
|
if err != nil {
|
|
|
|
return internalServerError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(w).Encode(users)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UsersHandler) Register(r *pat.Router) {
|
|
|
|
r.Get("/v1/users", errorHandler(h.GetUsers))
|
|
|
|
}
|