A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
update permission
aichy1266ec9dbec87da
diff --git a/internal/controller/answer_controller.go b/internal/controller/answer_controller.goindex ff0b3ff2..00f61280 100644--- a/internal/controller/answer_controller.go+++ b/internal/controller/answer_controller.go@@ -193,6 +193,8 @@ func (ac *AnswerController) AnswerList(ctx *gin.Context) {return}req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)+ userinfo := middleware.GetUserInfoFromContext(ctx)+ req.IsAdmin = userinfo.IsAdminlist, count, err := ac.answerService.SearchList(ctx, req)if err != nil {handler.HandleResponse(ctx, err, nil)diff --git a/internal/controller/question_controller.go b/internal/controller/question_controller.goindex 4d7e2366..bdb39b89 100644--- a/internal/controller/question_controller.go+++ b/internal/controller/question_controller.go@@ -89,7 +89,8 @@ func (qc *QuestionController) GetQuestion(c *gin.Context) {id := c.Query("id")ctx := context.Background()userID := middleware.GetLoginUserIDFromContext(c)- info, err := qc.questionService.GetQuestion(ctx, id, userID, true)+ userinfo := middleware.GetUserInfoFromContext(c)+ info, err := qc.questionService.GetQuestion(ctx, id, userID, true, userinfo.IsAdmin)if err != nil {handler.HandleResponse(c, err, nil)returndiff --git a/internal/schema/answer_schema.go b/internal/schema/answer_schema.goindex 390f90dc..fb523164 100644--- a/internal/schema/answer_schema.go+++ b/internal/schema/answer_schema.go@@ -38,6 +38,7 @@ type AnswerList struct {Page int `json:"page" form:"page"` // Query number of pagesPageSize int `json:"page_size" form:"page_size"` // Search page sizeLoginUserID string `json:"-" `+ IsAdmin bool `json:"-"`}type AnswerInfo struct {diff --git a/internal/service/answer_service.go b/internal/service/answer_service.goindex 3fd37a3d..56167a2b 100644--- a/internal/service/answer_service.go+++ b/internal/service/answer_service.go@@ -392,14 +392,14 @@ func (as *AnswerService) SearchList(ctx context.Context, search *schema.AnswerLiif err != nil {return list, count, err}- AnswerList, err := as.SearchFormatInfo(ctx, dblist, search.LoginUserID)+ AnswerList, err := as.SearchFormatInfo(ctx, dblist, search.LoginUserID, search.IsAdmin)if err != nil {return AnswerList, count, err}return AnswerList, count, nil}-func (as *AnswerService) SearchFormatInfo(ctx context.Context, dblist []*entity.Answer, loginUserID string) ([]*schema.AnswerInfo, error) {+func (as *AnswerService) SearchFormatInfo(ctx context.Context, dblist []*entity.Answer, loginUserID string, isAdmin bool) ([]*schema.AnswerInfo, error) {list := make([]*schema.AnswerInfo, 0)objectIds := make([]string, 0)userIds := make([]string, 0)@@ -442,7 +442,7 @@ func (as *AnswerService) SearchFormatInfo(ctx context.Context, dblist []*entity.}for _, item := range list {- item.MemberActions = permission.GetAnswerPermission(loginUserID, item.UserID)+ item.MemberActions = permission.GetAnswerPermission(ctx, loginUserID, item.UserID, isAdmin)}return list, nildiff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.goindex a7156c76..0d4235ab 100644--- a/internal/service/comment/comment_service.go+++ b/internal/service/comment/comment_service.go@@ -121,7 +121,7 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddCommentresp = &schema.GetCommentResp{}resp.SetFromComment(comment)- resp.MemberActions = permission.GetCommentPermission(req.UserID, resp.UserID)+ resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID)// get reply user infoif len(resp.ReplyUserID) > 0 {@@ -222,7 +222,7 @@ func (cs *CommentService) GetComment(ctx context.Context, req *schema.GetComment// check if current user vote this commentresp.IsVote = cs.checkIsVote(ctx, req.UserID, resp.CommentID)- resp.MemberActions = permission.GetCommentPermission(req.UserID, resp.UserID)+ resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID)return resp, nil}@@ -282,7 +282,7 @@ func (cs *CommentService) GetCommentWithPage(ctx context.Context, req *schema.Ge// check if current user vote this commentcommentResp.IsVote = cs.checkIsVote(ctx, req.UserID, commentResp.CommentID)- commentResp.MemberActions = permission.GetCommentPermission(req.UserID, commentResp.UserID)+ commentResp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, commentResp.UserID)resp = append(resp, commentResp)}return pager.NewPageModel(total, resp), nildiff --git a/internal/service/permission/comment_permission.go b/internal/service/permission/comment_permission.goindex ba04e0c3..b73dab9f 100644--- a/internal/service/permission/comment_permission.go+++ b/internal/service/permission/comment_permission.go@@ -1,9 +1,13 @@package permission-import "github.com/answerdev/answer/internal/schema"+import (+ "context"++ "github.com/answerdev/answer/internal/schema"+)// TODO: There is currently no permission management-func GetCommentPermission(userID string, commentCreatorUserID string) (+func GetCommentPermission(ctx context.Context, userID string, commentCreatorUserID string) (actions []*schema.PermissionMemberAction) {actions = make([]*schema.PermissionMemberAction, 0)if len(userID) > 0 {@@ -31,7 +35,7 @@ func GetCommentPermission(userID string, commentCreatorUserID string) (return actions}-func GetTagPermission(userID string, tagCreatorUserID string) (+func GetTagPermission(ctx context.Context, userID string, tagCreatorUserID string) (actions []*schema.PermissionMemberAction) {if userID != tagCreatorUserID {return []*schema.PermissionMemberAction{}@@ -50,18 +54,20 @@ func GetTagPermission(userID string, tagCreatorUserID string) (}}-func GetAnswerPermission(userID string, answerAuthID string) (+func GetAnswerPermission(ctx context.Context, userID string, answerAuthID string, isAdmin bool) (actions []*schema.PermissionMemberAction) {actions = make([]*schema.PermissionMemberAction, 0)- if len(userID) > 0 {- actions = append(actions, &schema.PermissionMemberAction{- Action: "report",- Name: "Flag",- Type: "reason",- })- }- if userID != answerAuthID {- return actions+ if !isAdmin {+ if len(userID) > 0 {+ actions = append(actions, &schema.PermissionMemberAction{+ Action: "report",+ Name: "Flag",+ Type: "reason",+ })+ }+ if userID != answerAuthID {+ return actions+ }}actions = append(actions, []*schema.PermissionMemberAction{{@@ -78,18 +84,19 @@ func GetAnswerPermission(userID string, answerAuthID string) (return actions}-func GetQuestionPermission(userID string, questionAuthID string) (- actions []*schema.PermissionMemberAction) {+func GetQuestionPermission(ctx context.Context, userID string, questionAuthID string, isAdmin bool) (actions []*schema.PermissionMemberAction) {actions = make([]*schema.PermissionMemberAction, 0)- if len(userID) > 0 {- actions = append(actions, &schema.PermissionMemberAction{- Action: "report",- Name: "Flag",- Type: "reason",- })- }- if userID != questionAuthID {- return actions+ if !isAdmin {+ if len(userID) > 0 {+ actions = append(actions, &schema.PermissionMemberAction{+ Action: "report",+ Name: "Flag",+ Type: "reason",+ })+ }+ if userID != questionAuthID {+ return actions+ }}actions = append(actions, []*schema.PermissionMemberAction{{diff --git a/internal/service/question_service.go b/internal/service/question_service.goindex 0409ff2d..f13dee2c 100644--- a/internal/service/question_service.go+++ b/internal/service/question_service.go@@ -167,7 +167,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Questionlog.Error("user IncreaseQuestionCount error", err.Error())}- questionInfo, err = qs.GetQuestion(ctx, question.ID, question.UserID, false)+ questionInfo, err = qs.GetQuestion(ctx, question.ID, question.UserID, false, false)return}@@ -299,12 +299,12 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Questreturn}- questionInfo, err = qs.GetQuestion(ctx, question.ID, question.UserID, false)+ questionInfo, err = qs.GetQuestion(ctx, question.ID, question.UserID, false, false)return}// GetQuestion get question one-func (qs *QuestionService) GetQuestion(ctx context.Context, id, loginUserID string, addpv bool) (resp *schema.QuestionInfo, err error) {+func (qs *QuestionService) GetQuestion(ctx context.Context, id, loginUserID string, addpv bool, isAdmin bool) (resp *schema.QuestionInfo, err error) {question, err := qs.questioncommon.Info(ctx, id, loginUserID)if err != nil {return@@ -316,7 +316,7 @@ func (qs *QuestionService) GetQuestion(ctx context.Context, id, loginUserID stri}}- question.MemberActions = permission.GetQuestionPermission(loginUserID, question.UserID)+ question.MemberActions = permission.GetQuestionPermission(ctx, loginUserID, question.UserID, isAdmin)return question, nil}@@ -543,7 +543,7 @@ func (qs *QuestionService) SearchByTitleLike(ctx context.Context, title string,// SimilarQuestionfunc (qs *QuestionService) SimilarQuestion(ctx context.Context, questionID string, loginUserID string) ([]*schema.QuestionInfo, int64, error) {list := make([]*schema.QuestionInfo, 0)- questionInfo, err := qs.GetQuestion(ctx, questionID, loginUserID, false)+ questionInfo, err := qs.GetQuestion(ctx, questionID, loginUserID, false, false)if err != nil {return list, 0, err}diff --git a/internal/service/tag/tag_service.go b/internal/service/tag/tag_service.goindex dcf02029..d7b5736e 100644--- a/internal/service/tag/tag_service.go+++ b/internal/service/tag/tag_service.go@@ -154,7 +154,7 @@ func (ts *TagService) GetTagInfo(ctx context.Context, req *schema.GetTagInfoReq)resp.Recommend = tagInfo.Recommendresp.Reserved = tagInfo.Reservedresp.IsFollower = ts.checkTagIsFollow(ctx, req.UserID, tagInfo.ID)- resp.MemberActions = permission.GetTagPermission(req.UserID, req.UserID)+ resp.MemberActions = permission.GetTagPermission(ctx, req.UserID, req.UserID)resp.GetExcerpt()return resp, nil}