-
Notifications
You must be signed in to change notification settings - Fork 26
/
dao_es6.go
134 lines (117 loc) · 2.99 KB
/
dao_es6.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
package permissions
import (
"context"
"encoding/json"
"fmt"
"github.com/appbaseio/reactivesearch-api/util"
es6 "gopkg.in/olivere/elastic.v6"
)
func (es *elasticsearch) checkRoleExistsEs6(ctx context.Context, role string) (bool, error) {
resp, err := util.GetClient6().Search().
Index(es.indexName).
Query(es6.NewTermQuery("role", role)).
Do(ctx)
if err != nil {
return false, err
}
return resp.Hits.TotalHits > 0, nil
}
func (es *elasticsearch) getRawRolePermissionEs6(ctx context.Context, role string) ([]byte, error) {
resp, err := util.GetClient6().Search().
Index(es.indexName).
Query(es6.NewTermQuery("role", role)).
Size(1).
FetchSource(true).
Do(ctx)
if err != nil {
return nil, err
}
for _, hit := range resp.Hits.Hits {
src, err := json.Marshal(*hit.Source)
if err == nil {
return src, nil
}
}
return nil, nil
}
func (es *elasticsearch) getRawOwnerPermissionsEs6(ctx context.Context, owner string) ([]byte, error) {
resp, err := util.GetClient6().Search().
Index(es.indexName).
Query(es6.NewTermQuery("owner.keyword", owner)).
Size(1000).
Do(ctx)
if err != nil {
return nil, err
}
rawPermissions := []json.RawMessage{}
for _, hit := range resp.Hits.Hits {
rawPermission, err := applyExpiredField(*hit.Source)
if err != nil {
return nil, err
}
rawPermissions = append(rawPermissions, rawPermission)
}
raw, err := json.Marshal(rawPermissions)
if err != nil {
return nil, fmt.Errorf("unable to marshal slice of raw permissions: %v", err)
}
return raw, nil
}
func (es *elasticsearch) getPermissionsEs6(ctx context.Context, indices []string) ([]byte, error) {
query := es6.NewBoolQuery()
util.GetIndexFilterQueryEs6(query, indices...)
resp, err := util.GetClient6().Search().
Index(es.indexName).
Query(query).
Size(1000).
Do(ctx)
if err != nil {
return nil, err
}
rawPermissions := []json.RawMessage{}
for _, hit := range resp.Hits.Hits {
rawPermission, err := applyExpiredField(*hit.Source)
if err != nil {
return nil, err
}
rawPermissions = append(rawPermissions, rawPermission)
}
raw, err := json.Marshal(rawPermissions)
if err != nil {
return nil, fmt.Errorf("unable to marshal slice of raw permissions: %v", err)
}
return raw, nil
}
func (es *elasticsearch) getRawPermissionEs6(ctx context.Context, username string) ([]byte, error) {
response, err := util.GetClient6().Get().
Index(es.indexName).
Type(typeName).
Id(username).
FetchSource(true).
Do(ctx)
if err != nil {
return nil, err
}
src, err := applyExpiredField(*response.Source)
if err != nil {
return nil, err
}
return src, nil
}
func (es *elasticsearch) patchPermissionEs6(ctx context.Context, username string, patch map[string]interface{}) ([]byte, error) {
response, err := util.GetClient6().Update().
Refresh("wait_for").
Index(es.indexName).
Type(typeName).
Id(username).
Doc(patch).
Do(ctx)
if err != nil {
return nil, err
}
src, err := json.Marshal(response)
if err != nil {
return nil, err
}
return src, nil
}