优化
This commit is contained in:
@@ -48,6 +48,12 @@ func (r *apiRepo) SetAPIRoles(ctx context.Context, path, method string, ids []ui
|
||||
return tx.Create(&rules).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (r *apiRepo) CheckPolicyStore(ctx context.Context) error {
|
||||
var count int64
|
||||
return r.data.gormDB.WithContext(ctx).Model(&casbinRulePO{}).Count(&count).Error
|
||||
}
|
||||
|
||||
func (r *apiRepo) Authorize(ctx context.Context, aid uint, path, method string) (bool, error) {
|
||||
rows, err := policyRowsForAuthority(r.data.gormDB.WithContext(ctx), aid)
|
||||
if err != nil {
|
||||
|
||||
@@ -112,3 +112,17 @@ func TestSetPolicyPathsUsesCompatibleDedupeKey(t *testing.T) {
|
||||
t.Fatalf("deduplicated policies = %#v, want only the first concatenated-key match", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPolicyStore(t *testing.T) {
|
||||
data := newPolicyTestData(t)
|
||||
repo := &apiRepo{data: data}
|
||||
if err := repo.CheckPolicyStore(context.Background()); err != nil {
|
||||
t.Fatalf("CheckPolicyStore returned error for migrated table: %v", err)
|
||||
}
|
||||
if err := data.gormDB.WithContext(context.Background()).Migrator().DropTable(&casbinRulePO{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.CheckPolicyStore(context.Background()); err == nil {
|
||||
t.Fatal("CheckPolicyStore succeeded after casbin_rule was dropped")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func migrateAll(db *gorm.DB) error {
|
||||
&apiPO{}, &ignoredAPIPO{}, &authorityAPIPO{}, &casbinRulePO{}, &menuButtonPO{}, &authorityButtonPO{},
|
||||
&departmentPO{}, &positionPO{}, &userDepartmentPO{}, &userPositionPO{}, &authorityDepartmentPO{},
|
||||
&dictionaryPO{}, &dictionaryDetailPO{}, ¶meterPO{}, &apiTokenPO{}, &jwtBlacklistPO{}, &securityConfigPO{},
|
||||
&integrationConfigPO{},
|
||||
&versionPO{}, &exportTemplatePO{}, &exportConditionPO{}, &exportJoinPO{},
|
||||
&operationPO{}, &loginLogPO{}, &dataAccessLogPO{}, &errorRecordPO{},
|
||||
&taskPO{}, &taskLogPO{}, &mediaPO{}, &categoryPO{}, &uploadSessionPO{}, &uploadChunkPO{},
|
||||
|
||||
@@ -198,7 +198,11 @@ func (r *initializationRepo) Initialize(ctx context.Context, input *biz.Database
|
||||
return err
|
||||
}
|
||||
}
|
||||
ignoredAPIs := defaultIgnoredAPIs()
|
||||
staticPath := "uploads/file"
|
||||
if admin := r.data.runtime.Admin(); admin != nil && admin.Local != nil && strings.Trim(admin.Local.PathPrefix, "/") != "" {
|
||||
staticPath = strings.Trim(admin.Local.PathPrefix, "/")
|
||||
}
|
||||
ignoredAPIs := defaultIgnoredAPIs(staticPath)
|
||||
for _, ignored := range ignoredAPIs {
|
||||
if err := tx.FirstOrCreate(&ignored, ignored).Error; err != nil {
|
||||
return err
|
||||
@@ -250,10 +254,12 @@ func (r *initializationRepo) Initialize(ctx context.Context, input *biz.Database
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultIgnoredAPIs() []ignoredAPIPO {
|
||||
func defaultIgnoredAPIs(staticPath string) []ignoredAPIPO {
|
||||
staticRoute := "/" + strings.Trim(staticPath, "/") + "/*filepath"
|
||||
return []ignoredAPIPO{
|
||||
{Method: "GET", Path: "/api/freshCasbin"}, {Method: "GET", Path: "/health"},
|
||||
{Method: "GET", Path: "/swagger/*any"},
|
||||
{Method: "GET", Path: staticRoute}, {Method: "HEAD", Path: staticRoute},
|
||||
{Method: "POST", Path: "/system/reloadSystem"}, {Method: "POST", Path: "/base/login"},
|
||||
{Method: "POST", Path: "/base/captcha"}, {Method: "POST", Path: "/init/initdb"},
|
||||
{Method: "POST", Path: "/init/checkdb"}, {Method: "GET", Path: "/info/getInfoDataSource"},
|
||||
|
||||
@@ -3,10 +3,20 @@ package data
|
||||
import "testing"
|
||||
|
||||
func TestDefaultIgnoredAPIsIncludeSwagger(t *testing.T) {
|
||||
for _, api := range defaultIgnoredAPIs() {
|
||||
if api.Method == "GET" && api.Path == "/swagger/*any" {
|
||||
return
|
||||
wants := map[string]bool{
|
||||
"GET /swagger/*any": false,
|
||||
"GET /uploads/file/*filepath": false,
|
||||
"HEAD /uploads/file/*filepath": false,
|
||||
}
|
||||
for _, api := range defaultIgnoredAPIs("uploads/file") {
|
||||
key := api.Method + " " + api.Path
|
||||
if _, ok := wants[key]; ok {
|
||||
wants[key] = true
|
||||
}
|
||||
}
|
||||
for key, found := range wants {
|
||||
if !found {
|
||||
t.Fatalf("default ignored APIs do not include %s", key)
|
||||
}
|
||||
}
|
||||
t.Fatal("default ignored APIs do not include the Swagger handler")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user