-
Notifications
You must be signed in to change notification settings - Fork 14
/
user.go
164 lines (137 loc) · 5.72 KB
/
user.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
package bamboo
import (
"fmt"
"log"
"net/http"
)
// User contains information about a Bamboo user account
type User struct {
Name string `json:"name"`
FullName string `json:"fullName"`
Email string `json:"email"`
Permissions []string `json:"permissions,omitempty"`
}
type userPermissionsResponse struct {
Results []User `json:"results"`
}
// UserPermissionsList returns a list of users and their permissions for the given resource key in the service
func (p *Permissions) UserPermissionsList(opts PermissionsOpts) ([]User, *http.Response, error) {
if !knownResources[opts.Resource] {
return nil, nil, &simpleError{fmt.Sprintf("Unknown resource %s", opts.Resource)}
}
request, err := p.client.NewRequest(http.MethodGet, userPermissionsListURL(opts.Resource, opts.Key), nil)
if err != nil {
return nil, nil, err
}
data := userPermissionsResponse{}
response, err := p.client.Do(request, &data)
if err != nil {
return nil, response, err
}
if response.StatusCode == 401 {
return nil, response, &simpleError{"You must be an admin to access this information"}
} else if response.StatusCode != 200 {
return nil, response, &simpleError{fmt.Sprintf("Retrieving user information for resource %s in service %s returned %s", opts.Key, opts.Resource, response.Status)}
}
return data.Results, response, nil
}
// UserPermissions returns the permissions for the specified user on the given resource in the given service
func (p *Permissions) UserPermissions(username string, opts PermissionsOpts) (*User, *http.Response, error) {
if !knownResources[opts.Resource] {
return nil, nil, &simpleError{fmt.Sprintf("Unknown resource %s", opts.Resource)}
}
request, err := p.client.NewRequest(http.MethodGet, userPermissionsURL(opts.Resource, opts.Key, username), nil)
if err != nil {
return nil, nil, err
}
data := userPermissionsResponse{}
response, err := p.client.Do(request, &data)
if err != nil {
return nil, response, err
}
if response.StatusCode == 401 {
return nil, response, &simpleError{"You must be an admin to access this information"}
} else if response.StatusCode != 200 {
return nil, response, &simpleError{fmt.Sprintf("Retrieving user information for resource %s in service %s returned %s", opts.Key, opts.Resource, response.Status)}
}
if len(data.Results) == 0 {
response.StatusCode = http.StatusNoContent
return nil, response, nil
}
return &data.Results[0], response, nil
}
// SetUserPermissions sets the users permissions for the given project's plans to the passed in permissions array
func (p *Permissions) SetUserPermissions(username string, permissions []string, opts PermissionsOpts) (*http.Response, error) {
if !knownResources[opts.Resource] {
return nil, &simpleError{fmt.Sprintf("Unknown resource %s", opts.Resource)}
}
request, err := p.client.NewRequest(http.MethodPut, editUserPermissionsURL(opts.Resource, opts.Key, username), permissions)
if err != nil {
return nil, err
}
response, err := p.client.Do(request, nil)
if err != nil {
return response, err
}
switch response.StatusCode {
case 400:
return response, &simpleError{"User doesn't exist or one of the requested permission isn't supported for the given endpoint."}
case 401:
return response, &simpleError{"You must be an admin to access this information"}
case 304:
log.Println("User already had requested permissions and permission state hasn't been changed.")
case 204:
log.Println("User's permissions were granted.")
default:
return response, &simpleError{fmt.Sprintf("Server responded with unexpected return code %d", response.StatusCode)}
}
return response, nil
}
// RemoveUserPermissions removes the given permissions from the users permissions for the given project's plans
func (p *Permissions) RemoveUserPermissions(username string, permissions []string, opts PermissionsOpts) (*http.Response, error) {
if !knownResources[opts.Resource] {
return nil, &simpleError{fmt.Sprintf("Unknown resource %s", opts.Resource)}
}
request, err := p.client.NewRequest(http.MethodDelete, editUserPermissionsURL(opts.Resource, opts.Key, username), permissions)
if err != nil {
return nil, err
}
response, err := p.client.Do(request, nil)
if err != nil {
return response, err
}
switch response.StatusCode {
case 400:
return response, &simpleError{"User doesn't exist or one of the requested permission isn't supported for the given endpoint."}
case 401:
return response, &simpleError{"You must be an admin to access this information"}
case 304:
log.Println("User already lacked requested permissions and permission state hasn't been changed")
case 204:
log.Println("User's permissions were revoked.")
default:
return response, &simpleError{fmt.Sprintf("Server responded with unexpected return code %d", response.StatusCode)}
}
return response, nil
}
// AvailableUsersPermissionsList return a list of users which weren't explicitly granted any project plan permissions for the given project.
func (p *Permissions) AvailableUsersPermissionsList(opts PermissionsOpts) ([]User, *http.Response, error) {
if !knownResources[opts.Resource] {
return nil, nil, &simpleError{fmt.Sprintf("Unknown resource %s", opts.Resource)}
}
request, err := p.client.NewRequest(http.MethodGet, availableUsersURL(opts.Resource, opts.Key), nil)
if err != nil {
return nil, nil, err
}
data := userPermissionsResponse{}
response, err := p.client.Do(request, &data)
if err != nil {
return nil, response, err
}
if response.StatusCode == 401 {
return nil, response, &simpleError{"You must be an admin to access this information"}
} else if response.StatusCode != 200 {
return nil, response, &simpleError{fmt.Sprintf("Retrieving user information for project %s returned %s", opts.Key, response.Status)}
}
return data.Results, response, nil
}