feat(middleware): add benefactor authorization middleware

This commit is contained in:
Masood Keshvari 2024-01-17 11:16:57 +03:30
parent 91409f0275
commit 1a4db87c73
2 changed files with 26 additions and 1 deletions

View File

@ -2,13 +2,15 @@ package benefactorkindboxreqhandler
import ( import (
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware" "git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
"git.gocasts.ir/ebhomengo/niki/entity"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
) )
func (h Handler) SetRoutes(e *echo.Echo) { func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/benefactor/kindboxreqs") 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 //nolint:gocritic
// r.GET("/:id", h.Get) // r.GET("/:id", h.Get)
// r.GET("/", h.GetAll) // r.GET("/", h.GetAll)

View File

@ -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)
}
}
}