-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
86 lines (78 loc) · 1.86 KB
/
context.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ginboot
import (
"errors"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"
)
type AuthContext struct {
UserID string
UserEmail string
Roles []string
Claims map[string]interface{}
}
type Context struct {
*gin.Context
authContext *AuthContext
}
func NewContext(c *gin.Context) *Context {
return &Context{
Context: c,
}
}
// GetAuthContext returns the current auth context
func (c *Context) GetAuthContext() (AuthContext, error) {
userId, exists := c.Get("user_id")
if !exists {
c.AbortWithStatus(http.StatusUnauthorized)
return AuthContext{}, errors.New("operation not permitted")
}
role, exists := c.Get("role")
if !exists {
c.AbortWithStatus(http.StatusUnauthorized)
return AuthContext{}, errors.New("operation not permitted")
}
return AuthContext{
UserID: userId.(string),
Roles: []string{role.(string)},
}, nil
}
func (c *Context) GetRequest(request interface{}) error {
if err := c.ShouldBind(request); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return errors.New("bad request: " + err.Error())
}
return nil
}
func (c *Context) GetPageRequest() PageRequest {
pageString := c.DefaultQuery("page", "1")
sizeString := c.DefaultQuery("size", "10")
sortString := c.DefaultQuery("sort", "_id,asc")
page, err := strconv.ParseInt(pageString, 10, 64)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
}
size, err := strconv.ParseInt(sizeString, 10, 64)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
}
sortSplit := strings.Split(sortString, ",")
var sort SortField
if len(sortSplit) > 1 {
direction := 1
if sortSplit[1] == "desc" {
direction = -1
}
sort = SortField{
Field: sortSplit[0],
Direction: direction,
}
} else {
sort = SortField{
Field: sortSplit[0],
Direction: 1,
}
}
return PageRequest{Page: int(page), Size: int(size), Sort: sort}
}