/
check_access.go
161 lines (152 loc) · 4.36 KB
/
check_access.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
package projectService
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strconv"
"time"
"github.com/dotenx/dotenx/ao-api/config"
"github.com/dotenx/dotenx/ao-api/pkg/utils"
"github.com/sirupsen/logrus"
)
func (ps *projectService) CheckCreateProjectAccess(accountId, projectType string) (bool, error) {
dto := projectDto{
AccountId: accountId,
ProjectType: projectType,
}
json_data, err := json.Marshal(dto)
if err != nil {
return false, errors.New("bad input body")
}
requestBody := bytes.NewBuffer(json_data)
token, err := utils.GeneratToken()
if err != nil {
return false, err
}
Requestheaders := []utils.Header{
{
Key: "Authorization",
Value: token,
},
{
Key: "Content-Type",
Value: "application/json",
},
}
httpHelper := utils.NewHttpHelper(utils.NewHttpClient())
url := config.Configs.Endpoints.Admin + "/internal/user/access/project"
out, err, status, _ := httpHelper.HttpRequest(http.MethodPost, url, requestBody, Requestheaders, time.Minute, true)
if err != nil {
return false, err
}
// if admin-api retrun 403 as status code, this shows there isn't any error so we return nil as error
if status == http.StatusForbidden {
return false, nil
}
if status != http.StatusOK && status != http.StatusAccepted {
logrus.Println(string(out))
return false, errors.New("not ok with status: " + strconv.Itoa(status))
}
var res struct {
Access bool `json:"access"`
}
if err := json.Unmarshal(out, &res); err != nil {
return false, err
}
return res.Access, nil
}
func (ps *projectService) CheckCreateDatabaseAccess(accountId string) (bool, error) {
// NOTE: currently we ignore limitation because body of this request should change based on project type and other changes for getting list of databases
// dto := projectDto{
// AccountId: accountId,
// }
// json_data, err := json.Marshal(dto)
// if err != nil {
// return false, errors.New("bad input body")
// }
// requestBody := bytes.NewBuffer(json_data)
// token, err := utils.GeneratToken()
// if err != nil {
// return false, err
// }
// Requestheaders := []utils.Header{
// {
// Key: "Authorization",
// Value: token,
// },
// {
// Key: "Content-Type",
// Value: "application/json",
// },
// }
// httpHelper := utils.NewHttpHelper(utils.NewHttpClient())
// url := config.Configs.Endpoints.Admin + "/internal/user/access/database"
// out, err, status, _ := httpHelper.HttpRequest(http.MethodPost, url, requestBody, Requestheaders, time.Minute, true)
// if err != nil {
// return false, err
// }
// //fmt.Println(string(out))
// if status != http.StatusOK && status != http.StatusAccepted {
// logrus.Println(string(out))
// return false, errors.New("not ok with status: " + strconv.Itoa(status))
// }
// var res struct {
// Access bool `json:"access"`
// }
// if err := json.Unmarshal(out, &res); err != nil {
// return false, err
// }
// return res.Access, nil
return true, nil
}
func (ps *projectService) CheckCreateDomainAccess(accountId, projectType string) (bool, error) {
dto := projectDto{
AccountId: accountId,
ProjectType: projectType,
}
json_data, err := json.Marshal(dto)
if err != nil {
return false, errors.New("bad input body")
}
requestBody := bytes.NewBuffer(json_data)
token, err := utils.GeneratToken()
if err != nil {
return false, err
}
Requestheaders := []utils.Header{
{
Key: "Authorization",
Value: token,
},
{
Key: "Content-Type",
Value: "application/json",
},
}
httpHelper := utils.NewHttpHelper(utils.NewHttpClient())
url := config.Configs.Endpoints.Admin + "/internal/user/access/domain"
out, err, status, _ := httpHelper.HttpRequest(http.MethodPost, url, requestBody, Requestheaders, time.Minute, true)
if err != nil {
return false, err
}
// if admin-api retrun 403 as status code, this shows there isn't any error so we return nil as error
if status == http.StatusForbidden {
return false, nil
}
if status != http.StatusOK && status != http.StatusAccepted {
logrus.Println(string(out))
return false, errors.New("not ok with status: " + strconv.Itoa(status))
}
var res struct {
Access bool `json:"access"`
}
if err := json.Unmarshal(out, &res); err != nil {
return false, err
}
return res.Access, nil
}
type projectDto struct {
AccountId string `json:"account_id" binding:"required"`
ProjectType string `json:"project_type" binding:"required"`
}