harness-drone/pkg/server/user.go

92 lines
2 KiB
Go
Raw Normal View History

2015-04-08 22:43:59 +00:00
package server
import (
2015-05-22 18:37:40 +00: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-08 22:43:59 +00:00
"github.com/drone/drone/pkg/token"
"github.com/drone/drone/pkg/types"
2015-04-08 22:43:59 +00: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) {
c.JSON(200, ToUser(c))
}
// PutUserCurr accepts a request to update the currently
// authenticated User profile.
//
// PUT /api/user
//
func PutUserCurr(c *gin.Context) {
store := ToDatastore(c)
user := ToUser(c)
2015-04-08 22:43:59 +00:00
in := &types.User{}
2015-04-08 22:43:59 +00:00
if !c.BindWith(in, binding.JSON) {
return
}
// TODO: we are no longer auto-generating avatar
user.Email = in.Email
2015-06-18 23:37:40 +00:00
user.Avatar = gravatar.Hash(in.Email)
err := store.SetUser(user)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(400, err)
} else {
c.JSON(200, user)
2015-04-08 22:43:59 +00: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) {
store := ToDatastore(c)
user := ToUser(c)
repos, err := store.RepoList(user)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(400, err)
} else {
c.JSON(200, &repos)
}
}
2015-04-11 05:22:55 +00:00
2015-08-18 23:09:27 +00: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)
}
}
// POST /api/user/token
func PostUserToken(c *gin.Context) {
user := ToUser(c)
t := token.New(token.UserToken, user.Login)
s, err := t.Sign(user.Hash)
if err != nil {
c.Fail(500, err)
} else {
c.String(200, "application/jwt", s)
}
2015-04-11 05:22:55 +00:00
}