This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
organisation.go
99 lines (78 loc) · 2.04 KB
/
organisation.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
package util
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/factly/dega-server/service/core/model"
"github.com/spf13/viper"
)
type paging struct {
Nodes []model.Organisation `json:"nodes"`
Total int64 `json:"total"`
}
// GetAllOrganisationsMap return slice of all organisations
func GetAllOrganisationsMap(q string) (map[uint]model.Organisation, error) {
path := "/organisations"
if q != "" {
path = fmt.Sprint(path, "?q=", q)
}
req, err := http.NewRequest("GET", viper.GetString("kavach_url")+path, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
var pagingList paging
if resp.StatusCode != http.StatusOK {
return nil, errors.New("could not fetch organisations")
}
err = json.NewDecoder(resp.Body).Decode(&pagingList)
if err != nil {
return nil, err
}
orgMap := make(map[uint]model.Organisation)
for _, organisation := range pagingList.Nodes {
orgMap[organisation.ID] = organisation
}
return orgMap, nil
}
type orgWithPermission struct {
model.Organisation
Permission permission `json:"permission"`
}
type permission struct {
Role string `json:"role"`
}
// CheckOwnerFromKavach checks if user is owner of organisation
func CheckOwnerFromKavach(uID, oID int) (bool, error) {
req, err := http.NewRequest("GET", viper.GetString("kavach_url")+"/organisations/my", nil)
if err != nil {
return false, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-User", fmt.Sprint(uID))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, err
}
if resp.StatusCode != http.StatusOK {
return false, errors.New("error from kavach server")
}
var permArr []orgWithPermission
err = json.NewDecoder(resp.Body).Decode(&permArr)
if err != nil {
return false, err
}
for _, each := range permArr {
if each.Permission.Role == "owner" && each.ID == uint(oID) {
return true, nil
}
}
return false, nil
}