mirror of https://gitee.com/answerdev/answer.git
Merge branch 'feat/1.1.0/report' into test
This commit is contained in:
commit
60fb52c103
|
@ -211,7 +211,8 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
||||||
pluginConfigRepo := plugin_config.NewPluginConfigRepo(dataData)
|
pluginConfigRepo := plugin_config.NewPluginConfigRepo(dataData)
|
||||||
pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configRepo)
|
pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configRepo)
|
||||||
pluginController := controller_admin.NewPluginController(pluginCommonService)
|
pluginController := controller_admin.NewPluginController(pluginCommonService)
|
||||||
answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_adminReportController, userAdminController, reasonController, themeController, siteInfoController, siteinfoController, notificationController, dashboardController, uploadController, activityController, roleController, pluginController)
|
permissionController := controller.NewPermissionController(rankService)
|
||||||
|
answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_adminReportController, userAdminController, reasonController, themeController, siteInfoController, siteinfoController, notificationController, dashboardController, uploadController, activityController, roleController, pluginController, permissionController)
|
||||||
swaggerRouter := router.NewSwaggerRouter(swaggerConf)
|
swaggerRouter := router.NewSwaggerRouter(swaggerConf)
|
||||||
uiRouter := router.NewUIRouter(siteinfoController, siteInfoCommonService)
|
uiRouter := router.NewUIRouter(siteinfoController, siteInfoCommonService)
|
||||||
authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService)
|
authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService)
|
||||||
|
|
96
docs/docs.go
96
docs/docs.go
|
@ -3009,6 +3009,102 @@ const docTemplate = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/answer/api/v1/permission": {
|
||||||
|
"get": {
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"ApiKeyAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "check user permission",
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Permission"
|
||||||
|
],
|
||||||
|
"summary": "check user permission",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "access-token",
|
||||||
|
"name": "Authorization",
|
||||||
|
"in": "header",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"enum": [
|
||||||
|
"question.add",
|
||||||
|
"question.edit",
|
||||||
|
"question.edit_without_review",
|
||||||
|
"question.delete",
|
||||||
|
"question.close",
|
||||||
|
"question.reopen",
|
||||||
|
"question.vote_up",
|
||||||
|
"question.vote_down",
|
||||||
|
"question.pin",
|
||||||
|
"question.unpin",
|
||||||
|
"question.hide",
|
||||||
|
"question.show",
|
||||||
|
"answer.add",
|
||||||
|
"answer.edit",
|
||||||
|
"answer.edit_without_review",
|
||||||
|
"answer.delete",
|
||||||
|
"answer.accept",
|
||||||
|
"answer.vote_up",
|
||||||
|
"answer.vote_down",
|
||||||
|
"answer.invite_someone_to_answer",
|
||||||
|
"comment.add",
|
||||||
|
"comment.edit",
|
||||||
|
"comment.delete",
|
||||||
|
"comment.vote_up",
|
||||||
|
"comment.vote_down",
|
||||||
|
"report.add",
|
||||||
|
"tag.add",
|
||||||
|
"tag.edit",
|
||||||
|
"tag.edit_slug_name",
|
||||||
|
"tag.edit_without_review",
|
||||||
|
"tag.delete",
|
||||||
|
"tag.synonym",
|
||||||
|
"link.url_limit",
|
||||||
|
"vote.detail",
|
||||||
|
"answer.audit",
|
||||||
|
"question.audit",
|
||||||
|
"tag.audit",
|
||||||
|
"tag.use_reserved_tag"
|
||||||
|
],
|
||||||
|
"type": "string",
|
||||||
|
"description": "permission key",
|
||||||
|
"name": "action",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/handler.RespBody"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/answer/api/v1/personal/answer/page": {
|
"/answer/api/v1/personal/answer/page": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
|
|
|
@ -2997,6 +2997,102 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/answer/api/v1/permission": {
|
||||||
|
"get": {
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"ApiKeyAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "check user permission",
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Permission"
|
||||||
|
],
|
||||||
|
"summary": "check user permission",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "access-token",
|
||||||
|
"name": "Authorization",
|
||||||
|
"in": "header",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"enum": [
|
||||||
|
"question.add",
|
||||||
|
"question.edit",
|
||||||
|
"question.edit_without_review",
|
||||||
|
"question.delete",
|
||||||
|
"question.close",
|
||||||
|
"question.reopen",
|
||||||
|
"question.vote_up",
|
||||||
|
"question.vote_down",
|
||||||
|
"question.pin",
|
||||||
|
"question.unpin",
|
||||||
|
"question.hide",
|
||||||
|
"question.show",
|
||||||
|
"answer.add",
|
||||||
|
"answer.edit",
|
||||||
|
"answer.edit_without_review",
|
||||||
|
"answer.delete",
|
||||||
|
"answer.accept",
|
||||||
|
"answer.vote_up",
|
||||||
|
"answer.vote_down",
|
||||||
|
"answer.invite_someone_to_answer",
|
||||||
|
"comment.add",
|
||||||
|
"comment.edit",
|
||||||
|
"comment.delete",
|
||||||
|
"comment.vote_up",
|
||||||
|
"comment.vote_down",
|
||||||
|
"report.add",
|
||||||
|
"tag.add",
|
||||||
|
"tag.edit",
|
||||||
|
"tag.edit_slug_name",
|
||||||
|
"tag.edit_without_review",
|
||||||
|
"tag.delete",
|
||||||
|
"tag.synonym",
|
||||||
|
"link.url_limit",
|
||||||
|
"vote.detail",
|
||||||
|
"answer.audit",
|
||||||
|
"question.audit",
|
||||||
|
"tag.audit",
|
||||||
|
"tag.use_reserved_tag"
|
||||||
|
],
|
||||||
|
"type": "string",
|
||||||
|
"description": "permission key",
|
||||||
|
"name": "action",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/handler.RespBody"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/answer/api/v1/personal/answer/page": {
|
"/answer/api/v1/personal/answer/page": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
|
|
|
@ -4086,6 +4086,78 @@ paths:
|
||||||
summary: DelRedDot
|
summary: DelRedDot
|
||||||
tags:
|
tags:
|
||||||
- Notification
|
- Notification
|
||||||
|
/answer/api/v1/permission:
|
||||||
|
get:
|
||||||
|
description: check user permission
|
||||||
|
parameters:
|
||||||
|
- description: access-token
|
||||||
|
in: header
|
||||||
|
name: Authorization
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
- description: permission key
|
||||||
|
enum:
|
||||||
|
- question.add
|
||||||
|
- question.edit
|
||||||
|
- question.edit_without_review
|
||||||
|
- question.delete
|
||||||
|
- question.close
|
||||||
|
- question.reopen
|
||||||
|
- question.vote_up
|
||||||
|
- question.vote_down
|
||||||
|
- question.pin
|
||||||
|
- question.unpin
|
||||||
|
- question.hide
|
||||||
|
- question.show
|
||||||
|
- answer.add
|
||||||
|
- answer.edit
|
||||||
|
- answer.edit_without_review
|
||||||
|
- answer.delete
|
||||||
|
- answer.accept
|
||||||
|
- answer.vote_up
|
||||||
|
- answer.vote_down
|
||||||
|
- answer.invite_someone_to_answer
|
||||||
|
- comment.add
|
||||||
|
- comment.edit
|
||||||
|
- comment.delete
|
||||||
|
- comment.vote_up
|
||||||
|
- comment.vote_down
|
||||||
|
- report.add
|
||||||
|
- tag.add
|
||||||
|
- tag.edit
|
||||||
|
- tag.edit_slug_name
|
||||||
|
- tag.edit_without_review
|
||||||
|
- tag.delete
|
||||||
|
- tag.synonym
|
||||||
|
- link.url_limit
|
||||||
|
- vote.detail
|
||||||
|
- answer.audit
|
||||||
|
- question.audit
|
||||||
|
- tag.audit
|
||||||
|
- tag.use_reserved_tag
|
||||||
|
in: query
|
||||||
|
name: action
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: OK
|
||||||
|
schema:
|
||||||
|
allOf:
|
||||||
|
- $ref: '#/definitions/handler.RespBody'
|
||||||
|
- properties:
|
||||||
|
data:
|
||||||
|
additionalProperties:
|
||||||
|
type: boolean
|
||||||
|
type: object
|
||||||
|
type: object
|
||||||
|
security:
|
||||||
|
- ApiKeyAuth: []
|
||||||
|
summary: check user permission
|
||||||
|
tags:
|
||||||
|
- Permission
|
||||||
/answer/api/v1/personal/answer/page:
|
/answer/api/v1/personal/answer/page:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
|
|
@ -184,7 +184,9 @@ backend:
|
||||||
fail_to_meet_the_condition:
|
fail_to_meet_the_condition:
|
||||||
other: Rank fail to meet the condition.
|
other: Rank fail to meet the condition.
|
||||||
vote_fail_to_meet_the_condition:
|
vote_fail_to_meet_the_condition:
|
||||||
other: Thanks for the feedback. You need at least {{ rank }} reputation to cast a vote.
|
other: Thanks for the feedback. You need at least {{.Rank}} reputation to cast a vote.
|
||||||
|
no_enough_rank_to_operate:
|
||||||
|
other: You need at least {{.Rank}} reputation to do this.
|
||||||
report:
|
report:
|
||||||
handle_failed:
|
handle_failed:
|
||||||
other: Report handle failed.
|
other: Report handle failed.
|
||||||
|
|
|
@ -178,7 +178,9 @@ backend:
|
||||||
fail_to_meet_the_condition:
|
fail_to_meet_the_condition:
|
||||||
other: 级别不符合条件
|
other: 级别不符合条件
|
||||||
vote_fail_to_meet_the_condition:
|
vote_fail_to_meet_the_condition:
|
||||||
other: 感谢您的投票。您至少需要{{ rank }}声望才能投票。
|
other: 感谢您的投票。您至少需要{{.Rank}}声望才能投票。
|
||||||
|
no_enough_rank_to_operate:
|
||||||
|
other: 您至少需要{{.Rank}}声望才能执行此操作。
|
||||||
report:
|
report:
|
||||||
handle_failed:
|
handle_failed:
|
||||||
other: 报告处理失败
|
other: 报告处理失败
|
||||||
|
|
|
@ -52,6 +52,7 @@ const (
|
||||||
TagAlreadyExist = "error.tag.already_exist"
|
TagAlreadyExist = "error.tag.already_exist"
|
||||||
RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition"
|
RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition"
|
||||||
VoteRankFailToMeetTheCondition = "error.rank.vote_fail_to_meet_the_condition"
|
VoteRankFailToMeetTheCondition = "error.rank.vote_fail_to_meet_the_condition"
|
||||||
|
NoEnoughRankToOperate = "error.rank.no_enough_rank_to_operate"
|
||||||
ThemeNotFound = "error.theme.not_found"
|
ThemeNotFound = "error.theme.not_found"
|
||||||
LangNotFound = "error.lang.not_found"
|
LangNotFound = "error.lang.not_found"
|
||||||
ReportHandleFailed = "error.report.handle_failed"
|
ReportHandleFailed = "error.report.handle_failed"
|
||||||
|
|
|
@ -26,4 +26,5 @@ var ProviderSetController = wire.NewSet(
|
||||||
NewTemplateController,
|
NewTemplateController,
|
||||||
NewConnectorController,
|
NewConnectorController,
|
||||||
NewUserCenterController,
|
NewUserCenterController,
|
||||||
|
NewPermissionController,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
package controller
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/answerdev/answer/internal/base/handler"
|
||||||
|
"github.com/answerdev/answer/internal/base/middleware"
|
||||||
|
"github.com/answerdev/answer/internal/schema"
|
||||||
|
"github.com/answerdev/answer/internal/service/rank"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PermissionController struct {
|
||||||
|
rankService *rank.RankService
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPermissionController new language controller.
|
||||||
|
func NewPermissionController(rankService *rank.RankService) *PermissionController {
|
||||||
|
return &PermissionController{rankService: rankService}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPermission check user permission
|
||||||
|
// @Summary check user permission
|
||||||
|
// @Description check user permission
|
||||||
|
// @Tags Permission
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Param Authorization header string true "access-token"
|
||||||
|
// @Produce json
|
||||||
|
// @Param action query string true "permission key" Enums(question.add, question.edit, question.edit_without_review, question.delete, question.close, question.reopen, question.vote_up, question.vote_down, question.pin, question.unpin, question.hide, question.show, answer.add, answer.edit, answer.edit_without_review, answer.delete, answer.accept, answer.vote_up, answer.vote_down, answer.invite_someone_to_answer, comment.add, comment.edit, comment.delete, comment.vote_up, comment.vote_down, report.add, tag.add, tag.edit, tag.edit_slug_name, tag.edit_without_review, tag.delete, tag.synonym, link.url_limit, vote.detail, answer.audit, question.audit, tag.audit, tag.use_reserved_tag)
|
||||||
|
// @Success 200 {object} handler.RespBody{data=map[string]bool}
|
||||||
|
// @Router /answer/api/v1/permission [get]
|
||||||
|
func (u *PermissionController) GetPermission(ctx *gin.Context) {
|
||||||
|
req := &schema.GetPermissionReq{}
|
||||||
|
if handler.BindAndCheck(ctx, req) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID := middleware.GetLoginUserIDFromContext(ctx)
|
||||||
|
ops, requireRanks, err := u.rankService.CheckOperationPermissionsForRanks(ctx, userID, req.Actions)
|
||||||
|
if err != nil {
|
||||||
|
handler.HandleResponse(ctx, err, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lang := handler.GetLangByCtx(ctx)
|
||||||
|
mapping := make(map[string]*schema.GetPermissionResp, len(ops))
|
||||||
|
for i, action := range req.Actions {
|
||||||
|
t := &schema.GetPermissionResp{HasPermission: ops[i]}
|
||||||
|
t.TrTip(lang, requireRanks[i])
|
||||||
|
mapping[action] = t
|
||||||
|
}
|
||||||
|
handler.HandleResponse(ctx, err, mapping)
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/answerdev/answer/internal/base/middleware"
|
"github.com/answerdev/answer/internal/base/middleware"
|
||||||
"github.com/answerdev/answer/internal/base/pager"
|
"github.com/answerdev/answer/internal/base/pager"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
"github.com/answerdev/answer/internal/base/validator"
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
|
@ -306,13 +307,14 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||||
|
|
||||||
canList, err := qc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
canList, requireRanks, err := qc.rankService.CheckOperationPermissionsForRanks(ctx, req.UserID, []string{
|
||||||
permission.QuestionAdd,
|
permission.QuestionAdd,
|
||||||
permission.QuestionEdit,
|
permission.QuestionEdit,
|
||||||
permission.QuestionDelete,
|
permission.QuestionDelete,
|
||||||
permission.QuestionClose,
|
permission.QuestionClose,
|
||||||
permission.QuestionReopen,
|
permission.QuestionReopen,
|
||||||
permission.TagUseReservedTag,
|
permission.TagUseReservedTag,
|
||||||
|
permission.TagAdd,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handler.HandleResponse(ctx, err, nil)
|
handler.HandleResponse(ctx, err, nil)
|
||||||
|
@ -324,11 +326,25 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
|
||||||
req.CanClose = canList[3]
|
req.CanClose = canList[3]
|
||||||
req.CanReopen = canList[4]
|
req.CanReopen = canList[4]
|
||||||
req.CanUseReservedTag = canList[5]
|
req.CanUseReservedTag = canList[5]
|
||||||
|
req.CanAddTag = canList[6]
|
||||||
if !req.CanAdd {
|
if !req.CanAdd {
|
||||||
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// can add tag
|
||||||
|
hasNewTag, err := qc.questionService.HasNewTag(ctx, req.Tags)
|
||||||
|
if err != nil {
|
||||||
|
handler.HandleResponse(ctx, err, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !req.CanAddTag && hasNewTag {
|
||||||
|
lang := handler.GetLang(ctx)
|
||||||
|
msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: requireRanks[6]})
|
||||||
|
handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
errList, err := qc.questionService.CheckAddQuestion(ctx, req)
|
errList, err := qc.questionService.CheckAddQuestion(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errlist, ok := errList.([]*validator.FormErrorField)
|
errlist, ok := errList.([]*validator.FormErrorField)
|
||||||
|
@ -480,11 +496,12 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
|
||||||
req.ID = uid.DeShortID(req.ID)
|
req.ID = uid.DeShortID(req.ID)
|
||||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||||
|
|
||||||
canList, err := qc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
canList, requireRanks, err := qc.rankService.CheckOperationPermissionsForRanks(ctx, req.UserID, []string{
|
||||||
permission.QuestionEdit,
|
permission.QuestionEdit,
|
||||||
permission.QuestionDelete,
|
permission.QuestionDelete,
|
||||||
permission.QuestionEditWithoutReview,
|
permission.QuestionEditWithoutReview,
|
||||||
permission.TagUseReservedTag,
|
permission.TagUseReservedTag,
|
||||||
|
permission.TagAdd,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handler.HandleResponse(ctx, err, nil)
|
handler.HandleResponse(ctx, err, nil)
|
||||||
|
@ -496,6 +513,7 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
|
||||||
req.CanDelete = canList[1]
|
req.CanDelete = canList[1]
|
||||||
req.NoNeedReview = canList[2] || objectOwner
|
req.NoNeedReview = canList[2] || objectOwner
|
||||||
req.CanUseReservedTag = canList[3]
|
req.CanUseReservedTag = canList[3]
|
||||||
|
req.CanAddTag = canList[4]
|
||||||
if !req.CanEdit {
|
if !req.CanEdit {
|
||||||
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
||||||
return
|
return
|
||||||
|
@ -511,6 +529,19 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// can add tag
|
||||||
|
hasNewTag, err := qc.questionService.HasNewTag(ctx, req.Tags)
|
||||||
|
if err != nil {
|
||||||
|
handler.HandleResponse(ctx, err, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !req.CanAddTag && hasNewTag {
|
||||||
|
lang := handler.GetLang(ctx)
|
||||||
|
msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: requireRanks[4]})
|
||||||
|
handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := qc.questionService.UpdateQuestion(ctx, req)
|
resp, err := qc.questionService.UpdateQuestion(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handler.HandleResponse(ctx, err, resp)
|
handler.HandleResponse(ctx, err, resp)
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/handler"
|
"github.com/answerdev/answer/internal/base/handler"
|
||||||
"github.com/answerdev/answer/internal/base/middleware"
|
"github.com/answerdev/answer/internal/base/middleware"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
|
@ -44,16 +42,15 @@ func (vc *VoteController) VoteUp(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
req.ObjectID = uid.DeShortID(req.ObjectID)
|
req.ObjectID = uid.DeShortID(req.ObjectID)
|
||||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||||
can, rank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, true)
|
can, needRank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handler.HandleResponse(ctx, err, nil)
|
handler.HandleResponse(ctx, err, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !can {
|
if !can {
|
||||||
lang := handler.GetLang(ctx)
|
lang := handler.GetLang(ctx)
|
||||||
msg := translator.Tr(lang, reason.VoteRankFailToMeetTheCondition)
|
msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: needRank})
|
||||||
msg = handler.MsgWithParameter(msg, map[string]string{"rank": fmt.Sprintf("%d", rank)})
|
handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
|
||||||
handler.HandleResponse(ctx, errors.Forbidden(reason.VoteRankFailToMeetTheCondition).WithMsg(msg), nil)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,16 +81,15 @@ func (vc *VoteController) VoteDown(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
req.ObjectID = uid.DeShortID(req.ObjectID)
|
req.ObjectID = uid.DeShortID(req.ObjectID)
|
||||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||||
can, rank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, false)
|
can, needRank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handler.HandleResponse(ctx, err, nil)
|
handler.HandleResponse(ctx, err, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !can {
|
if !can {
|
||||||
lang := handler.GetLang(ctx)
|
lang := handler.GetLang(ctx)
|
||||||
msg := translator.Tr(lang, reason.VoteRankFailToMeetTheCondition)
|
msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: needRank})
|
||||||
msg = handler.MsgWithParameter(msg, map[string]string{"rank": fmt.Sprintf("%d", rank)})
|
handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
|
||||||
handler.HandleResponse(ctx, errors.Forbidden(reason.VoteRankFailToMeetTheCondition).WithMsg(msg), nil)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ type AnswerAPIRouter struct {
|
||||||
activityController *controller.ActivityController
|
activityController *controller.ActivityController
|
||||||
roleController *controller_admin.RoleController
|
roleController *controller_admin.RoleController
|
||||||
pluginController *controller_admin.PluginController
|
pluginController *controller_admin.PluginController
|
||||||
|
permissionController *controller.PermissionController
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAnswerAPIRouter(
|
func NewAnswerAPIRouter(
|
||||||
|
@ -61,6 +62,7 @@ func NewAnswerAPIRouter(
|
||||||
activityController *controller.ActivityController,
|
activityController *controller.ActivityController,
|
||||||
roleController *controller_admin.RoleController,
|
roleController *controller_admin.RoleController,
|
||||||
pluginController *controller_admin.PluginController,
|
pluginController *controller_admin.PluginController,
|
||||||
|
permissionController *controller.PermissionController,
|
||||||
) *AnswerAPIRouter {
|
) *AnswerAPIRouter {
|
||||||
return &AnswerAPIRouter{
|
return &AnswerAPIRouter{
|
||||||
langController: langController,
|
langController: langController,
|
||||||
|
@ -88,6 +90,7 @@ func NewAnswerAPIRouter(
|
||||||
activityController: activityController,
|
activityController: activityController,
|
||||||
roleController: roleController,
|
roleController: roleController,
|
||||||
pluginController: pluginController,
|
pluginController: pluginController,
|
||||||
|
permissionController: permissionController,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,6 +223,9 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
|
||||||
// reason
|
// reason
|
||||||
r.GET("/reasons", a.reasonController.Reasons)
|
r.GET("/reasons", a.reasonController.Reasons)
|
||||||
|
|
||||||
|
// permission
|
||||||
|
r.GET("/permission", a.permissionController.GetPermission)
|
||||||
|
|
||||||
// notification
|
// notification
|
||||||
r.GET("/notification/status", a.notificationController.GetRedDot)
|
r.GET("/notification/status", a.notificationController.GetRedDot)
|
||||||
r.PUT("/notification/status", a.notificationController.ClearRedDot)
|
r.PUT("/notification/status", a.notificationController.ClearRedDot)
|
||||||
|
|
|
@ -1,7 +1,18 @@
|
||||||
package schema
|
package schema
|
||||||
|
|
||||||
const PermissionMemberActionTypeEdit = "edit"
|
import (
|
||||||
const PermissionMemberActionTypeReason = "reason"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
|
"github.com/segmentfault/pacman/i18n"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PermissionTrTplData template data as for translate permission message
|
||||||
|
type PermissionTrTplData struct {
|
||||||
|
Rank int
|
||||||
|
}
|
||||||
|
|
||||||
// PermissionMemberAction permission member action
|
// PermissionMemberAction permission member action
|
||||||
type PermissionMemberAction struct {
|
type PermissionMemberAction struct {
|
||||||
|
@ -9,3 +20,35 @@ type PermissionMemberAction struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPermissionReq get permission request
|
||||||
|
type GetPermissionReq struct {
|
||||||
|
Action string `form:"action"`
|
||||||
|
Actions []string `validate:"omitempty" form:"actions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetPermissionReq) Check() (errField []*validator.FormErrorField, err error) {
|
||||||
|
if len(r.Action) > 0 {
|
||||||
|
r.Actions = strings.Split(r.Action, ",")
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPermissionResp get permission response
|
||||||
|
type GetPermissionResp struct {
|
||||||
|
HasPermission bool `json:"has_permission"`
|
||||||
|
// only not allow, will return this tip
|
||||||
|
NoPermissionTip string `json:"no_permission_tip"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetPermissionResp) TrTip(lang i18n.Language, requireRank int) {
|
||||||
|
if r.HasPermission {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if requireRank <= 0 {
|
||||||
|
r.NoPermissionTip = translator.Tr(lang, reason.RankFailToMeetTheCondition)
|
||||||
|
} else {
|
||||||
|
r.NoPermissionTip = translator.TrWithData(
|
||||||
|
lang, reason.NoEnoughRankToOperate, &PermissionTrTplData{Rank: requireRank})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -124,6 +124,7 @@ type QuestionPermission struct {
|
||||||
CanUseReservedTag bool `json:"-"`
|
CanUseReservedTag bool `json:"-"`
|
||||||
// whether user can invite other user to answer this question
|
// whether user can invite other user to answer this question
|
||||||
CanInviteOtherToAnswer bool `json:"-"`
|
CanInviteOtherToAnswer bool `json:"-"`
|
||||||
|
CanAddTag bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CheckCanQuestionUpdate struct {
|
type CheckCanQuestionUpdate struct {
|
||||||
|
|
|
@ -100,13 +100,15 @@ func (r *GetUserToSetShowResp) GetFromUserEntity(userInfo *entity.User) {
|
||||||
if ok {
|
if ok {
|
||||||
r.Status = statusShow
|
r.Status = statusShow
|
||||||
}
|
}
|
||||||
|
|
||||||
avatarInfo := &AvatarInfo{}
|
avatarInfo := &AvatarInfo{}
|
||||||
_ = json.Unmarshal([]byte(userInfo.Avatar), avatarInfo)
|
_ = json.Unmarshal([]byte(userInfo.Avatar), avatarInfo)
|
||||||
if constant.DefaultAvatar == "gravatar" && avatarInfo.Type == "" {
|
if len(avatarInfo.Type) == 0 && constant.DefaultAvatar == AvatarTypeGravatar {
|
||||||
avatarInfo.Type = "gravatar"
|
avatarInfo.Type = AvatarTypeGravatar
|
||||||
|
avatarInfo.Gravatar = gravatar.GetAvatarURL(userInfo.EMail)
|
||||||
|
} else if avatarInfo.Type == AvatarTypeGravatar {
|
||||||
avatarInfo.Gravatar = gravatar.GetAvatarURL(userInfo.EMail)
|
avatarInfo.Gravatar = gravatar.GetAvatarURL(userInfo.EMail)
|
||||||
}
|
}
|
||||||
// if json.Unmarshal Error avatarInfo.Type is Empty
|
|
||||||
r.Avatar = avatarInfo
|
r.Avatar = avatarInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +120,7 @@ const (
|
||||||
|
|
||||||
func FormatAvatarInfo(avatarJson, email string) (res string) {
|
func FormatAvatarInfo(avatarJson, email string) (res string) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if constant.DefaultAvatar == "gravatar" && len(res) == 0 {
|
if constant.DefaultAvatar == AvatarTypeGravatar && len(res) == 0 {
|
||||||
res = gravatar.GetAvatarURL(email)
|
res = gravatar.GetAvatarURL(email)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -133,7 +135,7 @@ func FormatAvatarInfo(avatarJson, email string) (res string) {
|
||||||
}
|
}
|
||||||
switch avatarInfo.Type {
|
switch avatarInfo.Type {
|
||||||
case AvatarTypeGravatar:
|
case AvatarTypeGravatar:
|
||||||
return avatarInfo.Gravatar
|
return gravatar.GetAvatarURL(email)
|
||||||
case AvatarTypeCustom:
|
case AvatarTypeCustom:
|
||||||
return avatarInfo.Custom
|
return avatarInfo.Custom
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -200,6 +200,11 @@ func (qs *QuestionService) CheckAddQuestion(ctx context.Context, req *schema.Que
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasNewTag
|
||||||
|
func (qs *QuestionService) HasNewTag(ctx context.Context, tags []*schema.TagItem) (bool, error) {
|
||||||
|
return qs.tagCommon.HasNewTag(ctx, tags)
|
||||||
|
}
|
||||||
|
|
||||||
// AddQuestion add question
|
// AddQuestion add question
|
||||||
func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd) (questionInfo any, err error) {
|
func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd) (questionInfo any, err error) {
|
||||||
if len(req.Tags) == 0 {
|
if len(req.Tags) == 0 {
|
||||||
|
|
|
@ -95,21 +95,22 @@ func (rs *RankService) CheckOperationPermission(ctx context.Context, userID stri
|
||||||
return can, nil
|
return can, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckOperationPermissions verify that the user has permission
|
// CheckOperationPermissionsForRanks verify that the user has permission
|
||||||
func (rs *RankService) CheckOperationPermissions(ctx context.Context, userID string, actions []string) (
|
func (rs *RankService) CheckOperationPermissionsForRanks(ctx context.Context, userID string, actions []string) (
|
||||||
can []bool, err error) {
|
can []bool, requireRanks []int, err error) {
|
||||||
can = make([]bool, len(actions))
|
can = make([]bool, len(actions))
|
||||||
|
requireRanks = make([]int, len(actions))
|
||||||
if len(userID) == 0 {
|
if len(userID) == 0 {
|
||||||
return can, nil
|
return can, requireRanks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the rank of the current user
|
// get the rank of the current user
|
||||||
userInfo, exist, err := rs.userCommon.GetUserBasicInfoByID(ctx, userID)
|
userInfo, exist, err := rs.userCommon.GetUserBasicInfoByID(ctx, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return can, err
|
return can, requireRanks, err
|
||||||
}
|
}
|
||||||
if !exist {
|
if !exist {
|
||||||
return can, nil
|
return can, requireRanks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
powerMapping := rs.getUserPowerMapping(ctx, userID)
|
powerMapping := rs.getUserPowerMapping(ctx, userID)
|
||||||
|
@ -118,10 +119,18 @@ func (rs *RankService) CheckOperationPermissions(ctx context.Context, userID str
|
||||||
can[idx] = true
|
can[idx] = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
meetRank, _ := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
|
meetRank, requireRank := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
|
||||||
can[idx] = meetRank
|
can[idx] = meetRank
|
||||||
|
requireRanks[idx] = requireRank
|
||||||
}
|
}
|
||||||
return can, nil
|
return can, requireRanks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckOperationPermissions verify that the user has permission
|
||||||
|
func (rs *RankService) CheckOperationPermissions(ctx context.Context, userID string, actions []string) (
|
||||||
|
can []bool, err error) {
|
||||||
|
can, _, err = rs.CheckOperationPermissionsForRanks(ctx, userID, actions)
|
||||||
|
return can, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckOperationObjectOwner check operation object owner
|
// CheckOperationObjectOwner check operation object owner
|
||||||
|
@ -142,7 +151,7 @@ func (rs *RankService) CheckOperationObjectOwner(ctx context.Context, userID, ob
|
||||||
|
|
||||||
// CheckVotePermission verify that the user has vote permission
|
// CheckVotePermission verify that the user has vote permission
|
||||||
func (rs *RankService) CheckVotePermission(ctx context.Context, userID, objectID string, voteUp bool) (
|
func (rs *RankService) CheckVotePermission(ctx context.Context, userID, objectID string, voteUp bool) (
|
||||||
can bool, rank int, err error) {
|
can bool, needRank int, err error) {
|
||||||
if len(userID) == 0 || len(objectID) == 0 {
|
if len(userID) == 0 || len(objectID) == 0 {
|
||||||
return false, 0, nil
|
return false, 0, nil
|
||||||
}
|
}
|
||||||
|
@ -180,13 +189,12 @@ func (rs *RankService) CheckVotePermission(ctx context.Context, userID, objectID
|
||||||
action = permission.CommentVoteDown
|
action = permission.CommentVoteDown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
meetRank, rank := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
|
|
||||||
powerMapping := rs.getUserPowerMapping(ctx, userID)
|
powerMapping := rs.getUserPowerMapping(ctx, userID)
|
||||||
if powerMapping[action] {
|
if powerMapping[action] {
|
||||||
return true, rank, nil
|
return true, 0, nil
|
||||||
}
|
}
|
||||||
|
can, needRank = rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
|
||||||
return meetRank, rank, nil
|
return can, needRank, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getUserPowerMapping get user power mapping
|
// getUserPowerMapping get user power mapping
|
||||||
|
|
|
@ -247,6 +247,32 @@ func (ts *TagCommonService) ExistRecommend(ctx context.Context, tags []*schema.T
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ts *TagCommonService) HasNewTag(ctx context.Context, tags []*schema.TagItem) (bool, error) {
|
||||||
|
tagNames := make([]string, 0)
|
||||||
|
tagMap := make(map[string]bool)
|
||||||
|
for _, item := range tags {
|
||||||
|
item.SlugName = strings.ReplaceAll(item.SlugName, " ", "-")
|
||||||
|
tagNames = append(tagNames, item.SlugName)
|
||||||
|
tagMap[item.SlugName] = false
|
||||||
|
}
|
||||||
|
list, err := ts.GetTagListByNames(ctx, tagNames)
|
||||||
|
if err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
for _, item := range list {
|
||||||
|
_, ok := tagMap[item.SlugName]
|
||||||
|
if ok {
|
||||||
|
tagMap[item.SlugName] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, has := range tagMap {
|
||||||
|
if !has {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetObjectTag get object tag
|
// GetObjectTag get object tag
|
||||||
func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (objTags []*schema.TagResp, err error) {
|
func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (objTags []*schema.TagResp, err error) {
|
||||||
tagsInfoList, err := ts.GetObjectEntityTag(ctx, objectId)
|
tagsInfoList, err := ts.GetObjectEntityTag(ctx, objectId)
|
||||||
|
|
Loading…
Reference in New Issue