Skip to content

Commit

Permalink
Merge pull request #22 from DaydreamCafe/Dev
Browse files Browse the repository at this point in the history
🎨 用户权限相关BUG修复
  • Loading branch information
WhitePaper233 authored Aug 2, 2022
2 parents 70497c4 + 1406118 commit 018f38a
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions builtin/user_manager/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func handleUser(ctx *zero.Ctx) {

// 处理set命令
if set > 0 {
if ctx.Event.UserID == user {
ctx.SendChain(message.Text("你不能设置自己的权限等级"))
return
}
if control.CheckPremission(ctx.Event.UserID, 9) {
err := setPermissionLevel(user, set)
if err != nil {
Expand All @@ -97,6 +101,10 @@ func handleUser(ctx *zero.Ctx) {

// 处理unban命令
if pardon {
if ctx.Event.UserID == user {
ctx.SendChain(message.Text("你不能解除封禁自己"))
return
}
if control.CheckPremission(ctx.Event.UserID, 9) {
err := pardonUser(user)
if err != nil {
Expand All @@ -112,6 +120,10 @@ func handleUser(ctx *zero.Ctx) {

// 处理reset命令
if reset {
if ctx.Event.UserID == user {
ctx.SendChain(message.Text("你不能重置自己的权限等级"))
return
}
if control.CheckPremission(ctx.Event.UserID, 9) {
err := resetUser(user)
if err != nil {
Expand All @@ -126,13 +138,18 @@ func handleUser(ctx *zero.Ctx) {
}

// 处理ban命令
if ctx.Event.UserID == user {
ctx.SendChain(message.Text("你不能封禁自己"))
return
}
if control.CheckPremission(ctx.Event.UserID, 9) {
err := banUser(user, ban)
if err != nil {
ctx.SendChain(message.Text(err.Error()))
return
}
ctx.SendChain(message.Text(fmt.Sprintf("用户%d已被封禁", user)))
return
}
ctx.SendChain(message.Text("你没有权限执行这条指令"))
}
Expand Down Expand Up @@ -234,44 +251,18 @@ func banUser(QID int64, targetTime int64) error {
// 当有记录时, 查询是否有该用户的记录
if count > 0 {
err = db.Where("qid = ?", QID).First(&userPermission).Error
if err != nil {
if err != nil && err != gorm.ErrRecordNotFound {
logger.Errorln("封禁用户失败: ", err)
return errors.New("指令执行失败: 查询数据库失败")
}
if userPermission.IfSU {
return errors.New("指令执行失败: 您不能封禁超级用户")
}
}

// 查询表中是否有记录
err = db.Model(&model.UserPremissionModel{}).Count(&count).Error
if err != nil {
logger.Errorln("封禁用户失败: ", err)
return errors.New("指令执行失败: 查询数据库失败")
}

// 如果没有记录, 则创建一条记录
if count == 0 {
nowTime := time.Now().Unix()
var unbanTimeStamp int64
if targetTime == -1 {
unbanTimeStamp = -1
} else {
unbanTimeStamp = nowTime + targetTime
}
err = db.Create(&model.BanedUserModel{
QID: QID,
BanTimeStamp: nowTime,
UnbanTimeStamp: unbanTimeStamp,
}).Error
if err != nil {
logger.Errorln("封禁用户失败: ", err)
return errors.New("指令执行失败: 更新数据库失败")
// 查询是否为超级用户
if err != gorm.ErrRecordNotFound && userPermission.IfSU {
return errors.New("指令执行失败: 您不能封禁超级用户")
}
return nil
}

// 如果有记录, 则查询是否有该用户的记录
// 查询封禁表中是否有记录
var user model.BanedUserModel
err = db.Where("qid = ?", QID).First(&user).Error
if err != nil && err != gorm.ErrRecordNotFound {
Expand Down Expand Up @@ -341,20 +332,22 @@ func pardonUser(QID int64) error {
logger.Errorln("解封用户失败: ", err)
return errors.New("指令执行失败: 查询数据库失败")
}

// 当有记录时, 查询是否有该用户的记录
if count > 0 {
err = db.Where("qid = ?", QID).First(&userPermission).Error
if err != nil {
if err != nil && err != gorm.ErrRecordNotFound {
logger.Errorln("解封用户失败: ", err)
return errors.New("指令执行失败: 查询数据库失败")
}
if userPermission.IfSU {
if err != gorm.ErrRecordNotFound && userPermission.IfSU {
return errors.New("指令执行失败: 您不能解封超级用户")
}
}

// 查询表中是否有记录
err = db.Model(&model.BanedUserModel{}).Count(&count).Error
// 查询封禁表中是否有记录
var user model.BanedUserModel
err = db.Model(&user).Count(&count).Error
if err != nil {
logger.Errorln("解封用户失败: ", err)
return errors.New("指令执行失败: 查询数据库失败")
Expand All @@ -366,7 +359,6 @@ func pardonUser(QID int64) error {
}

// 如果有记录, 则查询是否有该用户的记录
var user model.BanedUserModel
err = db.Where("qid = ?", QID).First(&user).Error
if err != nil && err != gorm.ErrRecordNotFound {
logger.Errorln("解封用户失败: ", err)
Expand Down

0 comments on commit 018f38a

Please sign in to comment.