feat: complete Kra admin backend migration
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
"kra/pkg/adminauth"
|
||||
@@ -30,6 +31,14 @@ func (s *SystemService) IsInitialized(ctx context.Context) (bool, error) {
|
||||
return s.uc.IsInitialized(ctx)
|
||||
}
|
||||
func (s *SystemService) PersistConfig(ctx context.Context) error { return s.uc.PersistConfig(ctx) }
|
||||
func (s *SystemService) PersistAdminConfig(ctx context.Context, value *conf.AdminBackend) error {
|
||||
raw, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.uc.PersistAdminConfig(ctx, raw)
|
||||
}
|
||||
func (s *SystemService) ReloadConfig(ctx context.Context) error { return s.uc.ReloadConfig(ctx) }
|
||||
|
||||
type DatabaseInit struct {
|
||||
DBType string `json:"dbType"`
|
||||
@@ -43,7 +52,7 @@ type DatabaseInit struct {
|
||||
AdminPassword string `json:"adminPassword"`
|
||||
}
|
||||
|
||||
func (s *SystemService) Initialize(ctx context.Context, input *DatabaseInit) error {
|
||||
func (s *SystemService) Initialize(ctx context.Context, input *DatabaseInit, apis []*biz.API) error {
|
||||
config := ""
|
||||
switch input.DBType {
|
||||
case "mysql":
|
||||
@@ -51,7 +60,7 @@ func (s *SystemService) Initialize(ctx context.Context, input *DatabaseInit) err
|
||||
case "pgsql":
|
||||
config = "sslmode=disable TimeZone=Local"
|
||||
}
|
||||
return s.uc.Initialize(ctx, &biz.DatabaseConfig{Driver: input.DBType, Host: input.Host, Port: input.Port, User: input.UserName, Password: input.Password, Name: input.DBName, Path: input.DBPath, Config: config, AdminPassword: input.AdminPassword})
|
||||
return s.uc.Initialize(ctx, &biz.DatabaseConfig{Driver: input.DBType, Host: input.Host, Port: input.Port, User: input.UserName, Password: input.Password, Name: input.DBName, Path: input.DBPath, Config: config, Template: input.Template, AdminPassword: input.AdminPassword, APIs: apis})
|
||||
}
|
||||
|
||||
func (s *SystemService) CacheGet(ctx context.Context, key string) (string, bool, error) {
|
||||
@@ -63,6 +72,9 @@ func (s *SystemService) CacheSet(ctx context.Context, key, value string, expirat
|
||||
func (s *SystemService) CacheDelete(ctx context.Context, key string) error {
|
||||
return s.uc.CacheDelete(ctx, key)
|
||||
}
|
||||
func (s *SystemService) CacheIncrement(ctx context.Context, key string, expiration time.Duration) (int64, error) {
|
||||
return s.uc.CacheIncrement(ctx, key, expiration)
|
||||
}
|
||||
|
||||
func (s *SystemService) Login(ctx context.Context, username, password string) (*LoginResult, error) {
|
||||
u, err := s.uc.Login(ctx, username, password)
|
||||
@@ -75,15 +87,18 @@ func (s *SystemService) Login(ctx context.Context, username, password string) (*
|
||||
if security, securityErr := s.settings.CurrentSecurity(ctx); securityErr == nil && security.PwdExpireEnable && u.PasswordUpdatedAt != nil && time.Since(*u.PasswordUpdatedAt) > time.Duration(security.PwdExpireDays)*24*time.Hour {
|
||||
u.MustChangePassword = true
|
||||
}
|
||||
expires := 7 * 24 * time.Hour
|
||||
expires, buffer := 7*24*time.Hour, 24*time.Hour
|
||||
secret, issuer := "", "kra"
|
||||
if s.config != nil && s.config.Jwt != nil {
|
||||
secret, issuer = s.config.Jwt.SigningKey, s.config.Jwt.Issuer
|
||||
if s.config.Jwt.ExpiresTime != nil {
|
||||
expires = s.config.Jwt.ExpiresTime.AsDuration()
|
||||
}
|
||||
if s.config.Jwt.BufferTime != nil {
|
||||
buffer = s.config.Jwt.BufferTime.AsDuration()
|
||||
}
|
||||
}
|
||||
token, claims, err := adminauth.Generate(secret, issuer, expires, u.ID, u.AuthorityID, u.UUID, u.Username, u.NickName, u.MustChangePassword)
|
||||
token, claims, err := adminauth.Generate(secret, issuer, expires, buffer, u.ID, u.AuthorityID, u.UUID, u.Username, u.NickName, u.MustChangePassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -212,15 +227,18 @@ func (s *SystemService) SwitchAuthority(ctx context.Context, id, authorityID uin
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expires := 7 * 24 * time.Hour
|
||||
expires, buffer := 7*24*time.Hour, 24*time.Hour
|
||||
secret, issuer := "", "kra"
|
||||
if s.config != nil && s.config.Jwt != nil {
|
||||
secret, issuer = s.config.Jwt.SigningKey, s.config.Jwt.Issuer
|
||||
if s.config.Jwt.ExpiresTime != nil {
|
||||
expires = s.config.Jwt.ExpiresTime.AsDuration()
|
||||
}
|
||||
if s.config.Jwt.BufferTime != nil {
|
||||
buffer = s.config.Jwt.BufferTime.AsDuration()
|
||||
}
|
||||
}
|
||||
token, claims, err := adminauth.Generate(secret, issuer, expires, u.ID, authorityID, u.UUID, u.Username, u.NickName, u.MustChangePassword)
|
||||
token, claims, err := adminauth.Generate(secret, issuer, expires, buffer, u.ID, authorityID, u.UUID, u.Username, u.NickName, u.MustChangePassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -260,8 +278,16 @@ func convertMenu(menu *biz.Menu) map[string]any {
|
||||
children = append(children, convertMenu(child))
|
||||
}
|
||||
buttons := make([]map[string]any, 0, len(menu.Buttons))
|
||||
buttonAuthorities := make(map[string]uint, len(menu.Buttons))
|
||||
for _, button := range menu.Buttons {
|
||||
buttons = append(buttons, map[string]any{"ID": button.ID, "name": button.Name, "desc": button.Description, "sysBaseMenuID": button.MenuID})
|
||||
if button.AuthorityID != 0 {
|
||||
buttonAuthorities[button.Name] = button.AuthorityID
|
||||
}
|
||||
}
|
||||
return map[string]any{"ID": menu.ID, "parentId": menu.ParentID, "path": menu.Path, "name": menu.Name, "hidden": menu.Hidden, "component": menu.Component, "sort": menu.Sort, "meta": map[string]any{"activeName": menu.ActiveName, "keepAlive": menu.KeepAlive, "defaultMenu": menu.DefaultMenu, "title": menu.Title, "icon": menu.Icon, "closeTab": menu.CloseTab, "transitionType": menu.TransitionType}, "children": children, "parameters": []any{}, "menuBtn": buttons}
|
||||
parameters := make([]map[string]any, 0, len(menu.Parameters))
|
||||
for _, parameter := range menu.Parameters {
|
||||
parameters = append(parameters, map[string]any{"ID": parameter.ID, "SysBaseMenuID": parameter.MenuID, "type": parameter.Type, "key": parameter.Key, "value": parameter.Value})
|
||||
}
|
||||
return map[string]any{"ID": menu.ID, "parentId": menu.ParentID, "path": menu.Path, "name": menu.Name, "hidden": menu.Hidden, "component": menu.Component, "sort": menu.Sort, "meta": map[string]any{"activeName": menu.ActiveName, "keepAlive": menu.KeepAlive, "defaultMenu": menu.DefaultMenu, "title": menu.Title, "icon": menu.Icon, "closeTab": menu.CloseTab, "transitionType": menu.TransitionType}, "children": children, "parameters": parameters, "menuBtn": buttons, "btns": buttonAuthorities}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user