-
Notifications
You must be signed in to change notification settings - Fork 0
/
super.go
34 lines (28 loc) · 799 Bytes
/
super.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package auth
import (
"net/http"
"github.com/jinzhu/gorm"
"github.com/labstack/echo"
"github.com/nguyentrungtin/go-echo-boilerplate/db"
"github.com/nguyentrungtin/go-echo-boilerplate/lib"
"github.com/nguyentrungtin/go-echo-boilerplate/model"
)
func Super() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
id, ok := GetUserID(c)
if !ok {
return echo.ErrUnauthorized
}
user := model.User{}
db := db.Session()
if err := db.Where("id = ?", id).First(&user).Error; gorm.IsRecordNotFoundError(err) {
return echo.NewHTTPError(http.StatusBadGateway, "User is no longer exist!")
}
if _, ok := lib.Find(RBAC.SuperRBAC, user.Role); !ok {
return echo.ErrForbidden
}
return next(c)
}
}
}