/
subscriptions.go
213 lines (173 loc) · 6.2 KB
/
subscriptions.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package subscriptions
import (
"net/http"
"github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/opensentry/aap/app"
"github.com/opensentry/aap/gateway/aap"
"github.com/opensentry/aap/client"
bulky "github.com/charmixer/bulky/server"
)
func PostSubscriptions(env *app.Environment) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "PostSubscriptions",
})
var requests []client.CreateSubscriptionsRequest
err := c.BindJSON(&requests)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var handleRequests = func(iRequests []*bulky.Request) {
session, tx, err := aap.BeginWriteTx(env.Driver)
if err != nil {
bulky.FailAllRequestsWithInternalErrorResponse(iRequests)
log.Debug(err.Error())
return
}
defer tx.Close() // rolls back if not already committed/rolled back
defer session.Close()
requestor := c.MustGet("sub").(string)
var clients []string
for _, request := range iRequests {
r := request.Input.(client.CreateSubscriptionsRequest)
iSubscription := aap.Subscription{
Subscriber: aap.Identity{Id:r.Subscriber},
Publisher: aap.Identity{Id:r.Publisher},
Scope: aap.Scope{Name:r.Scope},
}
rSubscription, err := aap.CreateSubscription(tx, iSubscription, aap.Identity{Id:requestor})
if err != nil {
e := tx.Rollback()
if e != nil {
log.Debug(e.Error())
}
bulky.FailAllRequestsWithServerOperationAbortedResponse(iRequests) // Fail all with abort
request.Output = bulky.NewInternalErrorResponse(request.Index)
log.Debug(err.Error())
return
}
if rSubscription.Subscriber.Id != "" {
ok := client.CreateSubscriptionsResponse{
Subscriber: rSubscription.Subscriber.Id,
Publisher: rSubscription.Publisher.Id,
Scope: rSubscription.Scope.Name,
}
request.Output = bulky.NewOkResponse(request.Index, ok)
clients = append(clients, rSubscription.Subscriber.Id)
continue
}
// Deny by default
e := tx.Rollback()
if e != nil {
log.Debug(e.Error())
}
bulky.FailAllRequestsWithServerOperationAbortedResponse(iRequests) // Fail all with abort
request.Output = bulky.NewInternalErrorResponse(request.Index) // Specify error on failed one
return
}
err = bulky.OutputValidateRequests(iRequests)
if err == nil {
tx.Commit()
readSession, readTx, err := aap.BeginReadTx(env.Driver)
if err != nil {
log.Debug(err.Error())
return
}
defer readTx.Close() // rolls back if not already committed/rolled back
defer readSession.Close()
for _,id := range clients {
aap.SyncScopesToHydra(readTx, aap.Identity{Id:id}) // fire and forget to hydra
}
return
}
// Deny by default
tx.Rollback()
}
responses := bulky.HandleRequest(requests, handleRequests, bulky.HandleRequestParams{})
c.JSON(http.StatusOK, responses)
}
return gin.HandlerFunc(fn)
}
func DeleteSubscriptions(env *app.Environment) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "DeleteSubscriptions",
})
c.AbortWithStatusJSON(http.StatusOK, gin.H{
})
}
return gin.HandlerFunc(fn)
}
func GetSubscriptions(env *app.Environment) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "GetGrants",
})
var requests []client.ReadSubscriptionsRequest
err := c.BindJSON(&requests)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var handleRequest = func(iRequests []*bulky.Request){
//iRequest := aap.Identity{
//Id: c.MustGet("sub").(string),
//}
session, tx, err := aap.BeginReadTx(env.Driver)
if err != nil {
bulky.FailAllRequestsWithInternalErrorResponse(iRequests)
log.Debug(err.Error())
return
}
defer tx.Close() // rolls back if not already committed/rolled back
defer session.Close()
for _, request := range iRequests {
var r client.ReadSubscriptionsRequest
if request.Input != nil {
r = request.Input.(client.ReadSubscriptionsRequest)
}
var iFilterSubscriber aap.Identity
if r.Subscriber != "" {
iFilterSubscriber = aap.Identity{Id: r.Subscriber}
}
var iFilterPublisher aap.Identity
if r.Publisher != "" {
iFilterPublisher = aap.Identity{Id: r.Publisher}
}
var iFilterScopes []aap.Scope
if r.Scopes != nil {
for _,scopeName := range r.Scopes {
iFilterScopes = append(iFilterScopes, aap.Scope{Name: scopeName})
}
}
// TODO handle error
subscriptions, err := aap.FetchSubscriptions(tx, iFilterSubscriber, iFilterPublisher, iFilterScopes)
if err != nil {
// fail all requests
bulky.FailAllRequestsWithServerOperationAbortedResponse(iRequests)
// specify error on this request
request.Output = bulky.NewInternalErrorResponse(request.Index)
log.Debug(err.Error())
return
}
var ok = client.ReadSubscriptionsResponse{}
for _,subscription := range subscriptions {
ok = append(ok, client.Subscription{
Subscriber: subscription.Subscriber.Id,
Scope: subscription.Scope.Name,
Publisher: subscription.Publisher.Id,
})
}
request.Output = bulky.NewOkResponse(request.Index, ok)
}
}
responses := bulky.HandleRequest(requests, handleRequest, bulky.HandleRequestParams{EnableEmptyRequest: true})
c.JSON(http.StatusOK, responses)
}
return gin.HandlerFunc(fn)
}