This commit is contained in:
linkinstar 2022-11-23 08:38:26 +00:00
commit 8c1c1a664c
12 changed files with 200 additions and 130 deletions

View File

@ -26,6 +26,10 @@ backend:
answer:
not_found:
other: "Answer do not found."
cannot_deleted:
other: "No permission to delete."
cannot_update:
other: "No permission to update."
comment:
edit_without_permission:
other: "Comment are not allowed to edit."
@ -63,6 +67,12 @@ backend:
question:
not_found:
other: "Question not found."
cannot_deleted:
other: "No permission to delete."
cannot_close:
other: "No permission to close."
cannot_update:
other: "No permission to update."
rank:
fail_to_meet_the_condition:
other: "Rank fail to meet the condition."
@ -76,6 +86,8 @@ backend:
other: "Tag not found."
recommend_tag_not_found:
other: "Recommend Tag is not exist."
recommend_tag_enter:
other: "Please enter at least one required tag."
not_contain_synonym_tags:
other: "Should not contain synonym tags."
theme:
@ -94,7 +106,6 @@ backend:
other: "Username is already in use."
set_avatar:
other: "Avatar set failed."
config:
read_config_failed:
other: "Read config failed"

View File

@ -17,7 +17,12 @@ const (
EmailOrPasswordWrong = "error.object.email_or_password_incorrect"
CommentNotFound = "error.comment.not_found"
QuestionNotFound = "error.question.not_found"
QuestionCannotDeleted = "error.question.cannot_deleted"
QuestionCannotClose = "error.question.cannot_close"
QuestionCannotUpdate = "error.question.cannot_update"
AnswerNotFound = "error.answer.not_found"
AnswerCannotDeleted = "error.answer.cannot_deleted"
AnswerCannotUpdate = "error.answer.cannot_update"
CommentEditWithoutPermission = "error.comment.edit_without_permission"
DisallowVote = "error.object.disallow_vote"
DisallowFollow = "error.object.disallow_follow"

View File

@ -55,7 +55,8 @@ func (ac *AnswerController) RemoveAnswer(ctx *gin.Context) {
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
return
}
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
err := ac.answerService.RemoveAnswer(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
@ -147,6 +148,8 @@ func (ac *AnswerController) Update(ctx *gin.Context) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
if can, err := ac.rankService.CheckRankPermission(ctx, req.UserID, rank.AnswerEditRank); err != nil || !can {
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
@ -190,6 +193,8 @@ func (ac *AnswerController) AnswerList(ctx *gin.Context) {
return
}
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
list, count, err := ac.answerService.SearchList(ctx, req)
if err != nil {
handler.HandleResponse(ctx, err, nil)

View File

@ -43,9 +43,11 @@ func (qc *QuestionController) RemoveQuestion(ctx *gin.Context) {
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
if can, err := qc.rankService.CheckRankPermission(ctx, req.UserID, rank.QuestionDeleteRank); err != nil || !can {
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), errors.Forbidden(reason.RankFailToMeetTheCondition))
return
}
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
err := qc.questionService.RemoveQuestion(ctx, req)
handler.HandleResponse(ctx, err, nil)
@ -67,6 +69,8 @@ func (qc *QuestionController) CloseQuestion(ctx *gin.Context) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
err := qc.questionService.CloseQuestion(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
@ -85,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)
return
@ -213,7 +218,8 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
if can, err := qc.rankService.CheckRankPermission(ctx, req.UserID, rank.QuestionEditRank); err != nil || !can {
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
return

View File

@ -6,6 +6,7 @@ type RemoveAnswerReq struct {
ID string `validate:"required" json:"id"`
// user id
UserID string `json:"-"`
IsAdmin bool `json:"-"`
}
const (
@ -28,6 +29,7 @@ type AnswerUpdateReq struct {
Content string `json:"content"` // content
HTML string `json:"html" ` // html
EditSummary string `validate:"omitempty" json:"edit_summary"` // edit_summary
IsAdmin bool `json:"-"`
}
type AnswerList struct {
@ -36,6 +38,7 @@ type AnswerList struct {
Page int `json:"page" form:"page"` // Query number of pages
PageSize int `json:"page_size" form:"page_size"` // Search page size
LoginUserID string `json:"-" `
IsAdmin bool `json:"-"`
}
type AnswerInfo struct {

View File

@ -5,6 +5,7 @@ type RemoveQuestionReq struct {
// question id
ID string `validate:"required" comment:"question id" json:"id"`
UserID string `json:"-" ` // user_id
IsAdmin bool `json:"-"`
}
type CloseQuestionReq struct {
@ -12,6 +13,7 @@ type CloseQuestionReq struct {
UserID string `json:"-" ` // user_id
CloseType int `json:"close_type" ` // close_type
CloseMsg string `json:"close_msg" ` // close_type
IsAdmin bool `json:"-"`
}
type CloseQuestionMeta struct {
@ -47,6 +49,7 @@ type QuestionUpdate struct {
EditSummary string `validate:"omitempty" json:"edit_summary"`
// user id
UserID string `json:"-"`
IsAdmin bool `json:"-"`
}
type QuestionBaseInfo struct {

View File

@ -73,27 +73,29 @@ func (as *AnswerService) RemoveAnswer(ctx context.Context, req *schema.RemoveAns
if !exist {
return nil
}
if !req.IsAdmin {
if answerInfo.UserID != req.UserID {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
if answerInfo.VoteCount > 0 {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
if answerInfo.Adopted == schema.AnswerAdoptedEnable {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
if err != nil {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
if !exist {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
if questionInfo.AnswerCount > 1 {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
if questionInfo.AcceptedAnswerID != "" {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.AnswerCannotDeleted)
}
}
// user add question count
@ -180,6 +182,19 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
if !exist {
return "", errors.BadRequest(reason.QuestionNotFound)
}
if !req.IsAdmin {
answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.ID)
if err != nil {
return "", err
}
if !exist {
return "", nil
}
if answerInfo.UserID != req.UserID {
return "", errors.BadRequest(reason.AnswerCannotUpdate)
}
}
now := time.Now()
insertData := new(entity.Answer)
insertData.ID = req.ID
@ -377,14 +392,14 @@ func (as *AnswerService) SearchList(ctx context.Context, search *schema.AnswerLi
if 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)
@ -427,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, nil

View File

@ -121,7 +121,7 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment
resp = &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 info
if len(resp.ReplyUserID) > 0 {
@ -222,7 +222,7 @@ func (cs *CommentService) GetComment(ctx context.Context, req *schema.GetComment
// check if current user vote this comment
resp.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 comment
commentResp.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), nil

View File

@ -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,9 +54,10 @@ 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 !isAdmin {
if len(userID) > 0 {
actions = append(actions, &schema.PermissionMemberAction{
Action: "report",
@ -63,6 +68,7 @@ func GetAnswerPermission(userID string, answerAuthID string) (
if userID != answerAuthID {
return actions
}
}
actions = append(actions, []*schema.PermissionMemberAction{
{
Action: "edit",
@ -78,9 +84,9 @@ 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 !isAdmin {
if len(userID) > 0 {
actions = append(actions, &schema.PermissionMemberAction{
Action: "report",
@ -91,6 +97,7 @@ func GetQuestionPermission(userID string, questionAuthID string) (
if userID != questionAuthID {
return actions
}
}
actions = append(actions, []*schema.PermissionMemberAction{
{
Action: "edit",

View File

@ -71,6 +71,12 @@ func (qs *QuestionService) CloseQuestion(ctx context.Context, req *schema.CloseQ
if !has {
return nil
}
if !req.IsAdmin {
if questionInfo.UserID != req.UserID {
return errors.BadRequest(reason.QuestionCannotClose)
}
}
questionInfo.Status = entity.QuestionStatusclosed
err = qs.questionRepo.UpdateQuestionStatus(ctx, questionInfo)
if err != nil {
@ -161,7 +167,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
log.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
}
@ -174,15 +180,31 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
if !has {
return nil
}
if !req.IsAdmin {
if questionInfo.UserID != req.UserID {
return errors.BadRequest(reason.UnauthorizedError)
return errors.BadRequest(reason.QuestionCannotDeleted)
}
if questionInfo.AcceptedAnswerID != "" {
return errors.BadRequest(reason.UnauthorizedError)
if questionInfo.AcceptedAnswerID != "0" {
return errors.BadRequest(reason.QuestionCannotDeleted)
}
if questionInfo.AnswerCount > 1 {
return errors.BadRequest(reason.QuestionCannotDeleted)
}
if questionInfo.AnswerCount == 1 {
answersearch := &entity.AnswerSearch{}
answersearch.QuestionID = req.ID
answerList, _, err := qs.questioncommon.AnswerCommon.Search(ctx, answersearch)
if err != nil {
return err
}
for _, answer := range answerList {
if answer.VoteCount > 0 {
return errors.BadRequest(reason.QuestionCannotDeleted)
}
}
}
if questionInfo.AnswerCount > 0 {
return errors.BadRequest(reason.UnauthorizedError)
}
questionInfo.Status = entity.QuestionStatusDeleted
@ -223,8 +245,11 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
if !has {
return
}
if !req.IsAdmin {
if dbinfo.UserID != req.UserID {
return
return questionInfo, errors.BadRequest(reason.QuestionCannotUpdate)
}
}
//CheckChangeTag
@ -274,12 +299,12 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
return
}
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
@ -291,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
}
@ -518,7 +543,7 @@ func (qs *QuestionService) SearchByTitleLike(ctx context.Context, title string,
// SimilarQuestion
func (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
}

View File

@ -154,7 +154,7 @@ func (ts *TagService) GetTagInfo(ctx context.Context, req *schema.GetTagInfoReq)
resp.Recommend = tagInfo.Recommend
resp.Reserved = tagInfo.Reserved
resp.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
}

View File

@ -160,20 +160,10 @@ const routes: RouteNode[] = [
{
path: 'users/password-reset',
page: 'pages/Users/PasswordReset',
guard: async () => {
return guard.activated();
},
},
{
path: 'users/account-activation',
page: 'pages/Users/ActiveEmail',
// guard: async () => {
// const notActivated = guard.notActivated();
// if (notActivated.ok) {
// return notActivated;
// }
// return guard.notLogged();
// },
},
{
path: 'users/account-activation/success',