-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathperms.go
More file actions
90 lines (67 loc) · 2.05 KB
/
perms.go
File metadata and controls
90 lines (67 loc) · 2.05 KB
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
package perms
import (
"encoding/json"
"fmt"
"github.com/deis/workflow/client/controller/api"
"github.com/deis/workflow/client/controller/client"
)
// List users that can access an app.
func List(c *client.Client, appID string) ([]string, error) {
body, err := c.BasicRequest("GET", fmt.Sprintf("/v1/apps/%s/perms/", appID), nil)
if err != nil {
return []string{}, err
}
var users api.PermsAppResponse
if err = json.Unmarshal([]byte(body), &users); err != nil {
return []string{}, err
}
return users.Users, nil
}
// ListAdmins lists administrators.
func ListAdmins(c *client.Client, results int) ([]string, int, error) {
body, count, err := c.LimitedRequest("/v1/admin/perms/", results)
if err != nil {
return []string{}, -1, err
}
var users []api.PermsRequest
if err = json.Unmarshal([]byte(body), &users); err != nil {
return []string{}, -1, err
}
usersList := []string{}
for _, user := range users {
usersList = append(usersList, user.Username)
}
return usersList, count, nil
}
// New adds a user to an app.
func New(c *client.Client, appID string, username string) error {
return doNew(c, fmt.Sprintf("/v1/apps/%s/perms/", appID), username)
}
// NewAdmin makes a user an administrator.
func NewAdmin(c *client.Client, username string) error {
return doNew(c, "/v1/admin/perms/", username)
}
func doNew(c *client.Client, u string, username string) error {
req := api.PermsRequest{Username: username}
reqBody, err := json.Marshal(req)
if err != nil {
return err
}
_, err = c.BasicRequest("POST", u, reqBody)
if err != nil {
return err
}
return nil
}
// Delete removes a user from an app.
func Delete(c *client.Client, appID string, username string) error {
return doDelete(c, fmt.Sprintf("/v1/apps/%s/perms/%s", appID, username))
}
// DeleteAdmin removes administrative privilages from a user.
func DeleteAdmin(c *client.Client, username string) error {
return doDelete(c, fmt.Sprintf("/v1/admin/perms/%s", username))
}
func doDelete(c *client.Client, u string) error {
_, err := c.BasicRequest("DELETE", u, nil)
return err
}