From 577490d92d3d110e781d76a7ded8f4df2ada871a Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Sun, 12 Nov 2023 14:08:15 +0800 Subject: [PATCH] Feat: add unban api --- internal/model/user.go | 4 +++ internal/op/rooms.go | 1 + server/handlers/admin.go | 69 +++++++++++++++++++++++++++++++++------- server/handlers/init.go | 4 +++ server/handlers/room.go | 1 + 5 files changed, 67 insertions(+), 12 deletions(-) diff --git a/internal/model/user.go b/internal/model/user.go index b0f50e7..dd0c932 100644 --- a/internal/model/user.go +++ b/internal/model/user.go @@ -69,6 +69,10 @@ func (u *User) IsAdmin() bool { return u.Role == RoleAdmin || u.IsRoot() } +func (u *User) IsUser() bool { + return u.Role == RoleUser || u.IsAdmin() +} + func (u *User) IsPending() bool { return u.Role == RolePending } diff --git a/internal/op/rooms.go b/internal/op/rooms.go index 7e4da90..c798940 100644 --- a/internal/op/rooms.go +++ b/internal/op/rooms.go @@ -193,6 +193,7 @@ type RoomInfo struct { RoomName string `json:"roomName"` PeopleNum int64 `json:"peopleNum"` NeedPassword bool `json:"needPassword"` + CreatorID string `json:"creatorId"` Creator string `json:"creator"` CreatedAt int64 `json:"createdAt"` Status model.RoomStatus `json:"status"` diff --git a/server/handlers/admin.go b/server/handlers/admin.go index 93e11c1..9bfa1a4 100644 --- a/server/handlers/admin.go +++ b/server/handlers/admin.go @@ -156,7 +156,6 @@ func ApprovePendingUser(ctx *gin.Context) { 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)) @@ -169,16 +168,40 @@ func BanUser(ctx *gin.Context) { return } - if u.ID == user.ID { - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban yourself")) + if u.IsAdmin() && !user.IsRoot() { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban admin")) return } - if u.IsRoot() { - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban root user")) + + err = op.SetRoleByID(req.ID, dbModel.RoleBanned) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return } - err = op.SetRoleByID(req.ID, dbModel.RoleBanned) + ctx.Status(http.StatusNoContent) +} + +func UnBanUser(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 := db.GetUserByID(req.ID) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) + return + } + + if !u.IsBanned() { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("user is not banned")) + return + } + + err = op.SetRoleByID(req.ID, dbModel.RoleUser) if err != nil { ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return @@ -276,7 +299,6 @@ func ApprovePendingRoom(ctx *gin.Context) { func BanRoom(ctx *gin.Context) { user := ctx.MustGet("user").(*op.User) - req := model.RoomIDReq{} if err := model.Decode(ctx, &req); err != nil { ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) @@ -295,17 +317,40 @@ func BanRoom(ctx *gin.Context) { return } - if creator.ID == user.ID { - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban yourself")) + if creator.IsAdmin() && !user.IsRoot() { + ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("cannot ban admin")) return } - if creator.IsAdmin() { - ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("no permission")) + err = op.SetRoomStatus(req.Id, dbModel.RoomStatusBanned) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return } - err = op.SetRoomStatus(req.Id, dbModel.RoomStatusBanned) + ctx.Status(http.StatusNoContent) +} + +func UnBanRoom(ctx *gin.Context) { + // user := ctx.MustGet("user").(*op.User) + req := model.RoomIDReq{} + if err := model.Decode(ctx, &req); err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) + return + } + + r, err := db.GetRoomByID(req.Id) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) + return + } + + if !r.IsBanned() { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("room is not banned")) + return + } + + err = op.SetRoomStatus(req.Id, dbModel.RoomStatusActive) if err != nil { ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return diff --git a/server/handlers/init.go b/server/handlers/init.go index 7306797..6f26997 100644 --- a/server/handlers/init.go +++ b/server/handlers/init.go @@ -45,6 +45,10 @@ func Init(e *gin.Engine) { admin.POST("/ban/user", BanUser) admin.POST("/ban/room", BanRoom) + + admin.POST("/unban/user", UnBanUser) + + admin.POST("/unban/room", UnBanRoom) } { diff --git a/server/handlers/room.go b/server/handlers/room.go index 8f88828..87de822 100644 --- a/server/handlers/room.go +++ b/server/handlers/room.go @@ -145,6 +145,7 @@ func genRoomListResp(scopes ...func(db *gorm.DB) *gorm.DB) []*model.RoomListResp RoomName: r.Name, PeopleNum: op.ClientNum(r.ID), NeedPassword: len(r.HashedPassword) != 0, + CreatorID: r.CreatorID, Creator: op.GetUserName(r.CreatorID), CreatedAt: r.CreatedAt.UnixMilli(), Status: r.Status,