This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
super_organisation.go
79 lines (66 loc) · 1.7 KB
/
super_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
package util
import (
"encoding/json"
"net/http"
"strconv"
"github.com/factly/mande-server/model"
"github.com/factly/x/middlewarex"
"github.com/spf13/viper"
)
// CheckSuperOrganisation checks weather organisation of user is super org or not
func CheckSuperOrganisation(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
oID, err := GetOrganisation(r.Context())
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
superOrgID, err := middlewarex.GetSuperOrganisationID("mande")
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Check if the organisation id is of super org
if superOrgID != oID {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Check if the user belong to the organisaiton
req, err := http.NewRequest("GET", viper.GetString("kavach_url")+"/organisations/my", nil)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-User", strconv.Itoa(uID))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
defer resp.Body.Close()
var orgList []model.OrgWithRole
err = json.NewDecoder(resp.Body).Decode(&orgList)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
belongs := false
for _, org := range orgList {
if org.ID == uint(oID) {
belongs = true
}
}
if !belongs {
w.WriteHeader(http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
})
}