alist/server/handles/user.go

139 lines
2.8 KiB
Go
Raw Normal View History

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/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-26 11:36:27 +00:00
"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 model.PageReq
2022-06-26 11:36:27 +00:00
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 := op.GetUsers(req.Page, req.PerPage)
2022-06-26 11:36:27 +00:00
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
}
2023-08-07 10:51:54 +00:00
req.SetPassword(req.Password)
req.Password = ""
if err := op.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 := op.GetUserById(req.ID)
2022-06-26 11:36:27 +00:00
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 == "" {
2023-08-07 10:51:54 +00:00
req.PwdHash = user.PwdHash
req.Salt = user.Salt
} else {
req.SetPassword(req.Password)
req.Password = ""
2022-08-05 17:22:13 +00:00
}
2022-08-07 03:59:33 +00:00
if req.OtpSecret == "" {
req.OtpSecret = user.OtpSecret
}
if req.Disabled && req.IsAdmin() {
common.ErrorStrResp(c, "admin user can not be disabled", 400)
return
}
if err := op.UpdateUser(&req); err != nil {
2022-06-26 11:36:27 +00:00
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 := op.DeleteUserById(uint(id)); err != nil {
2022-06-26 11:36:27 +00:00
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 := op.GetUserById(uint(id))
2022-07-27 09:41:25 +00:00
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
common.SuccessResp(c, user)
}
2022-08-07 03:59:33 +00:00
func Cancel2FAById(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := op.Cancel2FAById(uint(id)); err != nil {
2022-08-07 03:59:33 +00:00
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
func DelUserCache(c *gin.Context) {
username := c.Query("username")
err := op.DelUserCache(username)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}