/
permission.go
51 lines (46 loc) · 1.15 KB
/
permission.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
package api
import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/THUAI-ssast/hiper-backend/web/model"
)
func grantCreationPermission(c *gin.Context) {
author_ids := c.Param("user_id")
userID := c.MustGet("userID").(int)
author_id, err := strconv.Atoi(author_ids)
if err != nil {
c.JSON(401, gin.H{})
}
if userID != 1 || author_id == 1 {
c.JSON(401, gin.H{})
}
_, err = model.GetUserByID((uint)(author_id))
if err != nil {
c.JSON(404, gin.H{})
}
err = model.UpdateUserByID((uint)(author_id), map[string]interface{}{"can_create_game_or_contest": true})
if err != nil {
c.JSON(500, gin.H{})
}
c.JSON(200, gin.H{})
}
func revokeCreationPermission(c *gin.Context) {
author_ids := c.Param("user_id")
userID := c.MustGet("userID").(int)
author_id, err := strconv.Atoi(author_ids)
if err != nil {
c.JSON(401, gin.H{})
}
if userID != 1 || author_id == 1 {
c.JSON(401, gin.H{})
}
_, err = model.GetUserByID((uint)(author_id))
if err != nil {
c.JSON(404, gin.H{})
}
err = model.UpdateUserByID((uint)(author_id), map[string]interface{}{"can_create_game_or_contest": false})
if err != nil {
c.JSON(500, gin.H{})
}
c.JSON(200, gin.H{})
}