-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
241 lines (180 loc) · 5.62 KB
/
api.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package api
import (
"fmt"
sq "github.com/Masterminds/squirrel"
"github.com/rande/gonode/core/helper"
"github.com/rande/gonode/core/security"
"github.com/rande/gonode/core/squirrel"
"github.com/rande/gonode/modules/base"
log "github.com/sirupsen/logrus"
)
const (
OPERATION_OK = "OK"
OPERATION_KO = "KO"
)
type ApiPager struct {
Elements []interface{} `json:"elements"`
Page uint64 `json:"page"`
PerPage uint64 `json:"per_page"`
Next uint64 `json:"next"`
Previous uint64 `json:"previous"`
}
type Api struct {
Version string
Manager base.NodeManager
BaseUrl string
Logger *log.Logger
Authorizer security.AuthorizationChecker
}
type ApiOperation struct {
Status string `json:"status"`
Message string `json:"message"`
}
func (a *Api) SelectBuilder(options *base.SelectOptions) sq.SelectBuilder {
return a.Manager.SelectBuilder(options)
}
func (a *Api) Find(query sq.SelectBuilder, page uint64, perPage uint64, options *base.AccessOptions) (*ApiPager, error) {
if options != nil && len(options.Roles) > 0 {
value, _ := options.Roles.ToStringSlice()
query = query.Where(squirrel.NewExprSlice(fmt.Sprintf("\"%s\" && ARRAY["+sq.Placeholders(len(options.Roles))+"]", "access"), value))
}
list := a.Manager.FindBy(query, (page-1)*perPage, perPage+1)
pager := &ApiPager{
Page: page,
PerPage: perPage,
}
pager.Elements = make([]interface{}, 0)
if page > 1 {
pager.Previous = page - 1
}
counter := uint64(0)
for e := list.Front(); e != nil; e = e.Next() {
if counter == perPage {
pager.Next = page + 1
break
}
pager.Elements = append(pager.Elements, e.Value.(*base.Node))
counter++
}
return pager, nil
}
func (a *Api) Save(node *base.Node, options *base.AccessOptions) (*base.Node, base.Errors, error) {
if a.Logger != nil {
a.Logger.Printf("trying to save node.uuid=%s, node.type=%s", node.Uuid, node.Type)
}
saved := a.Manager.Find(node.Uuid)
if saved != nil {
a.Logger.Printf("find uuid: %s", node.Uuid)
helper.PanicUnless(node.Type == saved.Type, "Type mismatch")
if options != nil {
result, _ := a.Authorizer.IsGranted(options.Token, nil, node)
if !result {
return nil, nil, base.ErrAccessForbidden
}
}
if node.Deleted == true {
return nil, nil, base.ErrAlreadyDeleted
}
if node.Revision != saved.Revision {
return nil, nil, base.ErrRevision
}
node.Id = saved.Id
// we cannot overwrite the Parents, Or the ParentUuid, need to use the http API
node.Parents = saved.Parents
node.ParentUuid = saved.ParentUuid
} else if a.Logger != nil {
a.Logger.Printf("cannot find uuid: %s, create a new one", node.Uuid)
}
if a.Logger != nil {
a.Logger.Printf("saving node.id=%d, node.uuid=%s", node.Id, node.Uuid)
}
if ok, errors := a.Manager.Validate(node); !ok {
return nil, errors, base.ErrValidation
}
node, err := a.Manager.Save(node, true)
return node, nil, err
}
func (a *Api) Move(nodeUuid, parentUuid string, options *base.AccessOptions) (*ApiOperation, error) {
// handle node
nodeReference, err := base.GetReferenceFromString(nodeUuid)
if err != nil {
return nil, err
}
if node := a.Manager.Find(nodeReference); node == nil {
return nil, base.ErrNotFound
} else if result, _ := a.Authorizer.IsGranted(options.Token, nil, node); !result {
return nil, base.ErrAccessForbidden
}
// parent node
parentReference, err := base.GetReferenceFromString(parentUuid)
if err != nil {
return nil, err
}
if parent := a.Manager.Find(parentReference); parent == nil {
return nil, base.ErrNotFound
} else if result, _ := a.Authorizer.IsGranted(options.Token, nil, parent); !result {
return nil, base.ErrAccessForbidden
}
// move node
if affectedNodes, err := a.Manager.Move(nodeReference, parentReference); err != nil {
return nil, err
} else {
return &ApiOperation{
Status: OPERATION_OK,
Message: fmt.Sprintf("Node altered: %d", affectedNodes),
}, nil
}
}
func (a *Api) FindOne(uuid string, options *base.AccessOptions) (*base.Node, error) {
reference, err := base.GetReferenceFromString(uuid)
if err != nil {
return nil, base.ErrNotFound
}
query := a.Manager.SelectBuilder(base.NewSelectOptions()).Where(sq.Eq{"uuid": reference.String()})
return a.FindOneBy(query, options)
}
func (a *Api) FindOneBy(query sq.SelectBuilder, options *base.AccessOptions) (*base.Node, error) {
node := a.Manager.FindOneBy(query)
if node == nil {
return nil, base.ErrNotFound
}
if options != nil {
result, _ := a.Authorizer.IsGranted(options.Token, nil, node)
if !result {
return nil, base.ErrAccessForbidden
}
}
return node, nil
}
func (a *Api) RemoveOne(uuid string, options *base.AccessOptions) (*base.Node, error) {
reference, err := base.GetReferenceFromString(uuid)
if err != nil {
return nil, err
}
node := a.Manager.Find(reference)
if node == nil {
return nil, base.ErrNotFound
}
if options != nil {
result, _ := a.Authorizer.IsGranted(options.Token, nil, node)
if !result {
return nil, base.ErrAccessForbidden
}
}
if node.Deleted {
return nil, base.ErrAlreadyDeleted
}
return a.Manager.RemoveOne(node)
}
func (a *Api) Remove(query sq.SelectBuilder, options *base.AccessOptions) (*ApiPager, error) {
if options != nil && len(options.Roles) > 0 {
value, _ := options.Roles.ToStringSlice()
query = query.Where(squirrel.NewExprSlice(fmt.Sprintf("\"%s\" && ARRAY["+sq.Placeholders(len(options.Roles))+"]", "access"), value))
}
a.Manager.Remove(query)
return a.Find(query, 0, 0, options)
}