2016-03-30 20:15:28 +00:00
|
|
|
package api
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-30 01:21:17 +00:00
|
|
|
"net/http"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/drone/drone/shared/crypto"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/store"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
// swagger:route GET /users user getUserList
|
|
|
|
//
|
|
|
|
// Get the list of all registered users.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: user
|
|
|
|
//
|
2015-04-08 22:43:59 +00:00
|
|
|
func GetUsers(c *gin.Context) {
|
2015-10-21 23:14:02 +00:00
|
|
|
users, err := store.GetUserList(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2016-03-30 20:15:28 +00:00
|
|
|
c.String(500, "Error getting user list. %s", err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, users)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
// swagger:route GET /users/{login} user getUserLogin
|
|
|
|
//
|
|
|
|
// Get the user with the matching login.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: user
|
|
|
|
//
|
2015-04-08 22:43:59 +00:00
|
|
|
func GetUser(c *gin.Context) {
|
2015-10-21 23:14:02 +00:00
|
|
|
user, err := store.GetUserLogin(c, c.Param("login"))
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2016-03-30 20:15:28 +00:00
|
|
|
c.String(404, "Cannot find user. %s", err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, user)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func PatchUser(c *gin.Context) {
|
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
user, err := store.GetUserLogin(c, c.Param("login"))
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
user.Admin = in.Admin
|
|
|
|
user.Active = in.Active
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
err = store.UpdateUser(c, user)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func PostUser(c *gin.Context) {
|
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
user := &model.User{}
|
|
|
|
user.Login = in.Login
|
|
|
|
user.Email = in.Email
|
|
|
|
user.Admin = in.Admin
|
|
|
|
user.Avatar = in.Avatar
|
|
|
|
user.Active = true
|
|
|
|
user.Hash = crypto.Rand()
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
err = store.CreateUser(c, user)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
// swagger:route DELETE /users/{login} user deleteUserLogin
|
|
|
|
//
|
|
|
|
// Delete the user with the matching login.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: user
|
|
|
|
//
|
2015-04-08 22:43:59 +00:00
|
|
|
func DeleteUser(c *gin.Context) {
|
2015-10-21 23:14:02 +00:00
|
|
|
user, err := store.GetUserLogin(c, c.Param("login"))
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2016-03-30 20:15:28 +00:00
|
|
|
c.String(404, "Cannot find user. %s", err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2016-03-30 20:15:28 +00:00
|
|
|
if err = store.DeleteUser(c, user); err != nil {
|
|
|
|
c.String(500, "Error deleting user. %s", err)
|
|
|
|
} else {
|
|
|
|
c.String(200, "")
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|