diff --git a/internal/service/comment.go b/internal/service/comment.go index 7688184..5dfb8d7 100644 --- a/internal/service/comment.go +++ b/internal/service/comment.go @@ -95,7 +95,15 @@ func (cs *CommentService) DeleteComment(ctx context.Context, commentID string) e return errs.New(errs.ErrNotFound.Code, "comment not found", err) } - if comment.UserID != currentUser.ID { + isTargetOwner := false + if comment.TargetType == constant.TargetTypePost { + post, err := repo.Post.GetPostByID(strconv.Itoa(int(comment.TargetID))) + if err == nil && post.UserID == currentUser.ID { + isTargetOwner = true + } + } + + if comment.UserID != currentUser.ID || isTargetOwner { return errs.ErrForbidden } diff --git a/pkg/constant/constant.go b/pkg/constant/constant.go index da77616..b97bf14 100644 --- a/pkg/constant/constant.go +++ b/pkg/constant/constant.go @@ -8,7 +8,8 @@ const ( ContextKeyUserID = "user_id" // 上下文键:用户ID ModeDev = "dev" ModeProd = "prod" - RoleUser = "user" + RoleUser = "user" // 普通用户 仅有阅读和评论权限 + RoleEditor = "editor" // 能够发布和管理自己内容的用户 RoleAdmin = "admin" EnvKeyBaseUrl = "BASE_URL" // 环境变量:基础URL EnvKeyCaptchaProvider = "CAPTCHA_PROVIDER" // captcha提供者