-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.go
143 lines (116 loc) · 3.45 KB
/
group.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package auth
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/kidstuff/auth/authmodel"
"net/http"
"strconv"
"strings"
)
// CreateGroup document: http://kidstuff.github.io/swagger/#!/default/groups_post
func CreateGroup(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
groups := &authmodel.Group{}
err := json.NewDecoder(req.Body).Decode(groups)
if err != nil {
return http.StatusBadRequest, err
}
req.Body.Close()
groups, err = authCtx.Auth.AddGroupDetail(*groups.Name, groups.Privileges, groups.Info)
if err != nil {
return http.StatusInternalServerError, err
}
err = json.NewEncoder(rw).Encode(groups)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func findGroup(ctx *AuthContext, req *http.Request) (*authmodel.Group, int, error) {
sid := mux.Vars(req)["group_id"]
if len(sid) == 0 {
return nil, http.StatusBadRequest, ErrInvalidId
}
g, err := ctx.Auth.FindGroup(sid)
if err != nil {
if err == authmodel.ErrNotFound {
return nil, http.StatusNotFound, err
}
return nil, http.StatusInternalServerError, err
}
return g, http.StatusOK, nil
}
// UpdateGroup document: http://kidstuff.github.io/swagger/#!/default/groups_group_id_patch
func UpdateGroup(ctx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
g, stt, err := findGroup(ctx, req)
if err != nil {
return stt, err
}
g2 := authmodel.Group{}
err = json.NewDecoder(req.Body).Decode(&g2)
req.Body.Close()
if err != nil {
return http.StatusBadRequest, err
}
ctx.Logs.Debugf("%#v", g2)
err = ctx.Auth.UpdateGroupDetail(*g.Id, g2.Privileges, g2.Info)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// GetGroup document: http://kidstuff.github.io/swagger/#!/default/groups_group_id_get
func GetGroup(ctx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
g, stt, err := findGroup(ctx, req)
if err != nil {
return stt, err
}
err = json.NewEncoder(rw).Encode(g)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// DeleteGroup document: http://kidstuff.github.io/swagger/#!/default/groups_group_id_delete
func DeleteGroup(ctx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
sid := mux.Vars(req)["group_id"]
if len(sid) == 0 {
return http.StatusBadRequest, ErrInvalidId
}
err := ctx.Auth.DeleteGroup(sid)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// ListGroup document: http://kidstuff.github.io/swagger/#!/default/groups_get
func ListGroup(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
limit, err := strconv.Atoi(req.FormValue("limit"))
if err != nil {
limit = -1
}
offsetId := req.FormValue("offset")
var selectFields []string
if slt := req.FormValue("select"); len(slt) > 0 {
selectFields = strings.Split(slt, ",")
}
groups, err := authCtx.Auth.FindAllGroup(limit, offsetId, selectFields)
if err != nil {
return http.StatusInternalServerError, err
}
next := req.URL.String()
if err != nil {
q := req.URL.Query()
q.Set("offset", *groups[len(groups)-1].Id)
req.URL.RawQuery = q.Encode()
next = req.URL.String()
}
response := struct {
Groups []*authmodel.Group
Next string
}{groups, next}
err = json.NewEncoder(rw).Encode(response)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}