11package elasticsearch
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "fmt"
7+ "io/ioutil"
58 "log"
69 "net/http"
710
@@ -13,6 +16,7 @@ import (
1316 "github.com/appbaseio/arc/model/acl"
1417 "github.com/appbaseio/arc/model/category"
1518 "github.com/appbaseio/arc/model/op"
19+ "github.com/appbaseio/arc/model/permission"
1620 "github.com/appbaseio/arc/plugins/auth"
1721 "github.com/appbaseio/arc/plugins/logs"
1822 "github.com/appbaseio/arc/util"
@@ -127,6 +131,39 @@ func transformRequest(h http.HandlerFunc) http.HandlerFunc {
127131 // transform POST request(search) to GET
128132 if * reqACL == category .Search {
129133 req .Method = http .MethodGet
134+ // Apply source filters
135+ reqPermission , err := permission .FromContext (ctx )
136+ if err != nil {
137+ log .Printf ("%s: %v\n " , logTag , err )
138+ util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
139+ return
140+ }
141+ sources := make (map [string ]interface {})
142+ var Includes , Excludes []string
143+ Includes = reqPermission .Includes
144+ Excludes = reqPermission .Excludes
145+ if len (Includes ) > 0 {
146+ sources ["includes" ] = Includes
147+ }
148+ if len (Excludes ) > 0 {
149+ sources ["excludes" ] = Excludes
150+ }
151+ body , err := ioutil .ReadAll (req .Body )
152+ if err != nil {
153+ log .Printf ("%s: %v\n " , logTag , err )
154+ util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
155+ return
156+ }
157+ d := json .NewDecoder (ioutil .NopCloser (bytes .NewReader (body )))
158+ reqBody := make (map [string ]interface {})
159+ d .Decode (& reqBody )
160+ _ , isExcludesPresent := sources ["excludes" ]
161+ isDefaultInclude := len (Includes ) > 0 && Includes [0 ] == "*"
162+ if ! isDefaultInclude || isExcludesPresent {
163+ reqBody ["_source" ] = sources
164+ }
165+ modifiedBody , _ := json .Marshal (reqBody )
166+ req .Body = ioutil .NopCloser (bytes .NewReader (modifiedBody ))
130167 }
131168 h (w , req )
132169 }
0 commit comments