/
user.go
36 lines (30 loc) 路 876 Bytes
/
user.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
35
36
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 User() 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!")
}
_, can := lib.Find(RBAC.UserRBAC, user.Role)
_, higher := lib.Find([]string{"SUPER", "ADMIN"}, user.Role)
if !can && !higher {
return echo.ErrForbidden
}
return next(c)
}
}
}