Skip to content

Commit

Permalink
Merge pull request #138 from tinh-tinh/feat/ren/137-sync-guard-syntax
Browse files Browse the repository at this point in the history
feat: sync syntax use guard between module and controller
  • Loading branch information
Ren0503 authored Dec 3, 2024
2 parents 36eb8fd + d90d29b commit 8e71c96
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/composition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func Test_Compose(t *testing.T) {
// Guard
guard := func(ctrl *core.DynamicController, ctx *core.Ctx) bool {
guard := func(ctrl core.RefProvider, ctx *core.Ctx) bool {
return ctx.Query("key") == "value"
}

Expand Down
2 changes: 1 addition & 1 deletion core/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Test_Middleware_CustomCtx(t *testing.T) {
return ctx.Req().Header.Get("x-tenant-id")
})

guard := func(ctrl *core.DynamicController, ctx *core.Ctx) bool {
guard := func(ctrl core.RefProvider, ctx *core.Ctx) bool {
return ctx.Query("key") == "value"
}

Expand Down
21 changes: 15 additions & 6 deletions core/guard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// Guard is a function that checks access permission for a controller
type Guard func(ctrl *DynamicController, ctx *Ctx) bool
type Guard func(ref RefProvider, ctx *Ctx) bool

// ParseGuard wraps a Guard function into a Middleware that checks access permission
// for the given DynamicController. If the guard function returns false, it responds
Expand Down Expand Up @@ -37,18 +37,27 @@ func (c *DynamicController) Guard(guards ...Guard) *DynamicController {
return c
}

// AppGuard is a function that checks access permission for a module
type AppGuard func(module *DynamicModule, ctx Ctx) bool

// ParseGuard wraps an AppGuard function into a Middleware that checks access permission
// for the given DynamicModule. If the guard function returns false, it responds
// with a forbidden error, otherwise it calls the next middleware in the chain.
func (module *DynamicModule) ParseGuard(guard AppGuard) Middleware {
func (module *DynamicModule) ParseGuard(guard Guard) Middleware {
return func(ctx Ctx) error {
isAccess := guard(module, ctx)
isAccess := guard(module, &ctx)
if !isAccess {
return common.ForbiddenException(ctx.Res(), "you can not access")
}
return ctx.Next()
}
}

func (module *DynamicModule) Guard(guards ...Guard) *DynamicModule {
middlewares := []Middleware{}

for _, v := range guards {
mid := module.ParseGuard(v)
middlewares = append(middlewares, mid)
}

module.Use(middlewares...)
return module
}
8 changes: 4 additions & 4 deletions core/guard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func Test_ParseGuardCtrl(t *testing.T) {
guard := func(ctrl *core.DynamicController, ctx *core.Ctx) bool {
guard := func(ctrl core.RefProvider, ctx *core.Ctx) bool {
return ctx.Query("key") == "value"
}

Expand Down Expand Up @@ -53,7 +53,7 @@ func Test_ParseGuardCtrl(t *testing.T) {
}

func Test_ParseGuardModule(t *testing.T) {
guard := func(module *core.DynamicModule, ctx core.Ctx) bool {
guard := func(module core.RefProvider, ctx *core.Ctx) bool {
return ctx.Query("key") == "value"
}

Expand All @@ -72,7 +72,7 @@ func Test_ParseGuardModule(t *testing.T) {
module := func() *core.DynamicModule {
appModule := core.NewModule(core.NewModuleOptions{
Controllers: []core.Controller{authCtrl},
Guards: []core.AppGuard{guard},
Guards: []core.Guard{guard},
})

return appModule
Expand All @@ -97,7 +97,7 @@ func Test_ParseGuardModule(t *testing.T) {
const Key core.CtxKey = "key"

func Test_Ctx_Guard(t *testing.T) {
guard := func(ctrl *core.DynamicController, ctx *core.Ctx) bool {
guard := func(ctrl core.RefProvider, ctx *core.Ctx) bool {
ctx.Set(Key, "value")
return true
}
Expand Down
2 changes: 1 addition & 1 deletion core/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_Metadata(t *testing.T) {

controller := func(module *core.DynamicModule) *core.DynamicController {
ctrl := module.NewController("test").Guard(
func(ctrl *core.DynamicController, ctx *core.Ctx) bool {
func(ctrl core.RefProvider, ctx *core.Ctx) bool {
roles, ok := ctx.GetMetadata(role_key).([]string)
fmt.Println(roles)
if !ok || len(roles) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type NewModuleOptions struct {
Controllers []Controller
Providers []Provider
Exports []Provider
Guards []AppGuard
Guards []Guard
Middlewares []Middleware
Interceptor Interceptor
}
Expand Down
10 changes: 5 additions & 5 deletions core/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func Test_Nil(t *testing.T) {
Providers: []core.Provider{nil},
Imports: []core.Module{nil},
Exports: []core.Provider{nil},
Guards: []core.AppGuard{nil},
Guards: []core.Guard{nil},
Middlewares: []core.Middleware{nil},
})
return module
Expand Down Expand Up @@ -255,7 +255,7 @@ func Test_LifecycleModule(t *testing.T) {
return ctx.Next()
}

tenantGuard := func(module *core.DynamicModule, ctx core.Ctx) bool {
tenantGuard := func(module core.RefProvider, ctx *core.Ctx) bool {
return ctx.Get(Tenant) != nil
}

Expand All @@ -274,7 +274,7 @@ func Test_LifecycleModule(t *testing.T) {
appModule := func() *core.DynamicModule {
return core.NewModule(core.NewModuleOptions{
Middlewares: []core.Middleware{tenantMiddleware},
Guards: []core.AppGuard{tenantGuard},
Guards: []core.Guard{tenantGuard},
Controllers: []core.Controller{appController},
})
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func Test_PassMiddlewareModule(t *testing.T) {
return ctx.Next()
}

tenantGuard := func(module *core.DynamicModule, ctx core.Ctx) bool {
tenantGuard := func(module core.RefProvider, ctx *core.Ctx) bool {
return ctx.Get(Tenant) != nil
}

Expand Down Expand Up @@ -345,7 +345,7 @@ func Test_PassMiddlewareModule(t *testing.T) {
return core.NewModule(core.NewModuleOptions{
Imports: []core.Module{userModule},
Middlewares: []core.Middleware{tenantMiddleware},
Guards: []core.AppGuard{tenantGuard},
Guards: []core.Guard{tenantGuard},
})
}

Expand Down

0 comments on commit 8e71c96

Please sign in to comment.