This commit is contained in:
2026-08-15 02:35:23 +08:00
parent bd5dbf2522
commit a34133cce5
49 changed files with 929 additions and 504 deletions
+8 -4
View File
@@ -2,10 +2,14 @@ package data
import "kra/internal/biz"
type accessRepo struct{ data *Data }
type authorityAccessRepo struct{ data *Data }
type apiRepo struct{ data *Data }
type permissionRepo struct{ data *Data }
func NewAuthorityAccessRepo(data *Data) biz.AuthorityAccessRepo { return &accessRepo{data: data} }
func NewAuthorityAccessRepo(data *Data) biz.AuthorityAccessRepo {
return &authorityAccessRepo{data: data}
}
func NewAPIRepo(data *Data) biz.APIRepo { return &accessRepo{data: data} }
func NewAPIRepo(data *Data) biz.APIRepo { return &apiRepo{data: data} }
func NewPermissionRepo(data *Data) biz.PermissionRepo { return &accessRepo{data: data} }
func NewPermissionRepo(data *Data) biz.PermissionRepo { return &permissionRepo{data: data} }
+14 -14
View File
@@ -44,7 +44,7 @@ func (authorityAPIPO) TableName() string { return "sys_authority_apis" }
func apiFromPO(po apiPO) *biz.API {
return &biz.API{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Path: po.Path, Description: po.Description, APIGroup: po.APIGroup, Method: po.Method}
}
func (r *accessRepo) CreateAPI(ctx context.Context, v *biz.API) error {
func (r *apiRepo) CreateAPI(ctx context.Context, v *biz.API) error {
po := apiPO{Path: v.Path, Description: v.Description, APIGroup: v.APIGroup, Method: strings.ToUpper(v.Method)}
var count int64
if err := r.data.gormDB.WithContext(ctx).Model(&apiPO{}).Where("path = ? AND method = ?", po.Path, po.Method).Count(&count).Error; err != nil {
@@ -59,7 +59,7 @@ func (r *accessRepo) CreateAPI(ctx context.Context, v *biz.API) error {
v.ID, v.CreatedAt, v.UpdatedAt, v.Method = po.ID, po.CreatedAt, po.UpdatedAt, po.Method
return nil
}
func (r *accessRepo) UpdateAPI(ctx context.Context, v *biz.API) error {
func (r *apiRepo) UpdateAPI(ctx context.Context, v *biz.API) error {
db := r.data.gormDB.WithContext(ctx)
var old apiPO
if err := db.First(&old, v.ID).Error; err != nil {
@@ -77,7 +77,7 @@ func (r *accessRepo) UpdateAPI(ctx context.Context, v *biz.API) error {
}
return db.Model(&old).Updates(map[string]any{"path": v.Path, "description": v.Description, "api_group": v.APIGroup, "method": method}).Error
}
func (r *accessRepo) DeleteAPIs(ctx context.Context, ids []uint) error {
func (r *apiRepo) DeleteAPIs(ctx context.Context, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("api_id IN ?", ids).Delete(&authorityAPIPO{}).Error; err != nil {
return err
@@ -85,14 +85,14 @@ func (r *accessRepo) DeleteAPIs(ctx context.Context, ids []uint) error {
return tx.Delete(&apiPO{}, ids).Error
})
}
func (r *accessRepo) FindAPI(ctx context.Context, id uint) (*biz.API, error) {
func (r *apiRepo) FindAPI(ctx context.Context, id uint) (*biz.API, error) {
var po apiPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return apiFromPO(po), nil
}
func (r *accessRepo) ListAPIs(ctx context.Context, page, size int, q *biz.API) ([]*biz.API, int64, error) {
func (r *apiRepo) ListAPIs(ctx context.Context, page, size int, q *biz.API) ([]*biz.API, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&apiPO{})
if q != nil && q.StrictAll {
config := r.data.runtime.Admin()
@@ -149,7 +149,7 @@ func (r *accessRepo) ListAPIs(ctx context.Context, page, size int, q *biz.API) (
}
return out, total, nil
}
func (r *accessRepo) APIRoleIDs(ctx context.Context, path, method string) ([]uint, error) {
func (r *apiRepo) APIRoleIDs(ctx context.Context, path, method string) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Table("sys_authority_apis").
Joins("JOIN sys_apis ON sys_apis.id = sys_authority_apis.api_id").
@@ -157,7 +157,7 @@ func (r *accessRepo) APIRoleIDs(ctx context.Context, path, method string) ([]uin
Pluck("sys_authority_apis.authority_id", &ids).Error
return ids, err
}
func (r *accessRepo) SetAPIRoles(ctx context.Context, path, method string, ids []uint) error {
func (r *apiRepo) SetAPIRoles(ctx context.Context, path, method string, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var api apiPO
if err := tx.Where("path = ? AND method = ?", path, strings.ToUpper(method)).First(&api).Error; err != nil {
@@ -176,7 +176,7 @@ func (r *accessRepo) SetAPIRoles(ctx context.Context, path, method string, ids [
return nil
})
}
func (r *accessRepo) Authorize(ctx context.Context, aid uint, path, method string) (bool, error) {
func (r *apiRepo) Authorize(ctx context.Context, aid uint, path, method string) (bool, error) {
policies, err := r.PolicyPaths(ctx, aid)
if err != nil {
return false, err
@@ -204,7 +204,7 @@ m = r.sub == p.sub && keyMatch2(r.obj, p.obj) && regexMatch(r.act, p.act)`)
}
return enforcer.Enforce(subject, path, strings.ToUpper(method))
}
func (r *accessRepo) PolicyPaths(ctx context.Context, aid uint) ([]*biz.API, error) {
func (r *apiRepo) PolicyPaths(ctx context.Context, aid uint) ([]*biz.API, error) {
var pos []apiPO
err := r.data.gormDB.WithContext(ctx).Table("sys_apis").Joins("JOIN sys_authority_apis ON sys_authority_apis.api_id = sys_apis.id").Where("sys_authority_apis.authority_id = ? AND sys_apis.deleted_at IS NULL", aid).Order("sys_apis.path,sys_apis.method").Find(&pos).Error
if err != nil {
@@ -216,8 +216,8 @@ func (r *accessRepo) PolicyPaths(ctx context.Context, aid uint) ([]*biz.API, err
}
return out, nil
}
func (r *accessRepo) SetPolicyPaths(ctx context.Context, aid uint, paths []*biz.API) error {
if err := r.checkAuthorityIDAuth(ctx, aid); err != nil {
func (r *apiRepo) SetPolicyPaths(ctx context.Context, aid uint, paths []*biz.API) error {
if err := (&authorityAccessRepo{data: r.data}).checkAuthorityIDAuth(ctx, aid); err != nil {
return err
}
config := r.data.runtime.Admin()
@@ -267,7 +267,7 @@ func (r *accessRepo) SetPolicyPaths(ctx context.Context, aid uint, paths []*biz.
})
}
func (r *accessRepo) IgnoredAPIs(ctx context.Context) ([]*biz.API, error) {
func (r *apiRepo) IgnoredAPIs(ctx context.Context) ([]*biz.API, error) {
var pos []ignoredAPIPO
if err := r.data.gormDB.WithContext(ctx).Order("path,method").Find(&pos).Error; err != nil {
return nil, err
@@ -278,14 +278,14 @@ func (r *accessRepo) IgnoredAPIs(ctx context.Context) ([]*biz.API, error) {
}
return out, nil
}
func (r *accessRepo) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
func (r *apiRepo) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
po := ignoredAPIPO{Path: path, Method: strings.ToUpper(method)}
if ignored {
return r.data.gormDB.WithContext(ctx).FirstOrCreate(&po, po).Error
}
return r.data.gormDB.WithContext(ctx).Where("path = ? AND method = ?", po.Path, po.Method).Delete(&ignoredAPIPO{}).Error
}
func (r *accessRepo) ApplyAPISync(ctx context.Context, added, deleted []*biz.API) error {
func (r *apiRepo) ApplyAPISync(ctx context.Context, added, deleted []*biz.API) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
for _, item := range added {
po := apiPO{Path: item.Path, Method: strings.ToUpper(item.Method), Description: item.Description, APIGroup: item.APIGroup}
+8 -8
View File
@@ -35,7 +35,7 @@ type jwtBlacklistPO struct {
func (jwtBlacklistPO) TableName() string { return "jwt_blacklists" }
func (r *settingsRepo) UserHasAuthority(ctx context.Context, userID, authorityID uint) (*biz.User, bool, error) {
func (r *apiTokenRepo) UserHasAuthority(ctx context.Context, userID, authorityID uint) (*biz.User, bool, error) {
var po userPO
if err := r.data.gormDB.WithContext(ctx).First(&po, userID).Error; err != nil {
return nil, false, err
@@ -45,10 +45,10 @@ func (r *settingsRepo) UserHasAuthority(ctx context.Context, userID, authorityID
if err != nil {
return nil, false, err
}
user, err := (&systemRepo{data: r.data}).toBizUser(ctx, &po)
user, err := (&userRepo{data: r.data}).toBizUser(ctx, &po)
return user, count > 0 || po.AuthorityID == authorityID, err
}
func (r *settingsRepo) CreateAPIToken(ctx context.Context, v *biz.APIToken) error {
func (r *apiTokenRepo) CreateAPIToken(ctx context.Context, v *biz.APIToken) error {
po := apiTokenPO{UserID: v.UserID, AuthorityID: v.AuthorityID, Token: v.Token, Status: v.Status, ExpiresAt: v.ExpiresAt, Remark: v.Remark}
if err := r.data.gormDB.WithContext(ctx).Create(&po).Error; err != nil {
return err
@@ -57,7 +57,7 @@ func (r *settingsRepo) CreateAPIToken(ctx context.Context, v *biz.APIToken) erro
v.CreatedAt = po.CreatedAt
return nil
}
func (r *settingsRepo) ListAPITokens(ctx context.Context, page, size int, userID uint, status *bool) ([]*biz.APIToken, int64, error) {
func (r *apiTokenRepo) ListAPITokens(ctx context.Context, page, size int, userID uint, status *bool) ([]*biz.APIToken, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&apiTokenPO{})
if userID != 0 {
db = db.Where("user_id = ?", userID)
@@ -94,14 +94,14 @@ func (r *settingsRepo) ListAPITokens(ctx context.Context, page, size int, userID
}
return out, total, nil
}
func (r *settingsRepo) DisableAPIToken(ctx context.Context, id uint) (string, error) {
func (r *apiTokenRepo) DisableAPIToken(ctx context.Context, id uint) (string, error) {
var po apiTokenPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return "", err
}
return po.Token, r.data.gormDB.WithContext(ctx).Model(&po).Update("status", false).Error
}
func (r *settingsRepo) DisableAndBlacklistAPIToken(ctx context.Context, id uint) error {
func (r *apiTokenRepo) DisableAndBlacklistAPIToken(ctx context.Context, id uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var po apiTokenPO
if err := tx.First(&po, id).Error; err != nil {
@@ -114,13 +114,13 @@ func (r *settingsRepo) DisableAndBlacklistAPIToken(ctx context.Context, id uint)
return tx.Model(&po).Update("status", false).Error
})
}
func (r *settingsRepo) BlacklistToken(ctx context.Context, token string) error {
func (r *apiTokenRepo) BlacklistToken(ctx context.Context, token string) error {
if token == "" {
return errors.New("token is empty")
}
return r.data.gormDB.WithContext(ctx).Create(&jwtBlacklistPO{JWT: token}).Error
}
func (r *settingsRepo) IsTokenDisabled(ctx context.Context, token string) (bool, error) {
func (r *apiTokenRepo) IsTokenDisabled(ctx context.Context, token string) (bool, error) {
var blacklistCount int64
if err := r.data.gormDB.WithContext(ctx).Model(&jwtBlacklistPO{}).Where("jwt = ?", token).Count(&blacklistCount).Error; err != nil {
return false, err
+6 -4
View File
@@ -2,10 +2,12 @@ package data
import "kra/internal/biz"
type auditRepo struct{ data *Data }
type auditQueryRepo struct{ data *Data }
type auditRecorderRepo struct{ data *Data }
type logFileRepo struct{ data *Data }
func NewAuditRepo(data *Data) biz.AuditQueryRepo { return &auditRepo{data: data} }
func NewAuditRepo(data *Data) biz.AuditQueryRepo { return &auditQueryRepo{data: data} }
func NewAuditRecorderRepo(data *Data) biz.AuditRecordRepo { return &auditRepo{data: data} }
func NewAuditRecorderRepo(data *Data) biz.AuditRecordRepo { return &auditRecorderRepo{data: data} }
func NewLogFileRepo(data *Data) biz.LogFileRepo { return &auditRepo{data: data} }
func NewLogFileRepo(data *Data) biz.LogFileRepo { return &logFileRepo{data: data} }
+12 -12
View File
@@ -11,7 +11,7 @@ import (
"gorm.io/gorm"
)
func (r *accessRepo) strictAuthorityIDs(ctx context.Context, actorID uint) (map[uint]bool, error) {
func (r *authorityAccessRepo) strictAuthorityIDs(ctx context.Context, actorID uint) (map[uint]bool, error) {
allowed := make(map[uint]bool)
if actorID == 0 {
return allowed, nil
@@ -46,7 +46,7 @@ func (r *accessRepo) strictAuthorityIDs(ctx context.Context, actorID uint) (map[
return allowed, nil
}
func (r *accessRepo) checkAuthorityIDAuth(ctx context.Context, targetID uint) error {
func (r *authorityAccessRepo) checkAuthorityIDAuth(ctx context.Context, targetID uint) error {
config := r.data.runtime.Admin()
if config == nil || config.System == nil || !config.System.UseStrictAuth {
return nil
@@ -65,7 +65,7 @@ func (r *accessRepo) checkAuthorityIDAuth(ctx context.Context, targetID uint) er
return nil
}
func (r *accessRepo) CreateAuthority(ctx context.Context, value *biz.Authority) error {
func (r *authorityAccessRepo) CreateAuthority(ctx context.Context, value *biz.Authority) error {
config := r.data.runtime.Admin()
if config != nil && config.System != nil && config.System.UseStrictAuth && (value.ParentID == nil || *value.ParentID == 0) {
if actor, ok := biz.ActorFromContext(ctx); ok {
@@ -104,7 +104,7 @@ func (r *accessRepo) CreateAuthority(ctx context.Context, value *biz.Authority)
return nil
})
}
func (r *accessRepo) CopyAuthority(ctx context.Context, sourceID uint, value *biz.Authority) error {
func (r *authorityAccessRepo) CopyAuthority(ctx context.Context, sourceID uint, value *biz.Authority) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
config := r.data.runtime.Admin()
actor, hasActor := biz.ActorFromContext(ctx)
@@ -179,10 +179,10 @@ func (r *accessRepo) CopyAuthority(ctx context.Context, sourceID uint, value *bi
return nil
})
}
func (r *accessRepo) UpdateAuthority(ctx context.Context, value *biz.Authority) error {
func (r *authorityAccessRepo) UpdateAuthority(ctx context.Context, value *biz.Authority) error {
return r.data.gormDB.WithContext(ctx).Model(&authorityPO{}).Where("authority_id = ?", value.AuthorityID).Updates(map[string]any{"authority_name": value.AuthorityName, "parent_id": value.ParentID, "data_scope": value.DataScope, "default_router": value.DefaultRouter}).Error
}
func (r *accessRepo) DeleteAuthority(ctx context.Context, id uint) error {
func (r *authorityAccessRepo) DeleteAuthority(ctx context.Context, id uint) error {
if id == 888 {
return errors.New("不能删除超级管理员")
}
@@ -221,7 +221,7 @@ func (r *accessRepo) DeleteAuthority(ctx context.Context, id uint) error {
return tx.Delete(&authorityPO{}, "authority_id = ?", id).Error
})
}
func (r *accessRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, error) {
func (r *authorityAccessRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, error) {
var pos []authorityPO
if err := r.data.gormDB.WithContext(ctx).Order("authority_id").Find(&pos).Error; err != nil {
return nil, err
@@ -245,7 +245,7 @@ func (r *accessRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, err
}
return out, nil
}
func (r *accessRepo) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error {
func (r *authorityAccessRepo) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var oldIDs []uint
if err := tx.Model(&userAuthorityPO{}).Where("sys_authority_authority_id = ?", id).Pluck("sys_user_id", &oldIDs).Error; err != nil {
@@ -287,12 +287,12 @@ func (r *accessRepo) SetAuthorityUsers(ctx context.Context, id uint, ids []uint)
return nil
})
}
func (r *accessRepo) AuthorityUserIDs(ctx context.Context, id uint) ([]uint, error) {
func (r *authorityAccessRepo) AuthorityUserIDs(ctx context.Context, id uint) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Model(&userAuthorityPO{}).Where("sys_authority_authority_id = ?", id).Pluck("sys_user_id", &ids).Error
return ids, err
}
func (r *accessRepo) SetDataScope(ctx context.Context, id uint, scope int, deptIDs []uint) error {
func (r *authorityAccessRepo) SetDataScope(ctx context.Context, id uint, scope int, deptIDs []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&authorityPO{}).Where("authority_id = ?", id).Update("data_scope", scope).Error; err != nil {
return err
@@ -312,12 +312,12 @@ func (r *accessRepo) SetDataScope(ctx context.Context, id uint, scope int, deptI
return nil
})
}
func (r *accessRepo) DataScopeDepartmentIDs(ctx context.Context, id uint) ([]uint, error) {
func (r *authorityAccessRepo) DataScopeDepartmentIDs(ctx context.Context, id uint) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Model(&authorityDepartmentPO{}).Where("authority_id = ?", id).Pluck("department_id", &ids).Error
return ids, err
}
func (r *accessRepo) ResolveDataScope(ctx context.Context, authorityID, userID uint) (biz.DataScope, error) {
func (r *authorityAccessRepo) ResolveDataScope(ctx context.Context, authorityID, userID uint) (biz.DataScope, error) {
identity := biz.DataScope{UserID: userID, AuthorityID: authorityID}
var user userPO
_ = r.data.gormDB.WithContext(ctx).Select("id", "dept_id").First(&user, userID).Error
+1 -1
View File
@@ -13,7 +13,7 @@ import (
"kra/internal/conf"
)
var ProviderSet = wire.NewSet(NewData, NewUserRepo, NewInitializationRepo, NewAuthorityAccessRepo, NewAPIRepo, NewPermissionRepo, NewMenuRepo, NewDepartmentRepo, NewPositionRepo, NewDictionaryRepo, NewParameterRepo, NewAPITokenRepo, NewSecurityRepo, NewVersionRepo, NewExportRepo, NewAuditRepo, NewAuditRecorderRepo, NewLogFileRepo, NewTaskRepo, NewMediaRepo, NewAnnouncementRepo, NewEmailRepo, NewCache, NewFileStorage)
var ProviderSet = wire.NewSet(NewData, NewRuntimeSettings, NewTokenIssuer, NewUserRepo, NewInitializationRepo, NewAuthorityAccessRepo, NewAPIRepo, NewPermissionRepo, NewMenuRepo, NewDepartmentRepo, NewPositionRepo, NewDictionaryRepo, NewParameterRepo, NewAPITokenRepo, NewSecurityRepo, NewVersionRepo, NewExportRepo, NewAuditRepo, NewAuditRecorderRepo, NewLogFileRepo, NewTaskRepo, NewMediaRepo, NewAnnouncementRepo, NewEmailRepo, NewCache, NewFileStorage)
type Data struct {
initMu sync.Mutex
+3 -3
View File
@@ -22,13 +22,13 @@ type dataAccessLogPO struct {
func (dataAccessLogPO) TableName() string { return "sys_data_access_logs" }
func (r *auditRepo) RecordDataAccess(ctx context.Context, v *biz.DataAccessLog) error {
func (r *auditRecorderRepo) RecordDataAccess(ctx context.Context, v *biz.DataAccessLog) error {
return r.data.gormDB.WithContext(ctx).Create(&dataAccessLogPO{EventType: v.EventType, TargetTable: v.TargetTable, Operation: v.Operation, UserID: v.UserID, AuthorityID: v.AuthorityID, Scope: v.Scope, RequestID: v.RequestID, Method: v.Method, Path: v.Path, Detail: v.Detail}).Error
}
func dataAccessFromPO(v dataAccessLogPO) *biz.DataAccessLog {
return &biz.DataAccessLog{ID: v.ID, CreatedAt: v.CreatedAt, EventType: v.EventType, TargetTable: v.TargetTable, Operation: v.Operation, UserID: v.UserID, AuthorityID: v.AuthorityID, Scope: v.Scope, RequestID: v.RequestID, Method: v.Method, Path: v.Path, Detail: v.Detail}
}
func (r *auditRepo) ListDataAccess(ctx context.Context, page, size int, q *biz.DataAccessLog) ([]*biz.DataAccessLog, int64, error) {
func (r *auditQueryRepo) ListDataAccess(ctx context.Context, page, size int, q *biz.DataAccessLog) ([]*biz.DataAccessLog, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&dataAccessLogPO{})
if q != nil {
if q.EventType != "" {
@@ -55,6 +55,6 @@ func (r *auditRepo) ListDataAccess(ctx context.Context, page, size int, q *biz.D
}
return out, total, nil
}
func (r *auditRepo) DeleteDataAccess(ctx context.Context, ids []uint) error {
func (r *auditQueryRepo) DeleteDataAccess(ctx context.Context, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Delete(&dataAccessLogPO{}, ids).Error
}
+10 -10
View File
@@ -45,7 +45,7 @@ func deptFromPO(po departmentPO) *biz.Department {
return &biz.Department{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, ParentID: po.ParentID, Ancestors: po.Ancestors, Sort: po.Sort, LeaderID: po.LeaderID, Status: po.Status}
}
func (r *organizationRepo) attachDepartmentLeaders(ctx context.Context, departments []*biz.Department) error {
func (r *departmentRepo) attachDepartmentLeaders(ctx context.Context, departments []*biz.Department) error {
leaderIDs := make([]uint, 0, len(departments))
for _, department := range departments {
if department.LeaderID != 0 {
@@ -68,7 +68,7 @@ func (r *organizationRepo) attachDepartmentLeaders(ctx context.Context, departme
}
return nil
}
func (r *organizationRepo) CreateDepartment(ctx context.Context, v *biz.Department) error {
func (r *departmentRepo) CreateDepartment(ctx context.Context, v *biz.Department) error {
v.Ancestors = "0"
if v.ParentID != 0 {
var parent departmentPO
@@ -79,7 +79,7 @@ func (r *organizationRepo) CreateDepartment(ctx context.Context, v *biz.Departme
}
return r.data.gormDB.WithContext(ctx).Create(&departmentPO{Name: v.Name, ParentID: v.ParentID, Ancestors: v.Ancestors, Sort: v.Sort, LeaderID: v.LeaderID, Status: v.Status}).Error
}
func (r *organizationRepo) UpdateDepartment(ctx context.Context, v *biz.Department) error {
func (r *departmentRepo) UpdateDepartment(ctx context.Context, v *biz.Department) error {
if v.ParentID == v.ID {
return errors.New("父部门不能是自己")
}
@@ -93,7 +93,7 @@ func (r *organizationRepo) UpdateDepartment(ctx context.Context, v *biz.Departme
}
return r.data.gormDB.WithContext(ctx).Model(&departmentPO{}).Where("id = ?", v.ID).Updates(map[string]any{"name": v.Name, "parent_id": v.ParentID, "ancestors": v.Ancestors, "sort": v.Sort, "leader_id": v.LeaderID, "status": v.Status}).Error
}
func (r *organizationRepo) DeleteDepartment(ctx context.Context, id uint) error {
func (r *departmentRepo) DeleteDepartment(ctx context.Context, id uint) error {
if id == 0 {
return errors.New("部门ID不能为空")
}
@@ -118,7 +118,7 @@ func (r *organizationRepo) DeleteDepartment(ctx context.Context, id uint) error
}
return r.data.gormDB.WithContext(ctx).Delete(&departmentPO{}, id).Error
}
func (r *organizationRepo) FindDepartment(ctx context.Context, id uint) (*biz.Department, error) {
func (r *departmentRepo) FindDepartment(ctx context.Context, id uint) (*biz.Department, error) {
var po departmentPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
@@ -129,7 +129,7 @@ func (r *organizationRepo) FindDepartment(ctx context.Context, id uint) (*biz.De
}
return department, nil
}
func (r *organizationRepo) ListDepartments(ctx context.Context, name string) ([]*biz.Department, error) {
func (r *departmentRepo) ListDepartments(ctx context.Context, name string) ([]*biz.Department, error) {
var pos []departmentPO
db := r.data.gormDB.WithContext(ctx).Order("sort,id")
if name != "" {
@@ -162,15 +162,15 @@ func (r *organizationRepo) ListDepartments(ctx context.Context, name string) ([]
}
return roots, nil
}
func (r *organizationRepo) DepartmentUserIDs(ctx context.Context, id uint) ([]uint, error) {
func (r *departmentRepo) DepartmentUserIDs(ctx context.Context, id uint) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Model(&userDepartmentPO{}).Where("sys_department_id = ?", id).Pluck("sys_user_id", &ids).Error
return ids, err
}
func (r *organizationRepo) SetDepartmentUsers(ctx context.Context, id uint, ids []uint) error {
func (r *departmentRepo) SetDepartmentUsers(ctx context.Context, id uint, ids []uint) error {
return r.replaceUserDepartments(ctx, id, ids)
}
func (r *organizationRepo) replaceUserDepartments(ctx context.Context, id uint, ids []uint) error {
func (r *departmentRepo) replaceUserDepartments(ctx context.Context, id uint, ids []uint) error {
if id == 0 {
return errors.New("部门ID不能为空")
}
@@ -214,7 +214,7 @@ func (r *organizationRepo) replaceUserDepartments(ctx context.Context, id uint,
return nil
})
}
func (r *organizationRepo) SetUserDepartments(ctx context.Context, uid uint, ids []uint, primary uint) error {
func (r *departmentRepo) SetUserDepartments(ctx context.Context, uid uint, ids []uint, primary uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var user userPO
if err := tx.First(&user, uid).Error; err != nil {
+13 -13
View File
@@ -54,7 +54,7 @@ func parameterFromPO(po parameterPO) *biz.SystemParameter {
return &biz.SystemParameter{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, Key: po.Key, Value: po.Value, Desc: po.Desc}
}
func (r *settingsRepo) CreateDictionary(ctx context.Context, v *biz.Dictionary) error {
func (r *dictionaryRepo) CreateDictionary(ctx context.Context, v *biz.Dictionary) error {
po := dictionaryPO{Name: v.Name, Type: v.Type, Status: v.Status, Desc: v.Desc, ParentID: v.ParentID}
if err := r.data.gormDB.WithContext(ctx).Create(&po).Error; err != nil {
return err
@@ -63,7 +63,7 @@ func (r *settingsRepo) CreateDictionary(ctx context.Context, v *biz.Dictionary)
return nil
}
func (r *settingsRepo) ImportDictionary(ctx context.Context, dictionary *biz.Dictionary, details []*biz.DictionaryDetail) error {
func (r *dictionaryRepo) ImportDictionary(ctx context.Context, dictionary *biz.Dictionary, details []*biz.DictionaryDetail) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
po := dictionaryPO{Name: dictionary.Name, Type: dictionary.Type, Status: dictionary.Status, Desc: dictionary.Desc, ParentID: dictionary.ParentID}
if err := tx.Create(&po).Error; err != nil {
@@ -103,10 +103,10 @@ func (r *settingsRepo) ImportDictionary(ctx context.Context, dictionary *biz.Dic
return nil
})
}
func (r *settingsRepo) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
func (r *dictionaryRepo) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
return r.data.gormDB.WithContext(ctx).Model(&dictionaryPO{}).Where("id = ?", v.ID).Updates(map[string]any{"name": v.Name, "type": v.Type, "status": v.Status, "desc": v.Desc, "parent_id": v.ParentID}).Error
}
func (r *settingsRepo) DeleteDictionary(ctx context.Context, id uint) error {
func (r *dictionaryRepo) DeleteDictionary(ctx context.Context, id uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var count int64
if err := tx.Model(&dictionaryPO{}).Where("parent_id = ?", id).Count(&count).Error; err != nil {
@@ -121,7 +121,7 @@ func (r *settingsRepo) DeleteDictionary(ctx context.Context, id uint) error {
return tx.Delete(&dictionaryPO{}, id).Error
})
}
func (r *settingsRepo) FindDictionary(ctx context.Context, id uint, typ string, status *bool, details bool) (*biz.Dictionary, error) {
func (r *dictionaryRepo) FindDictionary(ctx context.Context, id uint, typ string, status *bool, details bool) (*biz.Dictionary, error) {
var po dictionaryPO
db := r.data.gormDB.WithContext(ctx)
active := true
@@ -148,7 +148,7 @@ func (r *settingsRepo) FindDictionary(ctx context.Context, id uint, typ string,
return out, err
}
func (r *settingsRepo) ExportDictionary(ctx context.Context, id uint) (*biz.Dictionary, error) {
func (r *dictionaryRepo) ExportDictionary(ctx context.Context, id uint) (*biz.Dictionary, error) {
var po dictionaryPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
@@ -161,7 +161,7 @@ func (r *settingsRepo) ExportDictionary(ctx context.Context, id uint) (*biz.Dict
value.Details = details
return value, nil
}
func (r *settingsRepo) ListDictionaries(ctx context.Context, page, size int, name, typ string, details bool) ([]*biz.Dictionary, int64, error) {
func (r *dictionaryRepo) ListDictionaries(ctx context.Context, page, size int, name, typ string, details bool) ([]*biz.Dictionary, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&dictionaryPO{})
if name != "" {
like := "%" + name + "%"
@@ -219,7 +219,7 @@ func (r *settingsRepo) ListDictionaries(ctx context.Context, page, size int, nam
return out, total, nil
}
func (r *settingsRepo) CreateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
func (r *dictionaryRepo) CreateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
po := dictionaryDetailPO{Label: v.Label, Value: v.Value, Extend: v.Extend, Status: v.Status, Sort: v.Sort, DictionaryID: v.DictionaryID, ParentID: v.ParentID}
po.Level = 0
po.Path = ""
@@ -239,7 +239,7 @@ func (r *settingsRepo) CreateDictionaryDetail(ctx context.Context, v *biz.Dictio
v.Path = po.Path
return nil
}
func (r *settingsRepo) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
func (r *dictionaryRepo) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
var po dictionaryDetailPO
if err := r.data.gormDB.WithContext(ctx).First(&po, v.ID).Error; err != nil {
return err
@@ -268,7 +268,7 @@ func (r *settingsRepo) UpdateDictionaryDetail(ctx context.Context, v *biz.Dictio
return tx.Model(&dictionaryDetailPO{}).Where("path = ? OR path LIKE ?", oldPrefix, oldPrefix+",%").Update("path", gorm.Expr("REPLACE(path, ?, ?)", oldPrefix, newPrefix)).Error
})
}
func (r *settingsRepo) DeleteDictionaryDetail(ctx context.Context, id uint) error {
func (r *dictionaryRepo) DeleteDictionaryDetail(ctx context.Context, id uint) error {
var count int64
if err := r.data.gormDB.WithContext(ctx).Model(&dictionaryDetailPO{}).Where("parent_id = ?", id).Count(&count).Error; err != nil {
return err
@@ -278,14 +278,14 @@ func (r *settingsRepo) DeleteDictionaryDetail(ctx context.Context, id uint) erro
}
return r.data.gormDB.WithContext(ctx).Delete(&dictionaryDetailPO{}, id).Error
}
func (r *settingsRepo) FindDictionaryDetail(ctx context.Context, id uint) (*biz.DictionaryDetail, error) {
func (r *dictionaryRepo) FindDictionaryDetail(ctx context.Context, id uint) (*biz.DictionaryDetail, error) {
var po dictionaryDetailPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return detailFromPO(po), nil
}
func (r *settingsRepo) ListDictionaryDetails(ctx context.Context, page, size int, filter biz.DictionaryDetailFilter) ([]*biz.DictionaryDetail, int64, error) {
func (r *dictionaryRepo) ListDictionaryDetails(ctx context.Context, page, size int, filter biz.DictionaryDetailFilter) ([]*biz.DictionaryDetail, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&dictionaryDetailPO{})
if filter.DictionaryID != 0 {
db = db.Where("sys_dictionary_id = ?", filter.DictionaryID)
@@ -319,7 +319,7 @@ func (r *settingsRepo) ListDictionaryDetails(ctx context.Context, page, size int
}
return out, total, nil
}
func (r *settingsRepo) DictionaryDetailTree(ctx context.Context, dictionaryID uint, typ string) ([]*biz.DictionaryDetail, error) {
func (r *dictionaryRepo) DictionaryDetailTree(ctx context.Context, dictionaryID uint, typ string) ([]*biz.DictionaryDetail, error) {
if dictionaryID == 0 {
active := true
dictionary, err := r.FindDictionary(ctx, 0, typ, &active, false)
+5 -5
View File
@@ -28,26 +28,26 @@ func (errorRecordPO) TableName() string { return "sys_error" }
func errorFromPO(v errorRecordPO) *biz.ErrorRecord {
return &biz.ErrorRecord{ID: v.ID, CreatedAt: v.CreatedAt, Form: v.Form, Info: v.Info, Level: v.Level, RequestID: v.RequestID, TraceID: v.TraceID, Solution: v.Solution, Status: v.Status}
}
func (r *auditRepo) CreateError(ctx context.Context, v *biz.ErrorRecord) error {
func (r *auditRecorderRepo) CreateError(ctx context.Context, v *biz.ErrorRecord) error {
if v.Status == "" {
v.Status = "未处理"
}
return r.data.gormDB.WithContext(ctx).Create(&errorRecordPO{Form: v.Form, Info: v.Info, Level: v.Level, RequestID: v.RequestID, TraceID: v.TraceID, Solution: v.Solution, Status: v.Status}).Error
}
func (r *auditRepo) UpdateError(ctx context.Context, v *biz.ErrorRecord) error {
func (r *auditQueryRepo) UpdateError(ctx context.Context, v *biz.ErrorRecord) error {
return r.data.gormDB.WithContext(ctx).Model(&errorRecordPO{}).Where("id = ?", v.ID).Updates(map[string]any{"form": v.Form, "info": v.Info, "level": v.Level, "solution": v.Solution, "status": v.Status}).Error
}
func (r *auditRepo) DeleteErrors(ctx context.Context, ids []uint) error {
func (r *auditQueryRepo) DeleteErrors(ctx context.Context, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Delete(&errorRecordPO{}, ids).Error
}
func (r *auditRepo) FindError(ctx context.Context, id uint) (*biz.ErrorRecord, error) {
func (r *auditQueryRepo) FindError(ctx context.Context, id uint) (*biz.ErrorRecord, error) {
var po errorRecordPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return errorFromPO(po), nil
}
func (r *auditRepo) ListErrors(ctx context.Context, page, size int, q *biz.ErrorRecord) ([]*biz.ErrorRecord, int64, error) {
func (r *auditQueryRepo) ListErrors(ctx context.Context, page, size int, q *biz.ErrorRecord) ([]*biz.ErrorRecord, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&errorRecordPO{})
if q != nil {
if len(q.CreatedAtRange) == 2 {
+4 -4
View File
@@ -14,7 +14,7 @@ import (
"kra/internal/biz"
)
func (r *auditRepo) logRoot() (string, error) {
func (r *logFileRepo) logRoot() (string, error) {
admin := r.data.runtime.Admin()
if admin == nil || admin.Zap == nil || strings.TrimSpace(admin.Zap.Director) == "" {
return "", biz.ErrLogRootUnavailable
@@ -35,7 +35,7 @@ func (r *auditRepo) logRoot() (string, error) {
}
return root, nil
}
func (r *auditRepo) LogDates(ctx context.Context, month string) ([]biz.LogDate, error) {
func (r *logFileRepo) LogDates(ctx context.Context, month string) ([]biz.LogDate, error) {
if parsed, err := time.Parse("2006-01", month); err != nil || parsed.Format("2006-01") != month {
return nil, biz.ErrInvalidLogMonth
}
@@ -75,7 +75,7 @@ func (r *auditRepo) LogDates(ctx context.Context, month string) ([]biz.LogDate,
sort.Slice(out, func(i, j int) bool { return out[i].Date < out[j].Date })
return out, nil
}
func (r *auditRepo) LogFiles(ctx context.Context, date string) ([]biz.LogFile, error) {
func (r *logFileRepo) LogFiles(ctx context.Context, date string) ([]biz.LogFile, error) {
if parsed, err := time.Parse("2006-01-02", date); err != nil || parsed.Format("2006-01-02") != date {
return nil, biz.ErrInvalidLogDate
}
@@ -115,7 +115,7 @@ func (r *auditRepo) LogFiles(ctx context.Context, date string) ([]biz.LogFile, e
sort.Slice(out, func(i, j int) bool { return out[i].Path < out[j].Path })
return out, err
}
func (r *auditRepo) LogContent(ctx context.Context, date, path string, cursor *int64) (*biz.LogContent, error) {
func (r *logFileRepo) LogContent(ctx context.Context, date, path string, cursor *int64) (*biz.LogContent, error) {
if parsed, err := time.Parse("2006-01-02", date); err != nil || parsed.Format("2006-01-02") != date {
return nil, biz.ErrInvalidLogDate
}
+4 -4
View File
@@ -22,13 +22,13 @@ type loginLogPO struct {
func (loginLogPO) TableName() string { return "sys_login_logs" }
func (r *auditRepo) RecordLogin(ctx context.Context, v *biz.LoginLog) error {
func (r *auditRecorderRepo) RecordLogin(ctx context.Context, v *biz.LoginLog) error {
return r.data.gormDB.WithContext(ctx).Create(&loginLogPO{Username: v.Username, IP: v.IP, Status: v.Status, ErrorMessage: v.ErrorMessage, Agent: v.Agent, UserID: v.UserID}).Error
}
func loginFromPO(v loginLogPO) *biz.LoginLog {
return &biz.LoginLog{ID: v.ID, CreatedAt: v.CreatedAt, Username: v.Username, IP: v.IP, Status: v.Status, ErrorMessage: v.ErrorMessage, Agent: v.Agent, UserID: v.UserID}
}
func (r *auditRepo) ListLogins(ctx context.Context, page, size int, q *biz.LoginLog) ([]*biz.LoginLog, int64, error) {
func (r *auditQueryRepo) ListLogins(ctx context.Context, page, size int, q *biz.LoginLog) ([]*biz.LoginLog, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&loginLogPO{})
if q != nil {
if q.Username != "" {
@@ -61,13 +61,13 @@ func (r *auditRepo) ListLogins(ctx context.Context, page, size int, q *biz.Login
}
return out, total, nil
}
func (r *auditRepo) FindLogin(ctx context.Context, id uint) (*biz.LoginLog, error) {
func (r *auditQueryRepo) FindLogin(ctx context.Context, id uint) (*biz.LoginLog, error) {
var po loginLogPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return loginFromPO(po), nil
}
func (r *auditRepo) DeleteLogins(ctx context.Context, ids []uint) error {
func (r *auditQueryRepo) DeleteLogins(ctx context.Context, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Delete(&loginLogPO{}, ids).Error
}
+1 -1
View File
@@ -242,7 +242,7 @@ func (r *menuRepo) List(ctx context.Context) ([]*biz.Menu, error) {
}
func (r *menuRepo) SetAuthorityMenus(ctx context.Context, id uint, ids []uint) error {
if err := (&accessRepo{data: r.data}).checkAuthorityIDAuth(ctx, id); err != nil {
if err := (&authorityAccessRepo{data: r.data}).checkAuthorityIDAuth(ctx, id); err != nil {
return err
}
config := r.data.runtime.Admin()
+4 -4
View File
@@ -29,13 +29,13 @@ type operationPO struct {
func (operationPO) TableName() string { return "sys_operation_records" }
func (r *auditRepo) RecordOperation(ctx context.Context, v *biz.OperationRecord) error {
func (r *auditRecorderRepo) RecordOperation(ctx context.Context, v *biz.OperationRecord) error {
return r.data.gormDB.WithContext(ctx).Create(&operationPO{IP: v.IP, Method: v.Method, Path: v.Path, Status: v.Status, LatencyMS: v.LatencyMS, Agent: v.Agent, ErrorMessage: v.ErrorMessage, Body: v.Body, Response: v.Response, UserID: v.UserID, RequestID: v.RequestID, TraceID: v.TraceID, DeviceID: v.DeviceID}).Error
}
func opFromPO(v operationPO) *biz.OperationRecord {
return &biz.OperationRecord{ID: v.ID, CreatedAt: v.CreatedAt, IP: v.IP, Method: v.Method, Path: v.Path, Status: v.Status, LatencyMS: v.LatencyMS, Agent: v.Agent, ErrorMessage: v.ErrorMessage, Body: v.Body, Response: v.Response, UserID: v.UserID, RequestID: v.RequestID, TraceID: v.TraceID, DeviceID: v.DeviceID}
}
func (r *auditRepo) ListOperations(ctx context.Context, page, size int, q *biz.OperationRecord) ([]*biz.OperationRecord, int64, error) {
func (r *auditQueryRepo) ListOperations(ctx context.Context, page, size int, q *biz.OperationRecord) ([]*biz.OperationRecord, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&operationPO{})
if q != nil {
if q.Path != "" {
@@ -90,13 +90,13 @@ func auditUsers(ctx context.Context, db *gorm.DB, ids []uint) map[uint]*biz.User
}
return result
}
func (r *auditRepo) FindOperation(ctx context.Context, id uint) (*biz.OperationRecord, error) {
func (r *auditQueryRepo) FindOperation(ctx context.Context, id uint) (*biz.OperationRecord, error) {
var po operationPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return opFromPO(po), nil
}
func (r *auditRepo) DeleteOperations(ctx context.Context, ids []uint) error {
func (r *auditQueryRepo) DeleteOperations(ctx context.Context, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Delete(&operationPO{}, ids).Error
}
+4 -3
View File
@@ -2,8 +2,9 @@ package data
import "kra/internal/biz"
type organizationRepo struct{ data *Data }
type departmentRepo struct{ data *Data }
type positionRepo struct{ data *Data }
func NewDepartmentRepo(data *Data) biz.DepartmentRepo { return &organizationRepo{data: data} }
func NewDepartmentRepo(data *Data) biz.DepartmentRepo { return &departmentRepo{data: data} }
func NewPositionRepo(data *Data) biz.PositionRepo { return &organizationRepo{data: data} }
func NewPositionRepo(data *Data) biz.PositionRepo { return &positionRepo{data: data} }
+5 -5
View File
@@ -23,7 +23,7 @@ type parameterPO struct {
func (parameterPO) TableName() string { return "sys_params" }
func (r *settingsRepo) CreateParameter(ctx context.Context, v *biz.SystemParameter) error {
func (r *parameterRepo) CreateParameter(ctx context.Context, v *biz.SystemParameter) error {
po := parameterPO{Name: v.Name, Key: v.Key, Value: v.Value, Desc: v.Desc}
if err := r.data.gormDB.WithContext(ctx).Create(&po).Error; err != nil {
return err
@@ -31,13 +31,13 @@ func (r *settingsRepo) CreateParameter(ctx context.Context, v *biz.SystemParamet
v.ID = po.ID
return nil
}
func (r *settingsRepo) UpdateParameter(ctx context.Context, v *biz.SystemParameter) error {
func (r *parameterRepo) UpdateParameter(ctx context.Context, v *biz.SystemParameter) error {
return r.data.gormDB.WithContext(ctx).Model(&parameterPO{}).Where("id = ?", v.ID).Updates(map[string]any{"name": v.Name, "key": v.Key, "value": v.Value, "desc": v.Desc}).Error
}
func (r *settingsRepo) DeleteParameters(ctx context.Context, ids []uint) error {
func (r *parameterRepo) DeleteParameters(ctx context.Context, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Delete(&parameterPO{}, ids).Error
}
func (r *settingsRepo) FindParameter(ctx context.Context, id uint, key string) (*biz.SystemParameter, error) {
func (r *parameterRepo) FindParameter(ctx context.Context, id uint, key string) (*biz.SystemParameter, error) {
var po parameterPO
db := r.data.gormDB.WithContext(ctx)
var err error
@@ -51,7 +51,7 @@ func (r *settingsRepo) FindParameter(ctx context.Context, id uint, key string) (
}
return parameterFromPO(po), nil
}
func (r *settingsRepo) ListParameters(ctx context.Context, page, size int, q *biz.SystemParameter) ([]*biz.SystemParameter, int64, error) {
func (r *parameterRepo) ListParameters(ctx context.Context, page, size int, q *biz.SystemParameter) ([]*biz.SystemParameter, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&parameterPO{})
if q != nil {
if q.StartCreatedAt != nil && q.EndCreatedAt != nil {
+6 -6
View File
@@ -25,7 +25,7 @@ type authorityButtonPO struct {
func (authorityButtonPO) TableName() string { return "sys_authority_btns" }
func (r *accessRepo) Buttons(ctx context.Context, menuID uint) ([]*biz.MenuButton, error) {
func (r *permissionRepo) Buttons(ctx context.Context, menuID uint) ([]*biz.MenuButton, error) {
var pos []menuButtonPO
if err := r.data.gormDB.WithContext(ctx).Where("sys_base_menu_id = ?", menuID).Find(&pos).Error; err != nil {
return nil, err
@@ -36,7 +36,7 @@ func (r *accessRepo) Buttons(ctx context.Context, menuID uint) ([]*biz.MenuButto
}
return out, nil
}
func (r *accessRepo) SetAuthorityButtons(ctx context.Context, aid uint, buttons map[uint][]uint) error {
func (r *permissionRepo) SetAuthorityButtons(ctx context.Context, aid uint, buttons map[uint][]uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("authority_id = ?", aid).Delete(&authorityButtonPO{}).Error; err != nil {
return err
@@ -53,12 +53,12 @@ func (r *accessRepo) SetAuthorityButtons(ctx context.Context, aid uint, buttons
return nil
})
}
func (r *accessRepo) SelectedButtons(ctx context.Context, aid, menuID uint) ([]uint, error) {
func (r *permissionRepo) SelectedButtons(ctx context.Context, aid, menuID uint) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Model(&authorityButtonPO{}).Where("authority_id = ? AND sys_menu_id = ?", aid, menuID).Pluck("sys_base_menu_btn_id", &ids).Error
return ids, err
}
func (r *accessRepo) SetSelectedButtons(ctx context.Context, aid, menuID uint, ids []uint) error {
func (r *permissionRepo) SetSelectedButtons(ctx context.Context, aid, menuID uint, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("authority_id = ? AND sys_menu_id = ?", aid, menuID).Delete(&authorityButtonPO{}).Error; err != nil {
return err
@@ -73,12 +73,12 @@ func (r *accessRepo) SetSelectedButtons(ctx context.Context, aid, menuID uint, i
return nil
})
}
func (r *accessRepo) CanRemoveButton(ctx context.Context, id uint) (bool, error) {
func (r *permissionRepo) CanRemoveButton(ctx context.Context, id uint) (bool, error) {
var count int64
err := r.data.gormDB.WithContext(ctx).Model(&authorityButtonPO{}).Where("sys_base_menu_btn_id = ?", id).Count(&count).Error
return count == 0, err
}
func (r *accessRepo) AuthorityButtonIDs(ctx context.Context, aid uint) ([]uint, error) {
func (r *permissionRepo) AuthorityButtonIDs(ctx context.Context, aid uint) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Model(&authorityButtonPO{}).Where("authority_id = ?", aid).Pluck("sys_base_menu_btn_id", &ids).Error
return ids, err
+8 -8
View File
@@ -34,13 +34,13 @@ func (userPositionPO) TableName() string { return "sys_user_positions" }
func posFromPO(po positionPO) *biz.Position {
return &biz.Position{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, Code: po.Code, Sort: po.Sort, Status: po.Status, Remark: po.Remark}
}
func (r *organizationRepo) CreatePosition(ctx context.Context, v *biz.Position) error {
func (r *positionRepo) CreatePosition(ctx context.Context, v *biz.Position) error {
return r.data.gormDB.WithContext(ctx).Create(&positionPO{Name: v.Name, Code: v.Code, Sort: v.Sort, Status: v.Status, Remark: v.Remark}).Error
}
func (r *organizationRepo) UpdatePosition(ctx context.Context, v *biz.Position) error {
func (r *positionRepo) UpdatePosition(ctx context.Context, v *biz.Position) error {
return r.data.gormDB.WithContext(ctx).Model(&positionPO{}).Where("id = ?", v.ID).Updates(map[string]any{"name": v.Name, "code": v.Code, "sort": v.Sort, "status": v.Status, "remark": v.Remark}).Error
}
func (r *organizationRepo) DeletePosition(ctx context.Context, id uint) error {
func (r *positionRepo) DeletePosition(ctx context.Context, id uint) error {
if id == 0 {
return errors.New("岗位ID不能为空")
}
@@ -53,14 +53,14 @@ func (r *organizationRepo) DeletePosition(ctx context.Context, id uint) error {
}
return r.data.gormDB.WithContext(ctx).Delete(&positionPO{}, id).Error
}
func (r *organizationRepo) FindPosition(ctx context.Context, id uint) (*biz.Position, error) {
func (r *positionRepo) FindPosition(ctx context.Context, id uint) (*biz.Position, error) {
var po positionPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return posFromPO(po), nil
}
func (r *organizationRepo) ListPositions(ctx context.Context, page, size int, q *biz.PositionListFilter) ([]*biz.Position, int64, error) {
func (r *positionRepo) ListPositions(ctx context.Context, page, size int, q *biz.PositionListFilter) ([]*biz.Position, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&positionPO{})
if q != nil {
if q.Name != "" {
@@ -87,12 +87,12 @@ func (r *organizationRepo) ListPositions(ctx context.Context, page, size int, q
}
return out, total, nil
}
func (r *organizationRepo) PositionUserIDs(ctx context.Context, id uint) ([]uint, error) {
func (r *positionRepo) PositionUserIDs(ctx context.Context, id uint) ([]uint, error) {
var ids []uint
err := r.data.gormDB.WithContext(ctx).Model(&userPositionPO{}).Where("sys_position_id = ?", id).Pluck("sys_user_id", &ids).Error
return ids, err
}
func (r *organizationRepo) SetPositionUsers(ctx context.Context, id uint, ids []uint) error {
func (r *positionRepo) SetPositionUsers(ctx context.Context, id uint, ids []uint) error {
if id == 0 {
return errors.New("岗位ID不能为空")
}
@@ -110,7 +110,7 @@ func (r *organizationRepo) SetPositionUsers(ctx context.Context, id uint, ids []
return nil
})
}
func (r *organizationRepo) SetUserPositions(ctx context.Context, uid uint, ids []uint) error {
func (r *positionRepo) SetUserPositions(ctx context.Context, uid uint, ids []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("sys_user_id = ?", uid).Delete(&userPositionPO{}).Error; err != nil {
return err
+94
View File
@@ -0,0 +1,94 @@
package data
import (
"time"
"kra/internal/biz"
"kra/internal/conf"
"kra/pkg/adminauth"
)
type runtimeSettings struct{ runtime *conf.Runtime }
func NewRuntimeSettings(runtime *conf.Runtime) biz.RuntimeSettings {
return &runtimeSettings{runtime: runtime}
}
func (s *runtimeSettings) RouterPrefix() string {
config := s.runtime.Admin()
if config == nil {
return ""
}
return config.RouterPrefix
}
func (s *runtimeSettings) JWTSettings() biz.JWTSettings {
value := biz.JWTSettings{Issuer: "kra", Expires: 7 * 24 * time.Hour, Buffer: 24 * time.Hour}
config := s.runtime.Admin()
if config == nil || config.Jwt == nil {
return value
}
value.SigningKey = config.Jwt.SigningKey
if config.Jwt.Issuer != "" {
value.Issuer = config.Jwt.Issuer
}
if config.Jwt.ExpiresTime != nil {
value.Expires = config.Jwt.ExpiresTime.AsDuration()
}
if config.Jwt.BufferTime != nil {
value.Buffer = config.Jwt.BufferTime.AsDuration()
}
return value
}
func (s *runtimeSettings) CaptchaSettings() biz.CaptchaSettings {
value := biz.CaptchaSettings{KeyLong: 6, ImageWidth: 240, ImageHeight: 80, StoreExpiration: 3 * time.Minute}
config := s.runtime.Admin()
if config == nil || config.Captcha == nil {
return value
}
if config.Captcha.KeyLong > 0 {
value.KeyLong = int(config.Captcha.KeyLong)
}
if config.Captcha.ImgWidth > 0 {
value.ImageWidth = int(config.Captcha.ImgWidth)
}
if config.Captcha.ImgHeight > 0 {
value.ImageHeight = int(config.Captcha.ImgHeight)
}
if config.Captcha.StoreExpiration != nil {
value.StoreExpiration = config.Captcha.StoreExpiration.AsDuration()
}
return value
}
func (s *runtimeSettings) MediaSettings() biz.MediaSettings {
config := s.runtime.Admin()
if config == nil || config.Media == nil {
return biz.MediaSettings{}
}
return biz.MediaSettings{SessionTTL: int(config.Media.SessionTtl), MaxFileSize: config.Media.MaxFileSize}
}
func (s *runtimeSettings) UseMultipoint() bool {
config := s.runtime.Admin()
return config != nil && config.System != nil && config.System.UseMultipoint
}
type tokenIssuer struct{ settings biz.RuntimeSettings }
func NewTokenIssuer(settings biz.RuntimeSettings) biz.TokenIssuer {
return &tokenIssuer{settings: settings}
}
func (i *tokenIssuer) IssueToken(user *biz.User, authorityID uint, mustChangePassword bool, expires time.Duration) (*biz.IssuedToken, error) {
settings := i.settings.JWTSettings()
if expires <= 0 {
expires = settings.Expires
}
token, claims, err := adminauth.Generate(settings.SigningKey, settings.Issuer, expires, settings.Buffer, user.ID, authorityID, user.UUID, user.Username, user.NickName, mustChangePassword)
if err != nil {
return nil, err
}
return &biz.IssuedToken{Value: token, ExpiresAt: claims.ExpiresAt.Time, TTL: expires}, nil
}
+2 -2
View File
@@ -44,7 +44,7 @@ func securityFromPO(v securityConfigPO) *biz.SecurityConfig {
func securityToPO(v *biz.SecurityConfig) securityConfigPO {
return securityConfigPO{ID: 1, CaptchaOpen: v.CaptchaOpen, CaptchaTimeout: v.CaptchaTimeout, KeyLong: v.KeyLong, ImgWidth: v.ImgWidth, ImgHeight: v.ImgHeight, PwdMinLength: v.PwdMinLength, PwdRequireUpper: v.PwdRequireUpper, PwdRequireLower: v.PwdRequireLower, PwdRequireDigit: v.PwdRequireDigit, PwdRequireSpecial: v.PwdRequireSpecial, LimitEnable: v.LimitEnable, LimitWindow: v.LimitWindow, LimitCount: v.LimitCount, LockEnable: v.LockEnable, LockThreshold: v.LockThreshold, LockDuration: v.LockDuration, PwdExpireEnable: v.PwdExpireEnable, PwdExpireDays: v.PwdExpireDays, ForceNewUserChangePassword: v.ForceNewUserChangePassword}
}
func (r *settingsRepo) SecurityConfig(ctx context.Context) (*biz.SecurityConfig, error) {
func (r *securityRepo) SecurityConfig(ctx context.Context) (*biz.SecurityConfig, error) {
var po securityConfigPO
err := r.data.gormDB.WithContext(ctx).First(&po, 1).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -56,7 +56,7 @@ func (r *settingsRepo) SecurityConfig(ctx context.Context) (*biz.SecurityConfig,
}
return securityFromPO(po), nil
}
func (r *settingsRepo) SaveSecurityConfig(ctx context.Context, v *biz.SecurityConfig) error {
func (r *securityRepo) SaveSecurityConfig(ctx context.Context, v *biz.SecurityConfig) error {
previous, err := r.SecurityConfig(ctx)
if err != nil {
return err
+8 -5
View File
@@ -2,12 +2,15 @@ package data
import "kra/internal/biz"
type settingsRepo struct{ data *Data }
type dictionaryRepo struct{ data *Data }
type parameterRepo struct{ data *Data }
type apiTokenRepo struct{ data *Data }
type securityRepo struct{ data *Data }
func NewDictionaryRepo(data *Data) biz.DictionaryRepo { return &settingsRepo{data: data} }
func NewDictionaryRepo(data *Data) biz.DictionaryRepo { return &dictionaryRepo{data: data} }
func NewParameterRepo(data *Data) biz.ParameterRepo { return &settingsRepo{data: data} }
func NewParameterRepo(data *Data) biz.ParameterRepo { return &parameterRepo{data: data} }
func NewAPITokenRepo(data *Data) biz.APITokenRepo { return &settingsRepo{data: data} }
func NewAPITokenRepo(data *Data) biz.APITokenRepo { return &apiTokenRepo{data: data} }
func NewSecurityRepo(data *Data) biz.SecurityRepo { return &settingsRepo{data: data} }
func NewSecurityRepo(data *Data) biz.SecurityRepo { return &securityRepo{data: data} }
+4 -3
View File
@@ -90,8 +90,9 @@ type menuParameterPO struct {
func (menuParameterPO) TableName() string { return "sys_base_menu_parameters" }
type systemRepo struct{ data *Data }
type userRepo struct{ data *Data }
type initializationRepo struct{ data *Data }
func NewUserRepo(data *Data) biz.UserRepo { return &systemRepo{data: data} }
func NewUserRepo(data *Data) biz.UserRepo { return &userRepo{data: data} }
func NewInitializationRepo(data *Data) biz.InitializationRepo { return &systemRepo{data: data} }
func NewInitializationRepo(data *Data) biz.InitializationRepo { return &initializationRepo{data: data} }
+6 -6
View File
@@ -17,8 +17,8 @@ import (
"gorm.io/gorm"
)
func (r *systemRepo) PersistConfig(context.Context) error { return r.data.persistConfig() }
func (r *systemRepo) PersistAdminConfig(_ context.Context, raw []byte) error {
func (r *initializationRepo) PersistConfig(context.Context) error { return r.data.persistConfig() }
func (r *initializationRepo) PersistAdminConfig(_ context.Context, raw []byte) error {
currentData, currentAdmin := r.data.runtime.Values()
next := proto.Clone(currentAdmin).(*conf.AdminBackend)
if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(raw, next); err != nil {
@@ -33,7 +33,7 @@ func (r *systemRepo) PersistAdminConfig(_ context.Context, raw []byte) error {
r.data.runtime.Replace(currentData, next)
return nil
}
func (r *systemRepo) PersistRuntimeConfig(_ context.Context, dataRaw, adminRaw []byte) error {
func (r *initializationRepo) PersistRuntimeConfig(_ context.Context, dataRaw, adminRaw []byte) error {
currentData, currentAdmin := r.data.runtime.Values()
nextData := proto.Clone(currentData).(*conf.Data)
nextAdmin := proto.Clone(currentAdmin).(*conf.AdminBackend)
@@ -51,11 +51,11 @@ func (r *systemRepo) PersistRuntimeConfig(_ context.Context, dataRaw, adminRaw [
r.data.runtime.Replace(nextData, nextAdmin)
return nil
}
func (r *systemRepo) ReloadConfig(ctx context.Context) error {
func (r *initializationRepo) ReloadConfig(ctx context.Context) error {
return r.data.reloadConfig(ctx)
}
func (r *systemRepo) IsInitialized(ctx context.Context) (bool, error) {
func (r *initializationRepo) IsInitialized(ctx context.Context) (bool, error) {
db := r.data.gormDB.WithContext(ctx)
if !db.Migrator().HasTable(&userPO{}) {
return false, nil
@@ -67,7 +67,7 @@ func (r *systemRepo) IsInitialized(ctx context.Context) (bool, error) {
return count > 0, nil
}
func (r *systemRepo) Initialize(ctx context.Context, input *biz.DatabaseConfig) error {
func (r *initializationRepo) Initialize(ctx context.Context, input *biz.DatabaseConfig) error {
config := &conf.Data_Database{Driver: input.Driver, Host: input.Host, Port: input.Port, User: input.User, Password: input.Password, Name: input.Name, Path: input.Path, Config: input.Config}
r.data.initMu.Lock()
defer r.data.initMu.Unlock()
+2 -2
View File
@@ -30,7 +30,7 @@ func TestUserAuthorityWritesAreAtomic(t *testing.T) {
if err := data.gormDB.WithContext(ctx).Create(&[]authorityPO{{AuthorityID: 888, AuthorityName: "admin"}, {AuthorityID: 999, AuthorityName: "operator"}}).Error; err != nil {
t.Fatal(err)
}
repo := &systemRepo{data: data}
repo := &userRepo{data: data}
user := &biz.User{Username: "atomic", Password: "hash", NickName: "before", AuthorityID: 888, Enable: 1}
created, err := repo.CreateUserWithAuthorities(ctx, user, []uint{888, 999})
if err != nil {
@@ -66,7 +66,7 @@ func TestUserAuthorityWritesAreAtomic(t *testing.T) {
func TestDictionaryImportKeepsHierarchyInOneTransaction(t *testing.T) {
data := newTransactionTestData(t)
ctx := context.Background()
repo := &settingsRepo{data: data}
repo := &dictionaryRepo{data: data}
parentID := uint(10)
dictionary := &biz.Dictionary{Name: "status", Type: "status", Status: true}
details := []*biz.DictionaryDetail{{ID: 10, Label: "parent", Value: "1", Status: true}, {ID: 11, Label: "child", Value: "2", ParentID: &parentID, Status: true}}
+20 -20
View File
@@ -14,7 +14,7 @@ import (
"gorm.io/gorm"
)
func (r *systemRepo) FindUserByUsername(ctx context.Context, username string) (*biz.User, error) {
func (r *userRepo) FindUserByUsername(ctx context.Context, username string) (*biz.User, error) {
var po userPO
if err := r.data.gormDB.WithContext(ctx).Where("username = ?", username).First(&po).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -25,7 +25,7 @@ func (r *systemRepo) FindUserByUsername(ctx context.Context, username string) (*
return r.toBizUser(ctx, &po)
}
func (r *systemRepo) FindUserByID(ctx context.Context, id uint) (*biz.User, error) {
func (r *userRepo) FindUserByID(ctx context.Context, id uint) (*biz.User, error) {
var po userPO
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -36,7 +36,7 @@ func (r *systemRepo) FindUserByID(ctx context.Context, id uint) (*biz.User, erro
return r.toBizUser(ctx, &po)
}
func (r *systemRepo) toBizUser(ctx context.Context, po *userPO) (*biz.User, error) {
func (r *userRepo) toBizUser(ctx context.Context, po *userPO) (*biz.User, error) {
var authority authorityPO
if err := r.data.gormDB.WithContext(ctx).First(&authority, "authority_id = ?", po.AuthorityID).Error; err != nil {
return nil, err
@@ -90,7 +90,7 @@ func baseBizUser(po *userPO) *biz.User {
return &biz.User{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, UUID: po.UUID, Username: po.Username, Password: po.Password, NickName: po.NickName, HeaderImg: po.HeaderImg, AuthorityID: po.AuthorityID, DeptID: po.DeptID, Phone: po.Phone, Email: po.Email, Enable: po.Enable, OriginSetting: setting, MustChangePassword: po.MustChangePassword, PasswordUpdatedAt: po.PasswordUpdatedAt}
}
func (r *systemRepo) toBizUsers(ctx context.Context, pos []userPO) ([]*biz.User, error) {
func (r *userRepo) toBizUsers(ctx context.Context, pos []userPO) ([]*biz.User, error) {
if len(pos) == 0 {
return []*biz.User{}, nil
}
@@ -195,7 +195,7 @@ func (r *systemRepo) toBizUsers(ctx context.Context, pos []userPO) ([]*biz.User,
return users, nil
}
func (r *systemRepo) MenusByAuthority(ctx context.Context, authorityID uint) ([]*biz.Menu, error) {
func (r *userRepo) MenusByAuthority(ctx context.Context, authorityID uint) ([]*biz.Menu, error) {
var pos []menuPO
err := r.data.gormDB.WithContext(ctx).Table("sys_base_menus").
Joins("JOIN sys_authority_menus ON sys_authority_menus.sys_base_menu_id = sys_base_menus.id").
@@ -243,7 +243,7 @@ func (r *systemRepo) MenusByAuthority(ctx context.Context, authorityID uint) ([]
return roots, nil
}
func (r *systemRepo) ListUsers(ctx context.Context, page, pageSize int, filter *biz.UserListFilter) ([]*biz.User, int64, error) {
func (r *userRepo) ListUsers(ctx context.Context, page, pageSize int, filter *biz.UserListFilter) ([]*biz.User, int64, error) {
db := r.data.gormDB.WithContext(ctx).Model(&userPO{})
order := "id desc"
if filter != nil {
@@ -279,11 +279,11 @@ func (r *systemRepo) ListUsers(ctx context.Context, page, pageSize int, filter *
return users, total, err
}
func (r *systemRepo) CreateUser(ctx context.Context, user *biz.User) (*biz.User, error) {
func (r *userRepo) CreateUser(ctx context.Context, user *biz.User) (*biz.User, error) {
return r.CreateUserWithAuthorities(ctx, user, nil)
}
func (r *systemRepo) CreateUserWithAuthorities(ctx context.Context, user *biz.User, authorityIDs []uint) (*biz.User, error) {
func (r *userRepo) CreateUserWithAuthorities(ctx context.Context, user *biz.User, authorityIDs []uint) (*biz.User, error) {
if user.UUID == "" {
user.UUID = uuid.NewString()
}
@@ -332,11 +332,11 @@ func validateAuthorities(tx *gorm.DB, authorityIDs []uint) error {
return nil
}
func (r *systemRepo) UpdateUser(ctx context.Context, user *biz.User) error {
func (r *userRepo) UpdateUser(ctx context.Context, user *biz.User) error {
return r.updateUser(r.data.gormDB.WithContext(ctx), user)
}
func (r *systemRepo) UpdateSelfUser(ctx context.Context, user *biz.User) error {
func (r *userRepo) UpdateSelfUser(ctx context.Context, user *biz.User) error {
updates := make(map[string]any)
if user.NickName != "" {
updates["nick_name"] = user.NickName
@@ -359,7 +359,7 @@ func (r *systemRepo) UpdateSelfUser(ctx context.Context, user *biz.User) error {
return r.data.gormDB.WithContext(ctx).Model(&userPO{}).Where("id = ?", user.ID).Updates(updates).Error
}
func (r *systemRepo) FillDepartmentNamePaths(ctx context.Context, user *biz.User) error {
func (r *userRepo) FillDepartmentNamePaths(ctx context.Context, user *biz.User) error {
if user == nil || len(user.Departments) == 0 {
return nil
}
@@ -408,7 +408,7 @@ func (r *systemRepo) FillDepartmentNamePaths(ctx context.Context, user *biz.User
return nil
}
func (r *systemRepo) updateUser(tx *gorm.DB, user *biz.User) error {
func (r *userRepo) updateUser(tx *gorm.DB, user *biz.User) error {
updates := map[string]any{"nick_name": user.NickName, "header_img": user.HeaderImg, "phone": user.Phone, "email": user.Email, "enable": user.Enable}
if user.AuthorityID != 0 {
updates["authority_id"] = user.AuthorityID
@@ -416,7 +416,7 @@ func (r *systemRepo) updateUser(tx *gorm.DB, user *biz.User) error {
return tx.Model(&userPO{}).Where("id = ?", user.ID).Updates(updates).Error
}
func (r *systemRepo) UpdateUserWithAuthorities(ctx context.Context, user *biz.User, authorityIDs []uint) error {
func (r *userRepo) UpdateUserWithAuthorities(ctx context.Context, user *biz.User, authorityIDs []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := r.updateUser(tx, user); err != nil {
return err
@@ -427,7 +427,7 @@ func (r *systemRepo) UpdateUserWithAuthorities(ctx context.Context, user *biz.Us
return setUserAuthorities(tx, user.ID, authorityIDs)
})
}
func (r *systemRepo) DeleteUser(ctx context.Context, id uint) error {
func (r *userRepo) DeleteUser(ctx context.Context, id uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("sys_user_id = ?", id).Delete(&userAuthorityPO{}).Error; err != nil {
return err
@@ -441,7 +441,7 @@ func (r *systemRepo) DeleteUser(ctx context.Context, id uint) error {
return tx.Delete(&userPO{}, id).Error
})
}
func (r *systemRepo) UpdatePassword(ctx context.Context, id uint, password string, clearMustChange bool) error {
func (r *userRepo) UpdatePassword(ctx context.Context, id uint, password string, clearMustChange bool) error {
now := time.Now()
updates := map[string]any{"password": password, "password_updated_at": now}
if clearMustChange {
@@ -449,7 +449,7 @@ func (r *systemRepo) UpdatePassword(ctx context.Context, id uint, password strin
}
return r.data.gormDB.WithContext(ctx).Model(&userPO{}).Where("id = ?", id).Updates(updates).Error
}
func (r *systemRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, error) {
func (r *userRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, error) {
var pos []authorityPO
if err := r.data.gormDB.WithContext(ctx).Order("authority_id").Find(&pos).Error; err != nil {
return nil, err
@@ -462,9 +462,9 @@ func (r *systemRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, err
return result, nil
}
func (r *systemRepo) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
func (r *userRepo) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
access := &accessRepo{data: r.data}
access := &authorityAccessRepo{data: r.data}
for _, authorityID := range authorityIDs {
if err := access.checkAuthorityIDAuth(ctx, authorityID); err != nil {
return err
@@ -494,7 +494,7 @@ func setUserAuthorities(tx *gorm.DB, id uint, authorityIDs []uint) error {
return tx.Model(&userPO{}).Where("id = ?", id).Update("authority_id", authorityIDs[0]).Error
}
func (r *systemRepo) SetUserAuthority(ctx context.Context, id, authorityID uint) error {
func (r *userRepo) SetUserAuthority(ctx context.Context, id, authorityID uint) error {
db := r.data.gormDB.WithContext(ctx)
var count int64
if err := db.Model(&userAuthorityPO{}).Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityID).Count(&count).Error; err != nil {
@@ -520,7 +520,7 @@ func (r *systemRepo) SetUserAuthority(ctx context.Context, id, authorityID uint)
return db.Model(&userPO{}).Where("id = ?", id).Update("authority_id", authorityID).Error
}
func (r *systemRepo) SetUserSetting(ctx context.Context, id uint, setting map[string]any) error {
func (r *userRepo) SetUserSetting(ctx context.Context, id uint, setting map[string]any) error {
value, err := json.Marshal(setting)
if err != nil {
return err
+2 -2
View File
@@ -147,11 +147,11 @@ func (r *versionRepo) BuildVersionBundle(ctx context.Context, menuIDs, apiIDs, d
return nil, err
}
}
settings := &settingsRepo{data: r.data}
dictionariesRepo := &dictionaryRepo{data: r.data}
for _, po := range dictionaries {
v := dictionaryFromPO(po)
v.ID = 0
details, _, err := settings.ListDictionaryDetails(ctx, 0, 0, biz.DictionaryDetailFilter{DictionaryID: po.ID})
details, _, err := dictionariesRepo.ListDictionaryDetails(ctx, 0, 0, biz.DictionaryDetailFilter{DictionaryID: po.ID})
if err != nil {
return nil, err
}