Feat: add unban api

pull/31/head
zijiren233 2 years ago
parent 7c5a7e88aa
commit 577490d92d

@ -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
}

@ -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"`

@ -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

@ -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)
}
{

@ -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,

Loading…
Cancel
Save