woodpecker/server/handler/user.go

114 lines
2.6 KiB
Go
Raw Normal View History

2014-06-04 21:25:38 +00:00
package handler
import (
"encoding/json"
"net/http"
2014-09-29 01:36:24 +00:00
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/shared/model"
2014-09-29 01:36:24 +00:00
"github.com/goji/context"
"github.com/zenazn/goji/web"
2014-06-04 21:25:38 +00:00
)
2014-09-29 01:36:24 +00:00
// GetUserCurrent accepts a request to retrieve the
// currently authenticated user from the datastore
// and return in JSON format.
//
// GET /api/user
//
func GetUserCurrent(c web.C, w http.ResponseWriter, r *http.Request) {
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// return private data for the currently authenticated
// user, specifically, their auth token.
2014-06-04 21:25:38 +00:00
data := struct {
*model.User
2014-06-04 21:25:38 +00:00
Token string `json:"token"`
2014-09-29 01:36:24 +00:00
}{user, user.Token}
json.NewEncoder(w).Encode(&data)
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// PutUser accepts a request to update the currently
// authenticated User profile.
//
// PUT /api/user
//
func PutUser(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
// unmarshal the repository from the payload
defer r.Body.Close()
in := model.User{}
2014-06-04 21:25:38 +00:00
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
2014-09-29 01:36:24 +00:00
w.WriteHeader(http.StatusBadRequest)
return
2014-06-04 21:25:38 +00:00
}
// update the user email
if len(in.Email) != 0 {
2014-09-29 01:36:24 +00:00
user.SetEmail(in.Email)
2014-06-04 21:25:38 +00:00
}
// update the user full name
if len(in.Name) != 0 {
2014-09-29 01:36:24 +00:00
user.Name = in.Name
2014-06-04 21:25:38 +00:00
}
// update the database
2014-09-29 01:36:24 +00:00
if err := datastore.PutUser(ctx, user); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
json.NewEncoder(w).Encode(user)
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// GetRepos accepts a request to get the currently
// authenticated user's repository list from the datastore,
// encoded and returned in JSON format.
//
// GET /api/user/repos
//
func GetUserRepos(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
repos, err := datastore.GetRepoList(ctx, user)
2014-06-04 21:25:38 +00:00
if err != nil {
2014-09-29 01:36:24 +00:00
w.WriteHeader(http.StatusNotFound)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
json.NewEncoder(w).Encode(&repos)
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// GetUserFeed accepts a request to get the user's latest
// build feed, across all repositories, from the datastore.
// The results are encoded and returned in JSON format.
//
// GET /api/user/feed
//
func GetUserFeed(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
repos, err := datastore.GetCommitListUser(ctx, user)
2014-06-04 21:25:38 +00:00
if err != nil {
2014-09-29 01:36:24 +00:00
w.WriteHeader(http.StatusNotFound)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
json.NewEncoder(w).Encode(&repos)
2014-06-04 21:25:38 +00:00
}