-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.go
166 lines (143 loc) · 3.97 KB
/
resource.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
package query
import (
"github.com/fabbricadigitale/scimd/api"
"github.com/fabbricadigitale/scimd/api/attr"
"github.com/fabbricadigitale/scimd/api/filter"
"github.com/fabbricadigitale/scimd/api/messages"
"github.com/fabbricadigitale/scimd/schemas/core"
"github.com/fabbricadigitale/scimd/schemas/resource"
"github.com/fabbricadigitale/scimd/storage"
"github.com/fabbricadigitale/scimd/validation"
)
func makeAttrs(list []string) ([]*attr.Path, error) {
ret := make([]*attr.Path, len(list))
for i, a := range list {
p := attr.Parse(a)
if !p.Undefined() {
ret[i] = p
} else {
return nil, &api.InvalidPathError{
Path: a,
}
}
}
return ret, nil
}
func makeAttr(a string) (*attr.Path, error) {
p := attr.Parse(a)
if !p.Undefined() {
return p, nil
}
return nil, &api.InvalidPathError{
Path: a,
}
}
func Attributes(resTypes []*core.ResourceType, attrs *api.Attributes) (fields map[attr.Path]bool, err error) {
var in, ex []*attr.Path
fields = make(map[attr.Path]bool)
// When specified, the default list of attributes SHALL be
// overridden, and each resource returned MUST contain the
// minimum set of resource attributes and any attributes or
// sub-attributes explicitly requested by the "attributes"
// parameter (https://tools.ietf.org/html/rfc7644#section-3.9, https://tools.ietf.org/html/rfc7644#section-3.4.2.5)
if len(attrs.Attributes) > 0 {
in, err = makeAttrs(attrs.Attributes)
if err != nil {
return
}
}
// When specified, each resource returned MUST
// contain the minimum set of resource attributes.
// Additionally, the default set of attributes minus those
// attributes listed in "excludedAttributes" is returned (https://tools.ietf.org/html/rfc7644#section-3.9)
// (todo) > Specifing excludedAttribute whose schema "returned" parameter setting is "always" has no effect (https://tools.ietf.org/html/rfc7644#section-3.4.2.5)
if len(attrs.ExcludedAttributes) > 0 {
ex, err = makeAttrs(attrs.ExcludedAttributes)
if err != nil {
return
}
}
// Fields projection
if in != nil || ex != nil {
for _, rt := range resTypes {
for _, p := range attr.Projection(rt, in, ex) {
fields[*p] = true
}
}
}
return
}
func Resource(s storage.Storer, resType *core.ResourceType, id string, attrs *api.Attributes) (res core.ResourceTyper, err error) {
fields, err := Attributes([]*core.ResourceType{resType}, attrs)
if err != nil {
return
}
res, err = s.Get(resType, id, "", fields)
if err != nil {
return nil, err
}
if res == nil {
return nil, &api.NotFoundError{Subject: id}
}
return res, nil
}
func Resources(s storage.Storer, resTypes []*core.ResourceType, search *api.Search) (list *messages.ListResponse, err error) {
if err = validation.Validator.Struct(search); err != nil {
return
}
// Make filter
var f filter.Filter
if len(search.Filter) > 0 {
f, err = filter.CompileString(string(search.Filter))
if err != nil {
return
}
}
// Make query
var q storage.Querier
q, err = s.Find(resTypes, f)
defer q.Close()
if err != nil {
return
}
// Fields projection
fields, err := Attributes(resTypes, &search.Attributes)
if err != nil {
return
}
if fields != nil {
q.Fields(fields)
}
// Make list
list = messages.NewListResponse()
// Count
list.TotalResults, err = q.Count()
if err != nil {
return
}
// Unlimited
if search.Count == 0 {
search.Count = list.TotalResults
// (todo) > We need a way to LIMIT this to a MAX value (from config) - issue https://github.com/fabbricadigitale/scimd/issues/55
}
// Pagination
q.Skip(search.StartIndex - 1).Limit(search.Count)
list.StartIndex = search.StartIndex
list.ItemsPerPage = search.Count
// Sorting
if search.SortBy != "" {
var sortBy *attr.Path
sortBy, err = makeAttr(search.SortBy)
if err != nil {
return
}
q.Sort(*sortBy, search.SortOrder != api.DescendingOrder)
}
// Finally, fetch resources
var r *resource.Resource
for iter := q.Iter(); !iter.Done(); {
r = iter.Next()
list.Resources = append(list.Resources, r)
}
return
}