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"
|
2022-12-18 11:51:20 +00:00
|
|
|
"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) {
|
2022-11-28 05:45:25 +00:00
|
|
|
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)
|
2022-12-18 11:51:20 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
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 == "" {
|
|
|
|
req.Password = user.Password
|
|
|
|
}
|
2022-08-07 03:59:33 +00:00
|
|
|
if req.OtpSecret == "" {
|
|
|
|
req.OtpSecret = user.OtpSecret
|
|
|
|
}
|
2023-02-04 03:44:17 +00:00
|
|
|
if req.Disabled && req.IsAdmin() {
|
|
|
|
common.ErrorStrResp(c, "admin user can not be disabled", 400)
|
|
|
|
return
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
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)
|
|
|
|
}
|