From 1c235dbd9b86e73d40e9b5c197706d635de73aaa Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Tue, 11 Apr 2023 10:27:18 +0800 Subject: [PATCH] feat(permission): add privileges --- cmd/wire_gen.go | 2 +- internal/base/constant/rank.go | 73 +++++++++++++++++++ .../controller_admin/siteinfo_controller.go | 31 ++++++++ internal/router/answer_api_router.go | 2 + internal/schema/siteinfo_schema.go | 10 +++ internal/service/siteinfo/siteinfo_service.go | 35 ++++++++- 6 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 internal/base/constant/rank.go diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index db4eaa89..efacb2a0 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -193,7 +193,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, reasonService := reason2.NewReasonService(reasonRepo) reasonController := controller.NewReasonController(reasonService) themeController := controller_admin.NewThemeController() - siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService) + siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configRepo) siteInfoController := controller_admin.NewSiteInfoController(siteInfoService) siteinfoController := controller.NewSiteinfoController(siteInfoCommonService) notificationRepo := notification.NewNotificationRepo(dataData) diff --git a/internal/base/constant/rank.go b/internal/base/constant/rank.go new file mode 100644 index 00000000..44f7fba7 --- /dev/null +++ b/internal/base/constant/rank.go @@ -0,0 +1,73 @@ +package constant + +const ( + RankQuestionAddKey = "rank.question.add" + RankQuestionEditKey = "rank.question.edit" + RankQuestionDeleteKey = "rank.question.delete" + RankQuestionVoteUpKey = "rank.question.vote_up" + RankQuestionVoteDownKey = "rank.question.vote_down" + RankAnswerAddKey = "rank.answer.add" + RankAnswerEditKey = "rank.answer.edit" + RankAnswerDeleteKey = "rank.answer.delete" + RankAnswerAcceptKey = "rank.answer.accept" + RankAnswerVoteUpKey = "rank.answer.vote_up" + RankAnswerVoteDownKey = "rank.answer.vote_down" + RankCommentAddKey = "rank.comment.add" + RankCommentEditKey = "rank.comment.edit" + RankCommentDeleteKey = "rank.comment.delete" + RankReportAddKey = "rank.report.add" + RankTagAddKey = "rank.tag.add" + RankTagEditKey = "rank.tag.edit" + RankTagDeleteKey = "rank.tag.delete" + RankTagSynonymKey = "rank.tag.synonym" + RankLinkUrlLimitKey = "rank.link.url_limit" + RankVoteDetailKey = "rank.vote.detail" + RankCommentVoteUpKey = "rank.comment.vote_up" + RankCommentVoteDownKey = "rank.comment.vote_down" + RankQuestionEditWithoutReviewKey = "rank.question.edit_without_review" + RankAnswerEditWithoutReviewKey = "rank.answer.edit_without_review" + RankTagEditWithoutReviewKey = "rank.tag.edit_without_review" + RankAnswerAuditKey = "rank.answer.audit" + RankQuestionAuditKey = "rank.question.audit" + RankTagAuditKey = "rank.tag.audit" + RankQuestionCloseKey = "rank.question.close" + RankQuestionReopenKey = "rank.question.reopen" + RankTagUseReservedTagKey = "rank.tag.use_reserved_tag" +) + +var ( + RankAllKeys = []string{ + RankQuestionAddKey, + RankQuestionEditKey, + RankQuestionDeleteKey, + RankQuestionVoteUpKey, + RankQuestionVoteDownKey, + RankAnswerAddKey, + RankAnswerEditKey, + RankAnswerDeleteKey, + RankAnswerAcceptKey, + RankAnswerVoteUpKey, + RankAnswerVoteDownKey, + RankCommentAddKey, + RankCommentEditKey, + RankCommentDeleteKey, + RankReportAddKey, + RankTagAddKey, + RankTagEditKey, + RankTagDeleteKey, + RankTagSynonymKey, + RankLinkUrlLimitKey, + RankVoteDetailKey, + RankCommentVoteUpKey, + RankCommentVoteDownKey, + RankQuestionEditWithoutReviewKey, + RankAnswerEditWithoutReviewKey, + RankTagEditWithoutReviewKey, + RankAnswerAuditKey, + RankQuestionAuditKey, + RankTagAuditKey, + RankQuestionCloseKey, + RankQuestionReopenKey, + RankTagUseReservedTagKey, + } +) diff --git a/internal/controller_admin/siteinfo_controller.go b/internal/controller_admin/siteinfo_controller.go index d799fe5b..25bd9a0b 100644 --- a/internal/controller_admin/siteinfo_controller.go +++ b/internal/controller_admin/siteinfo_controller.go @@ -366,3 +366,34 @@ func (sc *SiteInfoController) UpdateSMTPConfig(ctx *gin.Context) { err := sc.siteInfoService.UpdateSMTPConfig(ctx, req) handler.HandleResponse(ctx, err, nil) } + +// GetPrivilegesConfig get privileges config +// @Summary GetPrivilegesConfig get privileges config +// @Description GetPrivilegesConfig get privileges config +// @Security ApiKeyAuth +// @Tags admin +// @Produce json +// @Success 200 {object} handler.RespBody{data=schema.GetPrivilegesConfigResp} +// @Router /answer/admin/api/setting/privileges [get] +func (sc *SiteInfoController) GetPrivilegesConfig(ctx *gin.Context) { + resp, err := sc.siteInfoService.GetPrivilegesConfig(ctx) + handler.HandleResponse(ctx, err, resp) +} + +// UpdatePrivilegesConfig update privileges config +// @Summary update privileges config +// @Description update privileges config +// @Security ApiKeyAuth +// @Tags admin +// @Produce json +// @Param data body schema.UpdatePrivilegesConfigReq true "smtp config" +// @Success 200 {object} handler.RespBody{} +// @Router /answer/admin/api/setting/smtp [put] +func (sc *SiteInfoController) UpdatePrivilegesConfig(ctx *gin.Context) { + req := &schema.UpdatePrivilegesConfigReq{} + if handler.BindAndCheck(ctx, req) { + return + } + err := sc.siteInfoService.UpdatePrivilegesConfig(ctx, req) + handler.HandleResponse(ctx, err, nil) +} diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go index 015bfcac..bcab270e 100644 --- a/internal/router/answer_api_router.go +++ b/internal/router/answer_api_router.go @@ -279,6 +279,8 @@ func (a *AnswerAPIRouter) RegisterAnswerAdminAPIRouter(r *gin.RouterGroup) { r.PUT("/siteinfo/seo", a.siteInfoController.UpdateSeo) r.GET("/setting/smtp", a.siteInfoController.GetSMTPConfig) r.PUT("/setting/smtp", a.siteInfoController.UpdateSMTPConfig) + r.GET("/setting/privileges", a.siteInfoController.GetPrivilegesConfig) + r.PUT("/setting/privileges", a.siteInfoController.UpdatePrivilegesConfig) // dashboard r.GET("/dashboard", a.dashboardController.DashboardInfo) diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go index 19f3d9fb..d1a6f784 100644 --- a/internal/schema/siteinfo_schema.go +++ b/internal/schema/siteinfo_schema.go @@ -233,3 +233,13 @@ type GetManifestJsonResp struct { ThemeColor string `json:"theme_color"` BackgroundColor string `json:"background_color"` } + +// GetPrivilegesConfigResp +type GetPrivilegesConfigResp struct { + Privileges map[string]int `json:"privileges"` +} + +// UpdatePrivilegesConfigReq +type UpdatePrivilegesConfigReq struct { + Privileges map[string]int `json:"privileges"` +} diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go index d41f4302..06f6205f 100644 --- a/internal/service/siteinfo/siteinfo_service.go +++ b/internal/service/siteinfo/siteinfo_service.go @@ -3,12 +3,14 @@ package siteinfo import ( "context" "encoding/json" + "fmt" "github.com/answerdev/answer/internal/base/constant" "github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/base/translator" "github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/schema" + "github.com/answerdev/answer/internal/service/config" "github.com/answerdev/answer/internal/service/export" "github.com/answerdev/answer/internal/service/siteinfo_common" tagcommon "github.com/answerdev/answer/internal/service/tag_common" @@ -23,26 +25,28 @@ type SiteInfoService struct { siteInfoCommonService *siteinfo_common.SiteInfoCommonService emailService *export.EmailService tagCommonService *tagcommon.TagCommonService + configRepo config.ConfigRepo } func NewSiteInfoService( siteInfoRepo siteinfo_common.SiteInfoRepo, siteInfoCommonService *siteinfo_common.SiteInfoCommonService, emailService *export.EmailService, - tagCommonService *tagcommon.TagCommonService) *SiteInfoService { - + tagCommonService *tagcommon.TagCommonService, + configRepo config.ConfigRepo, +) *SiteInfoService { resp, err := siteInfoCommonService.GetSiteInterface(context.Background()) if err != nil { log.Error(err) } else { constant.DefaultAvatar = resp.DefaultAvatar } - return &SiteInfoService{ siteInfoRepo: siteInfoRepo, siteInfoCommonService: siteInfoCommonService, emailService: emailService, tagCommonService: tagCommonService, + configRepo: configRepo, } } @@ -302,3 +306,28 @@ func (s *SiteInfoService) SaveSeo(ctx context.Context, req schema.SiteSeoReq) (e } return } + +func (s *SiteInfoService) GetPrivilegesConfig(ctx context.Context) (resp *schema.GetPrivilegesConfigResp, err error) { + resp = &schema.GetPrivilegesConfigResp{ + Privileges: make(map[string]int, 0), + } + for _, key := range constant.RankAllKeys { + v, err := s.configRepo.GetInt(key) + if err != nil { + log.Error(err) + continue + } + resp.Privileges[key] = v + } + return resp, nil +} + +func (s *SiteInfoService) UpdatePrivilegesConfig(ctx context.Context, req *schema.UpdatePrivilegesConfigReq) (err error) { + for key, value := range req.Privileges { + err = s.configRepo.SetConfig(key, fmt.Sprintf("%d", value)) + if err != nil { + return + } + } + return +}