Skip to content

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.

feat(init): set default privileges when init

LinkinStars964195fd859e
diff --git a/internal/migrations/init.go b/internal/migrations/init.go
index 19b03925..0dc3b35d 100644
--- a/internal/migrations/init.go
+++ b/internal/migrations/init.go
@@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
+ "github.com/answerdev/answer/internal/schema"
+ "github.com/segmentfault/pacman/log"
"github.com/answerdev/answer/internal/entity"
"golang.org/x/crypto/bcrypt"
@@ -38,6 +40,7 @@ func (m *Mentor) InitDB() error {
m.do("init version table", m.initVersionTable)
m.do("init admin user", m.initAdminUser)
m.do("init config", m.initConfig)
+ m.do("init default privileges config", m.initDefaultRankPrivileges)
m.do("init role", m.initRole)
m.do("init power", m.initPower)
m.do("init role power rel", m.initRolePowerRel)
@@ -48,6 +51,7 @@ func (m *Mentor) InitDB() error {
m.do("init site info theme config", m.initSiteInfoThemeConfig)
m.do("init site info seo config", m.initSiteInfoSEOConfig)
m.do("init site info user config", m.initSiteInfoUsersConfig)
+ m.do("init site info privilege rank", m.initSiteInfoPrivilegeRank)
return m.err
}
@@ -95,6 +99,19 @@ func (m *Mentor) initConfig() {
_, m.err = m.engine.Context(m.ctx).Insert(defaultConfigTable)
}
+func (m *Mentor) initDefaultRankPrivileges() {
+ chooseOption := schema.DefaultPrivilegeOptions.Choose(schema.PrivilegeLevel2)
+ for _, privilege := range chooseOption.Privileges {
+ _, err := m.engine.Context(m.ctx).Update(
+ &entity.Config{Value: fmt.Sprintf("%d", privilege.Value)},
+ &entity.Config{Key: privilege.Key},
+ )
+ if err != nil {
+ log.Error(err)
+ }
+ }
+}
+
func (m *Mentor) initRole() {
_, m.err = m.engine.Context(m.ctx).Insert(roles)
}
@@ -192,3 +209,15 @@ func (m *Mentor) initSiteInfoUsersConfig() {
Status: 1,
})
}
+
+func (m *Mentor) initSiteInfoPrivilegeRank() {
+ privilegeRankData := map[string]interface{}{
+ "level": schema.PrivilegeLevel2,
+ }
+ privilegeRankDataBytes, _ := json.Marshal(privilegeRankData)
+ _, m.err = m.engine.Context(m.ctx).Insert(&entity.SiteInfo{
+ Type: "privileges",
+ Content: string(privilegeRankDataBytes),
+ Status: 1,
+ })
+}
diff --git a/internal/migrations/init_data.go b/internal/migrations/init_data.go
index 623fa5d2..fc2d8aa8 100644
--- a/internal/migrations/init_data.go
+++ b/internal/migrations/init_data.go
@@ -232,7 +232,7 @@ var (
{ID: 48, Key: "rank.comment.edit", Value: `-1`},
{ID: 49, Key: "rank.comment.delete", Value: `-1`},
{ID: 50, Key: "rank.report.add", Value: `1`},
- {ID: 51, Key: "rank.tag.add", Value: `1`},
+ {ID: 51, Key: "rank.tag.add", Value: `1500`},
{ID: 52, Key: "rank.tag.edit", Value: `100`},
{ID: 53, Key: "rank.tag.delete", Value: `-1`},
{ID: 54, Key: "rank.tag.synonym", Value: `20000`},
diff --git a/internal/migrations/v3.go b/internal/migrations/v3.go
index 2d9b28eb..5b32bb81 100644
--- a/internal/migrations/v3.go
+++ b/internal/migrations/v3.go
@@ -117,7 +117,7 @@ ON "question" (
{ID: 48, Key: "rank.comment.edit", Value: `-1`},
{ID: 49, Key: "rank.comment.delete", Value: `-1`},
{ID: 50, Key: "rank.report.add", Value: `1`},
- {ID: 51, Key: "rank.tag.add", Value: `1`},
+ {ID: 51, Key: "rank.tag.add", Value: `1500`},
{ID: 52, Key: "rank.tag.edit", Value: `100`},
{ID: 53, Key: "rank.tag.delete", Value: `-1`},
{ID: 54, Key: "rank.tag.synonym", Value: `20000`},
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index bef9cf8e..8f54ab99 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -265,6 +265,16 @@ const (
)
type PrivilegeLevel int
+type PrivilegeOptions []*PrivilegeOption
+
+func (p PrivilegeOptions) Choose(level PrivilegeLevel) (option *PrivilegeOption) {
+ for _, op := range p {
+ if op.Level == level {
+ return op
+ }
+ }
+ return nil
+}
// GetPrivilegesConfigResp get privileges config response
type GetPrivilegesConfigResp struct {
@@ -285,7 +295,7 @@ type UpdatePrivilegesConfigReq struct {
}
var (
- DefaultPrivilegeOptions []*PrivilegeOption
+ DefaultPrivilegeOptions PrivilegeOptions
privilegeOptionsLevelMapping = map[string][]int{
constant.RankQuestionAddKey: {1, 1, 1},
constant.RankAnswerAddKey: {1, 1, 1},
diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go
index 70ddadba..daf20e65 100644
--- a/internal/service/siteinfo/siteinfo_service.go
+++ b/internal/service/siteinfo/siteinfo_service.go
@@ -322,13 +322,7 @@ func (s *SiteInfoService) translatePrivilegeOptions(ctx context.Context) (option
}
func (s *SiteInfoService) UpdatePrivilegesConfig(ctx context.Context, req *schema.UpdatePrivilegesConfigReq) (err error) {
- var chooseOption *schema.PrivilegeOption
- for _, option := range schema.DefaultPrivilegeOptions {
- if option.Level == req.Level {
- chooseOption = option
- break
- }
- }
+ chooseOption := schema.DefaultPrivilegeOptions.Choose(req.Level)
if chooseOption == nil {
return nil
}