forked from osuripple/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
35 lines (30 loc) · 933 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
35
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/kawatapw/api/common"
)
type context struct {
User sessionUser
Token string
Language string
AvatarsVersion uint64
}
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)
}