2015-04-09 00:43:59 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
2015-04-16 09:31:17 +02:00
|
|
|
"github.com/ungerik/go-gravatar"
|
2015-04-09 00:43:59 +02:00
|
|
|
|
|
|
|
"github.com/drone/drone/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
c.JSON(200, ToUser(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
in := &common.User{}
|
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
return
|
|
|
|
}
|
2015-04-15 07:04:38 +02:00
|
|
|
user.Email = in.Email
|
2015-04-16 09:31:17 +02:00
|
|
|
user.Gravatar = 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
|
|
|
|
|
|
|
// GetUserTokens accepts a request to get the currently
|
|
|
|
// authenticated user's token list from the datastore,
|
|
|
|
// encoded and returned in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/user/tokens
|
|
|
|
//
|
|
|
|
func GetUserTokens(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
|
|
|
tokens, err := store.TokenList(user)
|
2015-04-14 01:33:29 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, &tokens)
|
|
|
|
}
|
2015-04-11 07:22:55 +02:00
|
|
|
}
|