woodpecker/pkg/server/users.go

132 lines
2.8 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/types"
2015-04-08 22:43:59 +00:00
)
// GetUsers accepts a request to retrieve all users
// from the datastore and return encoded in JSON format.
//
// GET /api/users
//
func GetUsers(c *gin.Context) {
store := ToDatastore(c)
users, err := store.UserList()
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(400, err)
} else {
c.JSON(200, users)
}
}
// PostUser accepts a request to create a new user in the
// system. The created user account is returned in JSON
// format if successful.
//
// POST /api/users
//
func PostUser(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
name := c.Params.ByName("name")
user := &types.User{Login: name}
2015-04-08 22:54:08 +00:00
user.Token = c.Request.FormValue("token")
user.Secret = c.Request.FormValue("secret")
user.Hash = c.Request.FormValue("hash")
if len(user.Hash) == 0 {
user.Hash = types.GenerateToken()
}
if err := store.AddUser(user); err != nil {
2015-04-08 22:43:59 +00:00
c.Fail(400, err)
} else {
c.JSON(201, user)
}
}
// GetUser accepts a request to retrieve a user by hostname
// and login from the datastore and return encoded in JSON
// format.
//
// GET /api/users/:name
//
func GetUser(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
name := c.Params.ByName("name")
user, err := store.UserLogin(name)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(404, err)
} else {
c.JSON(200, user)
}
}
// PutUser accepts a request to update an existing user in
// the system. The modified user account is returned in JSON
// format if successful.
//
// PUT /api/users/:name
//
func PutUser(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
me := ToUser(c)
name := c.Params.ByName("name")
user, err := store.UserLogin(name)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(404, err)
return
}
in := &types.User{}
2015-04-08 22:43:59 +00:00
if !c.BindWith(in, binding.JSON) {
return
}
user.Email = in.Email
2015-06-18 23:37:40 +00:00
user.Avatar = gravatar.Hash(user.Email)
2015-04-08 22:43:59 +00:00
// an administrator must not be able to
// downgrade her own account.
if me.Login != user.Login {
user.Admin = in.Admin
}
err = store.SetUser(user)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(400, err)
} else {
c.JSON(200, user)
}
}
// DeleteUser accepts a request to delete the specified
// user account from the system. A successful request will
// respond with an OK 200 status.
//
// DELETE /api/users/:name
//
func DeleteUser(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
me := ToUser(c)
name := c.Params.ByName("name")
user, err := store.UserLogin(name)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(404, err)
return
}
// an administrator must not be able to
// delete her own account.
if user.Login == me.Login {
c.Writer.WriteHeader(403)
return
}
if err := store.DelUser(user); err != nil {
2015-04-08 22:43:59 +00:00
c.Fail(400, err)
} else {
c.Writer.WriteHeader(204)
}
}