/
main.go
177 lines (168 loc) · 6.21 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"log"
"net/http"
"github.com/InteractiveLecture/id-extractor"
"github.com/InteractiveLecture/middlewares/jwtware"
"github.com/InteractiveLecture/pgmapper"
"github.com/gorilla/mux"
)
func main() {
dbHost := flag.String("dbhost", "localhost", "the database host")
dbPort := flag.Int("dbport", 5432, "the database port")
dbUser := flag.String("dbuser", "aclapp", "the database user")
dbSsl := flag.Bool("dbssl", false, "database ssl config")
dbName := flag.String("dbname", "acl", "the database name")
dbPassword := flag.String("dbpass", "", "database password")
flag.Parse()
config := pgmapper.DefaultConfig()
config.Host = *dbHost
config.Port = *dbPort
config.User = *dbUser
config.Ssl = *dbSsl
config.Database = *dbName
config.Password = *dbPassword
r := mux.NewRouter()
mapper, err := pgmapper.New(config)
if err != nil {
log.Fatal(err)
}
objectIdExtractor := idextractor.MuxIdExtractor("objectId")
userIdExtractor := idextractor.MuxIdExtractor("userId")
r.Methods("POST").Path("/objects").Handler(jwtware.New(addObjectHandler(mapper)))
r.Methods("DELETE").Path("/objects/{objectId}").Handler(jwtware.New(deleteObjectHandler(mapper, objectIdExtractor)))
r.Methods("GET").Path("/objects/{objectId}/permissions/{userId}").Handler(jwtware.New(getPermissionsHandler(mapper, objectIdExtractor, userIdExtractor)))
r.Methods("PUT").Path("/objects/{objectId}/permissions").Handler(jwtware.New(upsertPermissionsHandler(mapper, objectIdExtractor)))
r.Methods("PUT").Path("/sids/{sid}/permissions").Handler(jwtware.New(upsertMultiplePermissionsHandler(mapper, idextractor.MuxIdExtractor("sid"))))
log.Println("listening on 8080")
http.ListenAndServe(":8080", r)
}
func addObjectHandler(mapper *pgmapper.Mapper) http.Handler {
result := func(w http.ResponseWriter, r *http.Request) {
entity := make(map[string]interface{})
err := json.NewDecoder(r.Body).Decode(&entity)
if err != nil {
log.Println("error while decoding json: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = mapper.Execute("insert into object_identities(id,parent_object) values(%v)", entity["id"], entity["parent"])
if err != nil {
log.Println("error while insertiing object into database: ", err)
w.WriteHeader(http.StatusBadRequest)
}
}
return http.Handler(http.HandlerFunc(result))
}
func deleteMultipleObjectsHandler(mapper *pgmapper.Mapper) http.Handler {
result := func(w http.ResponseWriter, r *http.Request) {
ids, ok := r.URL.Query()["oid"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
}
err := mapper.Execute("SELECT delete_objects(%v)", ids)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
return http.Handler(http.HandlerFunc(result))
}
func deleteObjectHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
result := func(w http.ResponseWriter, r *http.Request) {
objectId, err := extractor(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = mapper.ExecuteRaw("delete from object_identities where id = $1", objectId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
return http.Handler(http.HandlerFunc(result))
}
func getPermissionsHandler(mapper *pgmapper.Mapper, objectIdExtractor idextractor.Extractor, userIdExtractor idextractor.Extractor) http.Handler {
result := func(w http.ResponseWriter, r *http.Request) {
objectId, err := objectIdExtractor(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
userId, err := userIdExtractor(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
result, err := mapper.QueryIntoBytes("SELECT get_permissions(%v)", objectId, userId)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
_, err = io.Copy(w, bytes.NewReader(result))
if err != nil {
log.Println(err)
}
}
return http.Handler(http.HandlerFunc(result))
}
func upsertMultiplePermissionsHandler(mapper *pgmapper.Mapper, sidIdExtractor idextractor.Extractor) http.Handler {
result := func(w http.ResponseWriter, r *http.Request) {
sid, err := sidIdExtractor(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
ids, ok := r.URL.Query()["oid"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
permissions := make(map[string]interface{})
err = json.NewDecoder(r.Body).Decode(&permissions)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = mapper.Execute("SELECT insert_bulk_sid_permissions(%v)", sid, permissions["create_permission"], permissions["read_permission"], permissions["update_permission"], permissions["delete_permission"], ids)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
return
}
return http.Handler(http.HandlerFunc(result))
}
func upsertPermissionsHandler(mapper *pgmapper.Mapper, objectIdExtractor idextractor.Extractor) http.Handler {
result := func(w http.ResponseWriter, r *http.Request) {
objectId, err := objectIdExtractor(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
ids, ok := r.URL.Query()["sid"]
entity := make(map[string]interface{})
err = json.NewDecoder(r.Body).Decode(&entity)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if ok {
err = mapper.Execute("SELECT insert_bulk_permissions(%v)", objectId, entity["create_permission"], entity["read_permission"], entity["update_permission"], entity["delete_permission"], ids)
} else {
_, err = mapper.ExecuteRaw("insert into acl_entries(object_id,sid,create_permission,read_permission,update_permission,delete_permission) values($1,$2,$3,$4,$5,$6) ON CONFLICT (object_id,sid) DO UPDATE SET create_permission = $3, read_permission = $4, update_permission = $5, delete_permission = $6 where acl_entries.sid = $2 AND acl_entries.object_id = $1", objectId, entity["sid"], entity["create_permission"], entity["read_permission"], entity["update_permission"], entity["delete_permission"])
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
return http.Handler(http.HandlerFunc(result))
}