mirror of https://gitee.com/answerdev/answer.git
feat(init): set default privileges when init
This commit is contained in:
parent
b01d965f80
commit
964195fd85
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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`},
|
||||
|
|
|
@ -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`},
|
||||
|
|
|
@ -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},
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue