forked from osuthailand/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
34 lines (29 loc) · 895 Bytes
/
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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/osuthailand/api/common"
)
type context struct {
User sessionUser
Token string
Language string
}
type sessionUser struct {
ID int
Username string
Privileges common.UserPrivileges
Flags uint64
}
// OnlyUserPublic returns a string containing "(user.privileges & 1 = 1 OR users.id = <userID>)"
// if the user does not have the UserPrivilege AdminManageUsers, and returns "1" otherwise.
func (ctx context) OnlyUserPublic() string {
if ctx.User.Privileges&common.AdminPrivilegeManageUsers == common.AdminPrivilegeManageUsers {
return "1"
}
// It's safe to use sprintf directly even if it's a query, because UserID is an int.
return fmt.Sprintf("(users.privileges & 1 = 1 OR users.id = '%d')", ctx.User.ID)
}
func getContext(c *gin.Context) context {
return c.MustGet("context").(context)
}