From 78cc596544e98a93e4d8bbf8b151a0696b65052b Mon Sep 17 00:00:00 2001 From: Snowykami Date: Sat, 13 Sep 2025 23:46:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E7=9B=AE=E6=A0=87=E5=86=85=E5=AE=B9=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=80=85=E7=9A=84=E6=9D=83=E9=99=90=E6=A3=80=E6=9F=A5=EF=BC=9B?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=96=E8=BE=91=E8=80=85=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/comment.go | 10 +++++++++- pkg/constant/constant.go | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) 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提供者