This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
225 lines (191 loc) · 4.8 KB
/
role.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
package goh4
import (
"bytes"
"encoding/json"
"fmt"
)
// Role Struct for a Tetration Role
type Role struct {
ID string `json:"id,omitempty"`
Scope *Scope `json:"app_scope_id"`
Name string `json:"name"`
Description string `json:"description"`
Capabilities []Capability `json:"capabilities,omitempty"`
}
// MarshalJSON Converts Struct to JSON
func (r *Role) MarshalJSON() ([]byte, error) {
type Alias Role
return json.Marshal(&struct {
Scope string `json:"app_scope_id"`
*Alias
}{
Scope: r.Scope.ID,
Alias: (*Alias)(r),
})
}
// UnmarshalJSON Converts JSON to struct
func (r *Role) UnmarshalJSON(data []byte) error {
var err error
type Alias Role
aux := &struct {
Scope string `json:"app_scope_id"`
*Alias
}{
Alias: (*Alias)(r),
}
if err = json.Unmarshal(data, &aux); err != nil {
return err
}
if h4 != nil {
var scope *Scope
if aux.Scope != "" {
scope, err = h4.GetScope(aux.Scope)
if err != nil {
return err
}
} else {
scope = nil
}
r.Scope = scope
} else {
return fmt.Errorf("H4 is not defined")
}
return nil
}
// AddCapability Add a capability to a role
func (r *Role) AddCapability(c Capability) error {
var roleURL bytes.Buffer
roleURL.WriteString("/roles/")
roleURL.WriteString(r.ID)
roleURL.WriteString("/capabilities")
jsonStr, err := json.Marshal(&c)
if err != nil {
return fmt.Errorf("Error Marshalling capabilty %s", err)
}
_, err = h4.Post(roleURL.String(), fmt.Sprintf("%s", jsonStr))
if err != nil {
return fmt.Errorf("POST error %s", err)
}
r.Capabilities = append(r.Capabilities, c)
return nil
}
// Capability Struct to handle capabilities in Roles
type Capability struct {
ID string `json:"id,omitempty"`
Scope *Scope `json:"app_scope_id,omitempty"`
Ability string `json:"ability,omitempty"`
Inherited bool `json:"inherited,omitempty"`
}
// MarshalJSON Converts Struct to JSON
func (c *Capability) MarshalJSON() ([]byte, error) {
type Alias Capability
return json.Marshal(&struct {
Scope string `json:"app_scope_id"`
*Alias
}{
Scope: c.Scope.ID,
Alias: (*Alias)(c),
})
}
// UnmarshalJSON Converts JSON to struct
func (c *Capability) UnmarshalJSON(data []byte) error {
var err error
type Alias Capability
aux := &struct {
Scope string `json:"app_scope_id"`
*Alias
}{
Alias: (*Alias)(c),
}
if err = json.Unmarshal(data, &aux); err != nil {
return err
}
if h4 != nil {
var scope *Scope
if aux.Scope != "" {
scope, err = h4.GetScope(aux.Scope)
if err != nil {
return err
}
} else {
scope = nil
}
c.Scope = scope
} else {
return fmt.Errorf("H4 is not defined")
}
return nil
}
// GetRoles Return all roles
func (h *H4) GetRoles() ([]*Role, error) {
getResp, err := h.Get("/roles")
if err != nil {
return nil, fmt.Errorf("GET error: %s", err.Error())
}
var jsonResp []*Role
fmt.Printf("%s\n", getResp)
err = json.Unmarshal(getResp, &jsonResp)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling JSON: %s / JSON: %s", err.Error(), getResp)
}
return jsonResp, nil
}
// GetRole Return one role by ID
func (h *H4) GetRole(id string) (*Role, error) {
getResp, err := h.Get(fmt.Sprintf("/roles/%s", id))
if err != nil {
return nil, fmt.Errorf("GET error: %s", err.Error())
}
var jsonResp *Role
//fmt.Printf("%s\n", getResp)
err = json.Unmarshal(getResp, &jsonResp)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling JSON: %s / JSON: %s", err.Error(), getResp)
}
return jsonResp, nil
}
// AddRole Add a new role
func (h *H4) AddRole(r *Role) error {
jsonStr, err := json.Marshal(&r)
if err != nil {
return fmt.Errorf("Error Marshalling role %s", err)
}
postResp, err := h.Post("/roles", fmt.Sprintf("%s", jsonStr))
if err != nil {
return fmt.Errorf("POST error: %s / POST: %s", err.Error(), jsonStr)
}
err = json.Unmarshal(postResp, &r)
if err != nil {
return fmt.Errorf("Error unmarshalling JSON: %s / JSON: %s", err.Error(), postResp)
}
return nil
}
// DeleteRole Delete a Role
func (h *H4) DeleteRole(roleID string) error {
err := h.Delete(fmt.Sprintf("/roles/%s", roleID), "")
if err != nil {
return fmt.Errorf("Error deleting role %s: %s", roleID, err)
}
return nil
}
// AddRoleToUser Add a role to a user
func (h *H4) AddRoleToUser(r *Role, u *User) error {
var userURL bytes.Buffer
userURL.WriteString("/users/")
userURL.WriteString(u.ID)
userURL.WriteString("/add_role")
rid := make(map[string]string)
rid["role_id"] = r.ID
jsonStr, err := json.Marshal(&rid)
if err != nil {
return fmt.Errorf("Error Marshalling role ID %s", err)
}
fmt.Printf("User ID: %s\n", u.ID)
fmt.Printf("Request: %s\n", jsonStr)
postResp, err := h.Put(userURL.String(), fmt.Sprintf("%s", jsonStr))
if err != nil {
return fmt.Errorf("PUT error %s", err)
}
fmt.Printf("Response: %s\n", postResp)
return nil
}