forked from ebhomengo/niki
feat(middleware): add benefactor authorization middleware
This commit is contained in:
parent
91409f0275
commit
1a4db87c73
|
@ -2,13 +2,15 @@ package benefactorkindboxreqhandler
|
|||
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
echo "github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (h Handler) SetRoutes(e *echo.Echo) {
|
||||
r := e.Group("/benefactor/kindboxreqs")
|
||||
|
||||
r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig))
|
||||
r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig),
|
||||
middleware.BenefactorAuthorization(entity.UserBenefactorRole))
|
||||
//nolint:gocritic
|
||||
// r.GET("/:id", h.Get)
|
||||
// r.GET("/", h.GetAll)
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
claims := claim.GetClaimsFromEchoContext(c)
|
||||
if claims.Role != role {
|
||||
return c.JSON(http.StatusForbidden, echo.Map{"message": errmsg.ErrorMsgUserNotAllowed})
|
||||
}
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue