From cad627c7b5cfbba7a2a64fb671dd7bff77aa0b3d Mon Sep 17 00:00:00 2001 From: veypi Date: Thu, 2 Apr 2026 17:36:18 +0800 Subject: [PATCH] refactor(init): Move provider and role initialization to Init() - Move VBaseProvider initialization from init() to Init() function - Move role initialization (admin/user) from api/init.go to Init() - Remove global VBaseProvider variable reference from tests - Use NewAuth factory function instead of direct auth.Factory.New call --- api/init.go | 4 ---- init.go | 10 +++++----- tests/scoped_auth_test.go | 5 ----- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/api/init.go b/api/init.go index cb55bf7..dd8fbca 100644 --- a/api/init.go +++ b/api/init.go @@ -49,10 +49,6 @@ func init() { Router.Use(cfg.Auth.Login()) Router.After(common.JsonResponse, common.JsonErrorResponse) - // 初始化角色 - cfg.Auth.AddRole("admin", "管理员", "*:7") - cfg.Auth.AddRole("user", "普通用户") - // 子路由挂载 Router.Extend("/auth", apiAuth.Router) Router.Extend("/users", user.Router) diff --git a/init.go b/init.go index a6d2c5d..6d7558f 100644 --- a/init.go +++ b/init.go @@ -27,19 +27,19 @@ var ( ) func init() { - // 初始化 VBaseProvider 并设置到全局 Auth - auth.VBaseProvider = auth.Factory.New("vb") - cfg.Auth.SetProvider(auth.VBaseProvider) - Router.Extend("/api", api.Router) Router.Extend("v", vhtmlui.Router) Router.Extend("vhtml", vhtml.Router) - vhtml.WrapUI(Router, uifs) } type Options = cfg.Options func Init() error { + // 初始化 VBaseProvider 并设置到全局 Auth + cfg.Auth.SetProvider(NewAuth("vb")) + // 初始化角色 + cfg.Auth.AddRole("admin", "管理员", "*:7") + cfg.Auth.AddRole("user", "普通用户") return models.Migrate() } diff --git a/tests/scoped_auth_test.go b/tests/scoped_auth_test.go index 49ccd2b..e5401b9 100644 --- a/tests/scoped_auth_test.go +++ b/tests/scoped_auth_test.go @@ -29,11 +29,6 @@ func TestScopedAuth(t *testing.T) { if !testAuth.Check(ctx, User1ID, permID, level) { t.Errorf("Expected User1 to have %s in %s scope", permID, scope) } - - // Verify Check in global VBaseProvider scope (should be false) - if auth.VBaseProvider.Check(ctx, User1ID, permID, level) { - t.Errorf("Expected User1 NOT to have %s in 'vb' scope", permID) - } }) // 2. Test Hierarchy (Admin Inheritance vs Normal Strictness)