2015-09-30 03:21:17 +02:00
|
|
|
package controller
|
2015-04-09 00:43:59 +02:00
|
|
|
|
|
|
|
import (
|
2015-09-30 03:21:17 +02:00
|
|
|
"net/http"
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/drone/drone/router/middleware/context"
|
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/shared/crypto"
|
2015-04-09 00:43:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetUsers(c *gin.Context) {
|
2015-09-30 03:21:17 +02:00
|
|
|
db := context.Database(c)
|
|
|
|
users, err := model.GetUserList(db)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
c.IndentedJSON(http.StatusOK, users)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetUser(c *gin.Context) {
|
2015-09-30 03:21:17 +02:00
|
|
|
db := context.Database(c)
|
|
|
|
user, err := model.GetUserLogin(db, c.Param("login"))
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusOK, user)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
func PatchUser(c *gin.Context) {
|
|
|
|
me := session.User(c)
|
|
|
|
db := context.Database(c)
|
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
user, err := model.GetUserLogin(db, c.Param("login"))
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
user.Admin = in.Admin
|
|
|
|
user.Active = in.Active
|
|
|
|
|
|
|
|
// cannot update self
|
|
|
|
if me.ID == user.ID {
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = model.UpdateUser(db, user)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusOK, user)
|
|
|
|
}
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
func PostUser(c *gin.Context) {
|
|
|
|
db := context.Database(c)
|
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02: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()
|
|
|
|
|
|
|
|
err = model.CreateUser(db, user)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusOK, user)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(c *gin.Context) {
|
2015-09-30 03:21:17 +02:00
|
|
|
me := session.User(c)
|
|
|
|
db := context.Database(c)
|
|
|
|
|
|
|
|
user, err := model.GetUserLogin(db, c.Param("login"))
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
// cannot delete self
|
|
|
|
if me.ID == user.ID {
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
err = model.DeleteUser(db, user)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
c.Writer.WriteHeader(http.StatusNoContent)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|