|
|
|
@ -1,11 +1,13 @@
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/synctv-org/synctv/internal/db"
|
|
|
|
|
dbModel "github.com/synctv-org/synctv/internal/model"
|
|
|
|
|
"github.com/synctv-org/synctv/internal/op"
|
|
|
|
|
"github.com/synctv-org/synctv/internal/settings"
|
|
|
|
|
"github.com/synctv-org/synctv/server/model"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
@ -104,7 +106,7 @@ func Users(ctx *gin.Context) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
|
|
|
|
"total": db.GetAllUserWithRoleUserCount(scopes...),
|
|
|
|
|
"total": db.GetAllUserCountWithRole(dbModel.RoleUser, scopes...),
|
|
|
|
|
"list": genUserListResp(dbModel.RoleUser, append(scopes, db.Paginate(page, pageSize))...),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
@ -122,3 +124,102 @@ func genUserListResp(role dbModel.Role, scopes ...func(db *gorm.DB) *gorm.DB) []
|
|
|
|
|
}
|
|
|
|
|
return resp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PendingUsers(ctx *gin.Context) {
|
|
|
|
|
// user := ctx.MustGet("user").(*op.User)
|
|
|
|
|
order := ctx.Query("order")
|
|
|
|
|
if order == "" {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("order is required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page, pageSize, err := GetPageAndPageSize(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var desc = ctx.DefaultQuery("sort", "desc") == "desc"
|
|
|
|
|
|
|
|
|
|
scopes := []func(db *gorm.DB) *gorm.DB{}
|
|
|
|
|
|
|
|
|
|
if keyword := ctx.Query("keyword"); keyword != "" {
|
|
|
|
|
scopes = append(scopes, db.WhereUserNameLike(keyword))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch order {
|
|
|
|
|
case "createdAt":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByCreatedAtDesc)
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByCreatedAtAsc)
|
|
|
|
|
}
|
|
|
|
|
case "name":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByDesc("username"))
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByAsc("username"))
|
|
|
|
|
}
|
|
|
|
|
case "id":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByIDDesc)
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByIDAsc)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
|
|
|
|
"total": db.GetAllUserCountWithRole(dbModel.RolePending, scopes...),
|
|
|
|
|
"list": genUserListResp(dbModel.RolePending, append(scopes, db.Paginate(page, pageSize))...),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ApprovePendingUser(Authorization string, userID uint) error {
|
|
|
|
|
user, err := op.GetUserById(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !user.IsPending() {
|
|
|
|
|
return errors.New("user is not pending")
|
|
|
|
|
}
|
|
|
|
|
if err := user.SetRole(dbModel.RoleUser); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BanUser(ctx *gin.Context) {
|
|
|
|
|
user := ctx.MustGet("user").(*op.User)
|
|
|
|
|
|
|
|
|
|
req := model.UserIDReq{}
|
|
|
|
|
if err := model.Decode(ctx, &req); err != nil {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u, err := op.GetUserById(req.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if u.ID == user.ID {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban yourself"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if u.IsRoot() {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban root user"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = u.SetRole(dbModel.RoleBanned)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
|
}
|
|
|
|
|