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
|
|
|
|
2015-09-09 21:05:52 +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) {
|
2015-04-15 05:04:38 +00:00
|
|
|
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) {
|
2015-04-15 05:04:38 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
name := c.Params.ByName("name")
|
2015-09-09 21:05:52 +00:00
|
|
|
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")
|
2015-09-09 21:05:52 +00:00
|
|
|
user.Hash = c.Request.FormValue("hash")
|
|
|
|
if len(user.Hash) == 0 {
|
|
|
|
user.Hash = types.GenerateToken()
|
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
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) {
|
2015-04-15 05:04:38 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
name := c.Params.ByName("name")
|
2015-05-11 07:45:31 +00:00
|
|
|
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) {
|
2015-04-15 05:04:38 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
me := ToUser(c)
|
|
|
|
name := c.Params.ByName("name")
|
2015-05-11 07:45:31 +00:00
|
|
|
user, err := store.UserLogin(name)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
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) {
|
2015-04-15 05:04:38 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
me := ToUser(c)
|
|
|
|
name := c.Params.ByName("name")
|
2015-05-11 07:45:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
if err := store.DelUser(user); err != nil {
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Fail(400, err)
|
|
|
|
} else {
|
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
}
|
|
|
|
}
|