2022-07-11 09:12:50 +00:00
|
|
|
package handles
|
2022-06-26 11:36:27 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-28 10:12:53 +00:00
|
|
|
"strconv"
|
|
|
|
|
2022-06-26 11:36:27 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/db"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ListUsers(c *gin.Context) {
|
|
|
|
var req common.PageReq
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-12 10:41:16 +00:00
|
|
|
req.Validate()
|
2022-06-26 11:36:27 +00:00
|
|
|
log.Debugf("%+v", req)
|
|
|
|
users, total, err := db.GetUsers(req.PageIndex, req.PageSize)
|
|
|
|
if err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 500, true)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, common.PageResp{
|
|
|
|
Content: users,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateUser(c *gin.Context) {
|
|
|
|
var req model.User
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.IsAdmin() || req.IsGuest() {
|
|
|
|
common.ErrorStrResp(c, "admin or guest user can not be created", 400, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := db.CreateUser(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 500, true)
|
2022-06-26 11:36:27 +00:00
|
|
|
} else {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateUser(c *gin.Context) {
|
|
|
|
var req model.User
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := db.GetUserById(req.ID)
|
|
|
|
if err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 500)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if user.Role != req.Role {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorStrResp(c, "role can not be changed", 400)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-05 17:22:13 +00:00
|
|
|
if req.Password == "" {
|
|
|
|
req.Password = user.Password
|
|
|
|
}
|
2022-06-26 11:36:27 +00:00
|
|
|
if err := db.UpdateUser(&req); err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
} else {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(c *gin.Context) {
|
|
|
|
idStr := c.Query("id")
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 11:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := db.DeleteUserById(uint(id)); err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
2022-07-27 09:41:25 +00:00
|
|
|
|
|
|
|
func GetUser(c *gin.Context) {
|
|
|
|
idStr := c.Query("id")
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := db.GetUserById(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, user)
|
|
|
|
}
|