-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorebosgowslib.go
492 lines (459 loc) · 15.5 KB
/
corebosgowslib.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Package corebosgowslib coreBOS Web service access library
/*************************************************************************************************
* Copyright 2018 JPL TSolucio, S.L. -- This file is a part of TSOLUCIO coreBOS.
* The MIT License (MIT)
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************************************/
package corebosgowslib
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
type cbConnectionType struct {
servertime float64
expiretime string
token string
serviceuser string
servicekey string
sessionid string
userid string
}
var cbURL = "/webservice.php"
var netClient = &http.Client{
Timeout: time.Second * 10,
}
type cbContext struct {
cbConnection cbConnectionType
queryResultColumns map[int]string
}
func GetCbContext() *cbContext {
return &cbContext{
queryResultColumns: make(map[int]string),
}
}
// SetURL of the coreBOS application you want to connect to
func SetURL(cburl string) {
if cburl[len(cburl)-1:] == "/" {
cbURL = cburl + "webservice.php"
} else {
cbURL = cburl + "/webservice.php"
}
}
func (cbCtx *cbContext) GetServerTime() float64 {
return cbCtx.cbConnection.servertime
}
func (cbCtx *cbContext) GetExpireTime() string {
return cbCtx.cbConnection.expiretime
}
func (cbCtx *cbContext) GetToken() string {
return cbCtx.cbConnection.token
}
func (cbCtx *cbContext) GetServiceUser() string {
return cbCtx.cbConnection.serviceuser
}
func (cbCtx *cbContext) GetServiceKey() string {
return cbCtx.cbConnection.servicekey
}
func (cbCtx *cbContext) GetSessionId() string {
return cbCtx.cbConnection.sessionid
}
func (cbCtx *cbContext) GetUserId() string {
return cbCtx.cbConnection.userid
}
func (cbCtx *cbContext) doChallenge(username string) (bool, error) {
v := url.Values{
"operation": {"getchallenge"},
"username": {username},
}
_, dat, e := cbCtx.docbCall("GET", v)
if e == nil && dat["success"] == true {
rdo := dat["result"].(map[string]interface{})
cbCtx.cbConnection.servertime = rdo["serverTime"].(float64)
cbCtx.cbConnection.expiretime = rdo["expireTime"].(string)
cbCtx.cbConnection.token = rdo["token"].(string)
return true, nil
}
return false, e
}
// DoLogin connects to coreBOS and tries to validate the given user.
// true or false will be returned depending on the result of the validation
func (cbCtx *cbContext) DoLogin(username string, userAccesskey string, withpassword bool) (bool, error) {
dc, err := cbCtx.doChallenge(username)
if dc == false {
return false, err
}
v := url.Values{
"operation": {"login"},
"username": {username},
}
if withpassword == true {
v.Set("accessKey", cbCtx.cbConnection.token+userAccesskey)
} else {
v.Set("accessKey", getMD5Hash(cbCtx.cbConnection.token+userAccesskey))
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
rdo := dat["result"].(map[string]interface{})
cbCtx.cbConnection.serviceuser = username
cbCtx.cbConnection.servicekey = userAccesskey
cbCtx.cbConnection.sessionid = rdo["sessionName"].(string)
cbCtx.cbConnection.userid = rdo["userId"].(string)
return true, nil
}
if e == nil {
wserr := dat["error"].(map[string]interface{})
return false, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return false, e
}
// DoLogout disconnects from coreBOS.
// returns true or false depending on the result
func (cbCtx *cbContext) DoLogout() (bool, error) {
v := url.Values{
"operation": {"logout"},
"sessionName": {cbCtx.cbConnection.sessionid},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
return true, nil
}
if e == nil {
wserr := dat["error"].(map[string]interface{})
return false, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return false, e
}
// DoQuery retrieves records using a specilized query language
// returns the set of records and columsn obtained from the query
func (cbCtx *cbContext) DoQuery(query string) (interface{}, error) {
// query must end in ; so we make sure
q := strings.Trim(query, " ;") + ";"
v := url.Values{
"operation": {"query"},
"sessionName": {cbCtx.cbConnection.sessionid},
"query": {q},
}
_, dat, e := cbCtx.docbCall("GET", v)
if e == nil && dat["success"] == true {
rdos := dat["result"].([]interface{})
if len(rdos) > 0 {
idx := 0
for col := range rdos[0].(map[string]interface{}) {
cbCtx.queryResultColumns[idx] = col
idx++
}
} else {
cbCtx.queryResultColumns = make(map[int]string)
}
return dat["result"], nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// GetResultColumns returns the column names of the last query
func (cbCtx *cbContext) GetResultColumns() map[int]string {
return cbCtx.queryResultColumns
}
// DoListTypes returns a list of available Modules for the connected user
// filtered by those that contain a field of the given type(s). If no type is
// given all accessible modules will be returned
func (cbCtx *cbContext) DoListTypes(fieldTypeList []string) (map[string]string, error) {
ftl, _ := json.Marshal(fieldTypeList)
v := url.Values{
"operation": {"listtypes"},
"sessionName": {cbCtx.cbConnection.sessionid},
"fieldTypeList": {string(ftl)},
}
_, dat, e := cbCtx.docbCall("GET", v)
if e == nil && dat["success"] == true {
rdos := dat["result"].(map[string]interface{})
types := make(map[string]string)
for _, mod := range rdos["types"].([]interface{}) {
mname := mod.(string)
types[mname] = mname
}
return types, nil
}
empty := make(map[string]string)
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoDescribe gets all the details of a Module's permissions and fields
func (cbCtx *cbContext) DoDescribe(module string) (map[string]interface{}, error) {
v := url.Values{
"operation": {"describe"},
"sessionName": {cbCtx.cbConnection.sessionid},
"elementType": {module},
}
_, dat, e := cbCtx.docbCall("GET", v)
if e == nil && dat["success"] == true {
return dat["result"].(map[string]interface{}), nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoRetrieve details of a record.
func (cbCtx *cbContext) DoRetrieve(record string) (map[string]interface{}, error) {
v := url.Values{
"operation": {"retrieve"},
"sessionName": {cbCtx.cbConnection.sessionid},
"id": {record},
}
_, dat, e := cbCtx.docbCall("GET", v)
if e == nil && dat["success"] == true {
return dat["result"].(map[string]interface{}), nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoCreate adds new records to the application
func (cbCtx *cbContext) DoCreate(module string, valuemap map[string]interface{}) (map[string]interface{}, error) {
// Assign record to logged in user if not specified
_, exists := valuemap["assigned_user_id"]
if exists == false {
valuemap["assigned_user_id"] = cbCtx.cbConnection.userid
}
vals, _ := json.Marshal(valuemap)
v := url.Values{
"operation": {"create"},
"sessionName": {cbCtx.cbConnection.sessionid},
"elementType": {module},
"element": {string(vals)},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
return dat["result"].(map[string]interface{}), nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoUpdate updates the given record with the new values.
// ALL mandatory fields MUST be present
func (cbCtx *cbContext) DoUpdate(module string, valuemap map[string]interface{}) (map[string]interface{}, error) {
// Assign record to logged in user if not specified
_, exists := valuemap["assigned_user_id"]
if exists == false {
valuemap["assigned_user_id"] = cbCtx.cbConnection.userid
}
vals, _ := json.Marshal(valuemap)
v := url.Values{
"operation": {"update"},
"sessionName": {cbCtx.cbConnection.sessionid},
"elementType": {module},
"element": {string(vals)},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
return dat["result"].(map[string]interface{}), nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoRevise updates the given record with the new values.
// mandatory fields do not have to be present
func (cbCtx *cbContext) DoRevise(module string, valuemap map[string]interface{}) (map[string]interface{}, error) {
// Assign record to logged in user if not specified
_, exists := valuemap["assigned_user_id"]
if exists == false {
valuemap["assigned_user_id"] = cbCtx.cbConnection.userid
}
vals, _ := json.Marshal(valuemap)
v := url.Values{
"operation": {"revise"},
"sessionName": {cbCtx.cbConnection.sessionid},
"elementType": {module},
"element": {string(vals)},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
return dat["result"].(map[string]interface{}), nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoDelete eliminates the record with the given ID
func (cbCtx *cbContext) DoDelete(record string) (bool, error) {
v := url.Values{
"operation": {"delete"},
"sessionName": {cbCtx.cbConnection.sessionid},
"id": {record},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
rdo := dat["result"].(map[string]interface{})
if rdo["status"].(string) == "successful" {
return true, nil
} else {
return false, errors.New("Unexpected DELETE error")
}
}
if e == nil {
wserr := dat["error"].(map[string]interface{})
return false, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return false, e
}
// DoInvoke custom operations
// param String method Name of the webservice to invoke
// param Object type null or parameter values to method
// param String POST/GET
func (cbCtx *cbContext) DoInvoke(method string, params map[string]interface{}, typeofcall string) (map[string]interface{}, error) {
v := url.Values{
"operation": {method},
"sessionName": {cbCtx.cbConnection.sessionid},
}
for idx, val := range params {
if v.Get(idx) == "" {
v.Set(idx, val.(string))
}
}
_, dat, e := cbCtx.docbCall(typeofcall, v)
if e == nil && dat["success"] == true {
return dat["result"].(map[string]interface{}), nil
}
empty := make(map[string]interface{})
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoGetRelatedRecords will retrieve all related records of the given record that belong to the given related module
func (cbCtx *cbContext) DoGetRelatedRecords(record string, module string, relatedModule string, queryParameters map[string]interface{}) ([]interface{}, error) {
params, _ := json.Marshal(queryParameters)
v := url.Values{
"operation": {"getRelatedRecords"},
"sessionName": {cbCtx.cbConnection.sessionid},
"id": {record},
"module": {module},
"relatedModule": {relatedModule},
"queryParameters": {string(params)},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
rdo := dat["result"].(map[string]interface{})
return rdo["records"].([]interface{}), nil
}
var empty []interface{}
if e == nil {
wserr := dat["error"].(map[string]interface{})
return empty, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return empty, e
}
// DoSetRelated establishes a relation between records
// param relateThisID string ID of record we want to be related with other records
// param withTheseIds array of IDs to relate to the first parameter
func (cbCtx *cbContext) DoSetRelated(relateThisID string, withTheseIds []string) (bool, error) {
params, _ := json.Marshal(withTheseIds)
v := url.Values{
"operation": {"SetRelation"},
"sessionName": {cbCtx.cbConnection.sessionid},
"relate_this_id": {relateThisID},
"with_these_ids": {string(params)},
}
_, dat, e := cbCtx.docbCall("POST", v)
if e == nil && dat["success"] == true {
return dat["result"].(bool), nil
}
if e == nil {
wserr := dat["error"].(map[string]interface{})
return false, errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return false, e
}
// DoLoginPage get HTML for the Login page
func (cbCtx *cbContext) DoLoginPage(template string, language string, csrf string) (string, error) {
v := url.Values{
"operation": {"getLoginPage"},
"sessionName": {cbCtx.cbConnection.sessionid},
"template": {template},
"language": {language},
"csrf": {csrf},
}
_, dat, e := cbCtx.docbCall("GET", v)
if e == nil && dat["success"] == true {
return dat["result"].(string), nil
}
if e == nil {
wserr := dat["error"].(map[string]interface{})
return "", errors.New(wserr["code"].(string) + ": " + wserr["message"].(string))
}
return "", e
}
///////////////////////////////////////////////////////
func (cbCtx *cbContext) docbCall(typeofcall string, params url.Values) (bool, map[string]interface{}, error) {
empty := make(map[string]interface{})
var resp *http.Response
var err error
body := bytes.NewBufferString(params.Encode())
if strings.ToUpper(typeofcall[0:]) == "POST" {
resp, err = netClient.Post(cbURL, "application/x-www-form-urlencoded", body)
} else {
resp, err = netClient.Get(cbURL + "?" + body.String())
}
if err != nil {
return false, empty, err
}
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return false, empty, err
}
var dat map[string]interface{}
e := json.Unmarshal(b, &dat)
return true, dat, e
}
func getMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}