harness-drone/server/handler/users.go

41 lines
863 B
Go
Raw Normal View History

2014-06-04 21:25:38 +00:00
package handler
import (
"encoding/json"
"net/http"
"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 {
users database.UserManager
2014-06-04 21:25:38 +00:00
sess session.Session
}
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))
}