Skip to content

Commit

Permalink
feat: add admin path
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jan 8, 2024
1 parent 8f8f8bd commit 35e2242
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/app/middleware/auth/auth.middleware.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package auth

import (
"net/http"

"github.com/isd-sgcu/johnjud-gateway/src/app/dto"
"github.com/isd-sgcu/johnjud-gateway/src/app/router"
"github.com/isd-sgcu/johnjud-gateway/src/app/utils"
"github.com/isd-sgcu/johnjud-gateway/src/app/utils/auth"
"github.com/isd-sgcu/johnjud-gateway/src/config"
"github.com/isd-sgcu/johnjud-gateway/src/constant/user"
authPkg "github.com/isd-sgcu/johnjud-gateway/src/pkg/service/auth"
"net/http"
)

type Guard struct {
service authPkg.Service
excludes map[string]struct{}
adminpath map[string]struct{}
conf config.App
versionList map[string]struct{}
}

func NewAuthGuard(s authPkg.Service, e map[string]struct{}, conf config.App, versionList map[string]struct{}) Guard {
func NewAuthGuard(s authPkg.Service, e map[string]struct{}, a map[string]struct{}, conf config.App, versionList map[string]struct{}) Guard {
return Guard{
service: s,
excludes: e,
adminpath: a,
conf: conf,
versionList: versionList,
}
Expand Down Expand Up @@ -53,5 +57,14 @@ func (m *Guard) Use(ctx router.IContext) error {
ctx.StoreValue("UserId", payload.UserId)
ctx.StoreValue("Role", payload.Role)

if utils.IsExisted(m.adminpath, path) && payload.Role != string(user.ADMIN) {
ctx.JSON(http.StatusUnauthorized, dto.ResponseErr{
StatusCode: http.StatusUnauthorized,
Message: "Unauthorized",
Data: nil,
})
return nil
}

return ctx.Next()
}
2 changes: 2 additions & 0 deletions src/constant/auth/auth.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var ExcludePath = map[string]struct{}{
"GET /adopt/": {},
}

var AdminPath = map[string]struct{}{}

var VersionList = map[string]struct{}{
"v1": {},
}

0 comments on commit 35e2242

Please sign in to comment.