Skip to content

Commit 853b7a9

Browse files
committed
feat:支持初始化管理员帐户
1 parent 0b2bf2b commit 853b7a9

File tree

5 files changed

+502
-14
lines changed

5 files changed

+502
-14
lines changed

admin/maintain.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func (s *Server) HasMainUser(ctx context.Context) (*apisecurity.User, error) {
4747
log.Error("check hash main user", zap.Error(err), utils.RequestID(ctx))
4848
return nil, err
4949
}
50+
if mainUser == nil {
51+
return nil, nil
52+
}
5053
ret := mainUser.ToSpec()
5154
ret.AuthToken = wrapperspb.String("")
5255
return ret, nil

apiserver/httpserver/admin_access.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (h *HTTPServer) GetAdminAccessServer() *restful.WebService {
7070
ws.Route(docs.EnrichEnablePprofApiDocs(ws.POST("/pprof/enable").To(h.EnablePprof)))
7171
ws.Route(docs.EnrichGetServerFunctionsApiDocs(ws.GET("/server/functions").To(h.GetServerFunctions)))
7272
ws.Route(ws.GET("/mainuser/exist").To(h.HasMainUser))
73-
ws.Route(ws.GET("/mainuser/create").To(h.InitMainUser))
73+
ws.Route(ws.POST("/mainuser/create").To(h.InitMainUser))
7474
return ws
7575
}
7676

config/config_file.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ import (
3737

3838
// CreateConfigFile 创建配置文件
3939
func (s *Server) CreateConfigFile(ctx context.Context, req *apiconfig.ConfigFile) *apiconfig.ConfigResponse {
40+
savaData := model.ToConfigFileStore(req)
41+
if errResp := s.chains.BeforeCreateFile(ctx, savaData); errResp != nil {
42+
return errResp
43+
}
44+
4045
if rsp := s.prepareCreateConfigFile(ctx, req); rsp.Code.Value != api.ExecuteSuccess {
4146
return rsp
4247
}
@@ -50,7 +55,7 @@ func (s *Server) CreateConfigFile(ctx context.Context, req *apiconfig.ConfigFile
5055
_ = tx.Rollback()
5156
}()
5257

53-
resp := s.handleCreateConfigFile(ctx, tx, req)
58+
resp := s._handleCreateConfigFile(ctx, tx, savaData)
5459
if resp.GetCode().GetValue() != uint32(apimodel.Code_ExecuteSuccess) {
5560
return resp
5661
}
@@ -63,30 +68,37 @@ func (s *Server) CreateConfigFile(ctx context.Context, req *apiconfig.ConfigFile
6368
return resp
6469
}
6570

71+
// handleCreateConfigFile .
6672
func (s *Server) handleCreateConfigFile(ctx context.Context, tx store.Tx,
6773
req *apiconfig.ConfigFile) *apiconfig.ConfigResponse {
6874

69-
data, err := s.storage.GetConfigFileTx(tx, req.GetNamespace().GetValue(), req.GetGroup().GetValue(),
70-
req.GetName().GetValue())
75+
savaData := model.ToConfigFileStore(req)
76+
if errResp := s.chains.BeforeCreateFile(ctx, savaData); errResp != nil {
77+
return errResp
78+
}
79+
80+
return s._handleCreateConfigFile(ctx, tx, savaData)
81+
}
82+
83+
func (s *Server) _handleCreateConfigFile(ctx context.Context, tx store.Tx,
84+
saveData *model.ConfigFile) *apiconfig.ConfigResponse {
85+
86+
data, err := s.storage.GetConfigFileTx(tx, saveData.Namespace, saveData.Group,
87+
saveData.Name)
7188
if err != nil {
7289
log.Error("[Config][File] create config file when get save data.", utils.RequestID(ctx),
73-
utils.ZapNamespace(req.GetNamespace().GetValue()), utils.ZapGroup(req.GetGroup().GetValue()),
74-
utils.ZapFileName(req.GetName().GetValue()), zap.Error(err))
90+
utils.ZapNamespace(saveData.Namespace), utils.ZapGroup(saveData.Group),
91+
utils.ZapFileName(saveData.Name), zap.Error(err))
7592
return api.NewConfigResponse(commonstore.StoreCode2APICode(err))
7693
}
7794
if data != nil {
7895
return api.NewConfigResponse(apimodel.Code_ExistedResource)
7996
}
80-
81-
savaData := model.ToConfigFileStore(req)
82-
if errResp := s.chains.BeforeCreateFile(ctx, savaData); errResp != nil {
83-
return errResp
84-
}
8597
// 创建配置文件
86-
if err := s.storage.CreateConfigFileTx(tx, savaData); err != nil {
98+
if err := s.storage.CreateConfigFileTx(tx, saveData); err != nil {
8799
log.Error("[Config][File] create config file error.", utils.RequestID(ctx),
88-
utils.ZapNamespace(req.GetNamespace().GetValue()), utils.ZapGroup(req.GetGroup().GetValue()),
89-
utils.ZapFileName(req.GetName().GetValue()), zap.Error(err))
100+
utils.ZapNamespace(saveData.Namespace), utils.ZapGroup(saveData.Group),
101+
utils.ZapFileName(saveData.Name), zap.Error(err))
90102
return api.NewConfigResponse(commonstore.StoreCode2APICode(err))
91103
}
92104
return api.NewConfigResponse(apimodel.Code_ExecuteSuccess)

store/boltdb/load.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func (m *boltStore) loadByFile(loadFile string) error {
4444
}
4545
cf, err := os.Open(loadFile)
4646
if err != nil {
47+
// 如果文件不存在,则需要用户首次调用接口进行管理员帐户的初始化操作
48+
if errors.Is(err, os.ErrNotExist) {
49+
return nil
50+
}
4751
return err
4852
}
4953
defer cf.Close()

0 commit comments

Comments
 (0)