2015-04-09 00:43:59 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-09-09 23:34:28 +02:00
|
|
|
// "crypto/sha1"
|
|
|
|
|
2015-05-22 20:37:40 +02:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/ungerik/go-gravatar"
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-09 23:05:52 +02:00
|
|
|
"github.com/drone/drone/pkg/token"
|
|
|
|
"github.com/drone/drone/pkg/types"
|
2015-04-09 00:43:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetUserCurr accepts a request to retrieve the
|
|
|
|
// currently authenticated user from the datastore
|
|
|
|
// and return in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/user
|
|
|
|
//
|
|
|
|
func GetUserCurr(c *gin.Context) {
|
2015-09-09 23:34:28 +02:00
|
|
|
u := ToUser(c)
|
|
|
|
// f := fmt.Printf("% x", sha1.Sum(u.Hash))
|
|
|
|
|
|
|
|
// v := struct {
|
|
|
|
// *types.User
|
|
|
|
|
|
|
|
// // token fingerprint
|
|
|
|
// Token string `json:"token"`
|
|
|
|
// }{u, f}
|
|
|
|
|
|
|
|
c.JSON(200, u)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutUserCurr accepts a request to update the currently
|
|
|
|
// authenticated User profile.
|
|
|
|
//
|
|
|
|
// PUT /api/user
|
|
|
|
//
|
|
|
|
func PutUserCurr(c *gin.Context) {
|
2015-04-15 07:04:38 +02:00
|
|
|
store := ToDatastore(c)
|
|
|
|
user := ToUser(c)
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-09 23:05:52 +02:00
|
|
|
in := &types.User{}
|
2015-04-09 00:43:59 +02:00
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
return
|
|
|
|
}
|
2015-08-03 08:50:08 +02:00
|
|
|
// TODO: we are no longer auto-generating avatar
|
2015-04-15 07:04:38 +02:00
|
|
|
user.Email = in.Email
|
2015-06-19 01:37:40 +02:00
|
|
|
user.Avatar = gravatar.Hash(in.Email)
|
2015-04-15 07:04:38 +02:00
|
|
|
err := store.SetUser(user)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
} else {
|
2015-04-15 07:04:38 +02:00
|
|
|
c.JSON(200, user)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserRepos 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 *gin.Context) {
|
2015-04-15 07:04:38 +02:00
|
|
|
store := ToDatastore(c)
|
|
|
|
user := ToUser(c)
|
2015-05-11 09:45:31 +02:00
|
|
|
repos, err := store.RepoList(user)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, &repos)
|
|
|
|
}
|
|
|
|
}
|
2015-04-11 07:22:55 +02:00
|
|
|
|
2015-08-19 01:09:27 +02:00
|
|
|
// GetUserFeed accepts a request to get the currently
|
|
|
|
// authenticated user's build feed from the datastore,
|
|
|
|
// encoded and returned in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/user/feed
|
|
|
|
//
|
|
|
|
func GetUserFeed(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
user := ToUser(c)
|
|
|
|
feed, err := store.UserFeed(user, 25, 0)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, &feed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:05:52 +02:00
|
|
|
// POST /api/user/token
|
|
|
|
func PostUserToken(c *gin.Context) {
|
2015-04-15 07:04:38 +02:00
|
|
|
user := ToUser(c)
|
2015-09-09 23:05:52 +02:00
|
|
|
|
2015-09-09 23:34:28 +02:00
|
|
|
token := token.New(token.UserToken, user.Login)
|
|
|
|
tokenstr, err := token.Sign(user.Hash)
|
2015-04-14 01:33:29 +02:00
|
|
|
if err != nil {
|
2015-09-09 23:05:52 +02:00
|
|
|
c.Fail(500, err)
|
2015-04-14 01:33:29 +02:00
|
|
|
} else {
|
2015-09-09 23:34:28 +02:00
|
|
|
c.String(200, tokenstr)
|
2015-04-14 01:33:29 +02:00
|
|
|
}
|
2015-04-11 07:22:55 +02:00
|
|
|
}
|