2024-02-24 17:06:13 +00:00
|
|
|
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"
|
|
|
|
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AdminAuthorization(service adminauthorizationservice.Service,
|
|
|
|
permissions ...entity.AdminPermission,
|
|
|
|
) echo.MiddlewareFunc {
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) (err error) {
|
|
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
|
|
|
|
|
|
isAllowed, err := service.CheckAccess(claims.UserID, entity.MapToAdminRole(claims.Role), permissions...)
|
|
|
|
if err != nil {
|
2024-07-21 13:10:22 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, errmsg.ErrorMsgSomethingWentWrong)
|
2024-02-24 17:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isAllowed {
|
2024-07-21 13:10:22 +00:00
|
|
|
return echo.NewHTTPError(http.StatusForbidden, errmsg.ErrorMsgUserNotAllowed)
|
2024-02-24 17:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|