feat(permission): add i18n for permission action

This commit is contained in:
LinkinStars 2023-04-10 15:20:01 +08:00
parent 150432835b
commit e0fe3f29ed
7 changed files with 57 additions and 14 deletions

View File

@ -12,6 +12,17 @@ backend:
other: Unauthorized.
database_error:
other: Data server error.
action:
report:
other: Flag
edit:
other: Edit
delete:
other: Delete
close:
other: Close
reopen:
other: Reopen
role:
name:
user:

View File

@ -11,6 +11,17 @@ backend:
other: 未授权。
database_error:
other: 数据服务器错误。
action:
report:
other: 举报
edit:
other: 编辑
delete:
other: 删除
close:
other: 关闭
reopen:
other: 重新打开
role:
name:
user:

View File

@ -3,24 +3,27 @@ package permission
import (
"context"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/schema"
)
// GetAnswerPermission get answer permission
func GetAnswerPermission(ctx context.Context, userID string, creatorUserID string, canEdit, canDelete bool) (
actions []*schema.PermissionMemberAction) {
lang := handler.GetLangByCtx(ctx)
actions = make([]*schema.PermissionMemberAction, 0)
if len(userID) > 0 {
actions = append(actions, &schema.PermissionMemberAction{
Action: "report",
Name: "Flag",
Name: translator.Tr(lang, reportActionName),
Type: "reason",
})
}
if canEdit || userID == creatorUserID {
actions = append(actions, &schema.PermissionMemberAction{
Action: "edit",
Name: "Edit",
Name: translator.Tr(lang, editActionName),
Type: "edit",
})
}
@ -28,7 +31,7 @@ func GetAnswerPermission(ctx context.Context, userID string, creatorUserID strin
if canDelete || userID == creatorUserID {
actions = append(actions, &schema.PermissionMemberAction{
Action: "delete",
Name: "Delete",
Name: translator.Tr(lang, deleteActionName),
Type: "confirm",
})
}

View File

@ -5,17 +5,20 @@ import (
"time"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/schema"
)
// GetCommentPermission get comment permission
func GetCommentPermission(ctx context.Context, userID string, creatorUserID string,
createdAt time.Time, canEdit, canDelete bool) (actions []*schema.PermissionMemberAction) {
lang := handler.GetLangByCtx(ctx)
actions = make([]*schema.PermissionMemberAction, 0)
if len(userID) > 0 {
actions = append(actions, &schema.PermissionMemberAction{
Action: "report",
Name: "Flag",
Name: translator.Tr(lang, reportActionName),
Type: "reason",
})
}
@ -23,7 +26,7 @@ func GetCommentPermission(ctx context.Context, userID string, creatorUserID stri
if canEdit || (userID == creatorUserID && time.Now().Before(deadline)) {
actions = append(actions, &schema.PermissionMemberAction{
Action: "edit",
Name: "Edit",
Name: translator.Tr(lang, editActionName),
Type: "edit",
})
}
@ -31,7 +34,7 @@ func GetCommentPermission(ctx context.Context, userID string, creatorUserID stri
if canDelete || userID == creatorUserID {
actions = append(actions, &schema.PermissionMemberAction{
Action: "delete",
Name: "Delete",
Name: translator.Tr(lang, deleteActionName),
Type: "reason",
})
}

View File

@ -36,3 +36,11 @@ const (
TagAudit = "tag.audit"
TagUseReservedTag = "tag.use_reserved_tag"
)
const (
reportActionName = "action.report"
editActionName = "action.edit"
deleteActionName = "action.delete"
closeActionName = "action.close"
reopenActionName = "action.reopen"
)

View File

@ -3,6 +3,8 @@ package permission
import (
"context"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/schema"
)
@ -10,39 +12,40 @@ import (
func GetQuestionPermission(ctx context.Context, userID string, creatorUserID string,
canEdit, canDelete, canClose, canReopen bool) (
actions []*schema.PermissionMemberAction) {
lang := handler.GetLangByCtx(ctx)
actions = make([]*schema.PermissionMemberAction, 0)
if len(userID) > 0 {
actions = append(actions, &schema.PermissionMemberAction{
Action: "report",
Name: "Flag",
Name: translator.Tr(lang, reportActionName),
Type: "reason",
})
}
if canEdit || userID == creatorUserID {
actions = append(actions, &schema.PermissionMemberAction{
Action: "edit",
Name: "Edit",
Name: translator.Tr(lang, editActionName),
Type: "edit",
})
}
if canClose {
actions = append(actions, &schema.PermissionMemberAction{
Action: "close",
Name: "Close",
Name: translator.Tr(lang, closeActionName),
Type: "confirm",
})
}
if canReopen {
actions = append(actions, &schema.PermissionMemberAction{
Action: "reopen",
Name: "Reopen",
Name: translator.Tr(lang, reopenActionName),
Type: "confirm",
})
}
if canDelete || userID == creatorUserID {
actions = append(actions, &schema.PermissionMemberAction{
Action: "delete",
Name: "Delete",
Name: translator.Tr(lang, deleteActionName),
Type: "confirm",
})
}

View File

@ -3,17 +3,20 @@ package permission
import (
"context"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/schema"
)
// GetTagPermission get tag permission
func GetTagPermission(ctx context.Context, canEdit, canDelete bool) (
actions []*schema.PermissionMemberAction) {
lang := handler.GetLangByCtx(ctx)
actions = make([]*schema.PermissionMemberAction, 0)
if canEdit {
actions = append(actions, &schema.PermissionMemberAction{
Action: "edit",
Name: "Edit",
Name: translator.Tr(lang, editActionName),
Type: "edit",
})
}
@ -21,7 +24,7 @@ func GetTagPermission(ctx context.Context, canEdit, canDelete bool) (
if canDelete {
actions = append(actions, &schema.PermissionMemberAction{
Action: "delete",
Name: "Delete",
Name: translator.Tr(lang, deleteActionName),
Type: "reason",
})
}
@ -31,11 +34,12 @@ func GetTagPermission(ctx context.Context, canEdit, canDelete bool) (
// GetTagSynonymPermission get tag synonym permission
func GetTagSynonymPermission(ctx context.Context, canEdit bool) (
actions []*schema.PermissionMemberAction) {
lang := handler.GetLangByCtx(ctx)
actions = make([]*schema.PermissionMemberAction, 0)
if canEdit {
actions = append(actions, &schema.PermissionMemberAction{
Action: "edit",
Name: "Edit",
Name: translator.Tr(lang, editActionName),
Type: "edit",
})
}