Skip to content

Commit a42edd4

Browse files
committed
feat(perms): add object perm
1 parent 6a60781 commit a42edd4

3 files changed

Lines changed: 79 additions & 160 deletions

File tree

api/perms.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package api
22

3-
// PermsAppResponse is the definition of GET /v2/apps/<app id>/perms/.
4-
type PermsAppResponse struct {
5-
Users []string `json:"users"`
3+
// PermCodeResponse is the definition of GET /v2/perms/codes/.
4+
type PermCodeResponse struct {
5+
Codename string `json:"codename"`
6+
Description string `json:"description"`
67
}
78

8-
// PermsRequest is the definition of a requst on /perms/.
9-
type PermsRequest struct {
9+
// UserPermResponse is the definition of GET /v2/perms/rules/.
10+
type UserPermResponse struct {
11+
ID uint64 `json:"id"`
12+
Codename string `json:"codename"`
13+
Uniqueid string `json:"uniqueid"`
14+
Username string `json:"username"`
15+
}
16+
17+
// UserPermRequest is the definition of a requst on /v2/perms/rules/.
18+
type UserPermRequest struct {
19+
Codename string `json:"codename"`
20+
Uniqueid string `json:"uniqueid"`
1021
Username string `json:"username"`
1122
}

perms/perms.go

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,59 @@ import (
99
"github.com/drycc/controller-sdk-go/api"
1010
)
1111

12-
// List users that can access an app.
13-
func List(c *drycc.Client, appID string) ([]string, error) {
14-
res, reqErr := c.Request("GET", fmt.Sprintf("/v2/apps/%s/perms/", appID), nil)
12+
// List Codename
13+
func Codes(c *drycc.Client, results int) ([]api.PermCodeResponse, int, error) {
14+
body, count, reqErr := c.LimitedRequest("/v2/perms/codes/", results)
15+
1516
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
16-
return []string{}, reqErr
17+
return []api.PermCodeResponse{}, count, reqErr
1718
}
18-
defer res.Body.Close()
1919

20-
var users api.PermsAppResponse
21-
if err := json.NewDecoder(res.Body).Decode(&users); err != nil {
22-
return []string{}, err
20+
var codenames []api.PermCodeResponse
21+
if err := json.Unmarshal([]byte(body), &codenames); err != nil {
22+
return []api.PermCodeResponse{}, count, err
2323
}
2424

25-
return users.Users, reqErr
25+
return codenames, count, reqErr
2626
}
2727

28-
// ListAdmins lists drycc platform administrators.
29-
func ListAdmins(c *drycc.Client, results int) ([]string, int, error) {
30-
body, count, reqErr := c.LimitedRequest("/v2/admin/perms/", results)
28+
// List UserPerm
29+
func List(c *drycc.Client, codename string, results int) ([]api.UserPermResponse, int, error) {
30+
u := fmt.Sprintf("/v2/perms/rules/?codename=%s", codename)
31+
body, count, reqErr := c.LimitedRequest(u, results)
3132

3233
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
33-
return []string{}, -1, reqErr
34-
}
35-
36-
var users []api.PermsRequest
37-
if err := json.Unmarshal([]byte(body), &users); err != nil {
38-
return []string{}, -1, err
34+
return []api.UserPermResponse{}, count, reqErr
3935
}
4036

41-
usersList := []string{}
42-
43-
for _, user := range users {
44-
usersList = append(usersList, user.Username)
37+
var userPerm []api.UserPermResponse
38+
if err := json.Unmarshal([]byte(body), &userPerm); err != nil {
39+
return []api.UserPermResponse{}, count, err
4540
}
4641

47-
return usersList, count, reqErr
42+
return userPerm, count, reqErr
4843
}
4944

50-
// New gives a user access to an app.
51-
func New(c *drycc.Client, appID string, username string) error {
52-
return doNew(c, fmt.Sprintf("/v2/apps/%s/perms/", appID), username)
53-
}
54-
55-
// NewAdmin makes a user an administrator.
56-
func NewAdmin(c *drycc.Client, username string) error {
57-
return doNew(c, "/v2/admin/perms/", username)
58-
}
59-
60-
func doNew(c *drycc.Client, u string, username string) error {
61-
req := api.PermsRequest{Username: username}
62-
45+
// Create a UserPerm
46+
func Create(c *drycc.Client, codename, uniqueid, username string) error {
47+
req := api.UserPermRequest{Codename: codename, Uniqueid: uniqueid, Username: username}
6348
reqBody, err := json.Marshal(req)
6449

6550
if err != nil {
6651
return err
6752
}
6853

69-
res, err := c.Request("POST", u, reqBody)
54+
res, err := c.Request("POST", "/v2/perms/rules/", reqBody)
7055
if err == nil {
7156
res.Body.Close()
7257
}
7358

7459
return err
7560
}
7661

77-
// Delete removes a user from an app.
78-
func Delete(c *drycc.Client, appID string, username string) error {
79-
return doDelete(c, fmt.Sprintf("/v2/apps/%s/perms/%s", appID, username))
80-
}
81-
82-
// DeleteAdmin removes administrative privileges from a user.
83-
func DeleteAdmin(c *drycc.Client, username string) error {
84-
return doDelete(c, fmt.Sprintf("/v2/admin/perms/%s", username))
85-
}
86-
87-
func doDelete(c *drycc.Client, u string) error {
88-
res, err := c.Request("DELETE", u, nil)
62+
// Delete a UserPerm.
63+
func Delete(c *drycc.Client, userPermID string) error {
64+
res, err := c.Request("DELETE", fmt.Sprintf("/v2/perms/rules/%s/", userPermID), nil)
8965
if err == nil {
9066
res.Body.Close()
9167
}

perms/perms_test.go

Lines changed: 38 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,45 @@ import (
99
"testing"
1010

1111
drycc "github.com/drycc/controller-sdk-go"
12+
"github.com/drycc/controller-sdk-go/api"
1213
)
1314

14-
const adminFixture string = `
15+
const codenamesFixture string = `
1516
{
16-
"count": 1,
17-
"next": null,
18-
"previous": null,
19-
"results": [
20-
{
21-
"username": "test",
22-
"is_superuser": true
23-
},
24-
{
25-
"username": "foo",
26-
"is_superuser": true
27-
}
28-
]
17+
"results": [
18+
{"codename": "use_app", "description": "Can use app"},
19+
{"codename": "use_cert", "description": "Can use cert"}
20+
],
21+
"count": 2
2922
}`
3023

31-
const appPermsFixture string = `
24+
const listUserPermFixture string = `
3225
{
33-
"users": [
34-
"test",
35-
"testing"
36-
]
26+
"results": [
27+
{"id": 1, "codename": "use_app", "uniqueid": "autotest-app", "username": "foo"},
28+
{"id": 2, "codename": "use_cert", "uniqueid": "autotest-cert-1", "username": "foo"}
29+
],
30+
"count": 2
3731
}`
3832

39-
const adminCreateExpected = `{"username":"test"}`
40-
const appCreateExpected = `{"username":"foo"}`
33+
const createUserPermExpected = `{"codename":"use_app","uniqueid":"autotest","username":"foo"}`
4134

4235
type fakeHTTPServer struct{}
4336

4437
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
4538
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
4639

47-
if req.URL.Path == "/v2/admin/perms/" && req.Method == "GET" {
48-
res.Write([]byte(adminFixture))
40+
if req.URL.Path == "/v2/perms/codes/" && req.Method == "GET" {
41+
res.Write([]byte(codenamesFixture))
4942
return
5043
}
5144

52-
if req.URL.Path == "/v2/admin/perms/" && req.Method == "POST" {
45+
if req.URL.Path == "/v2/perms/rules/" && req.Method == "GET" {
46+
res.Write([]byte(listUserPermFixture))
47+
return
48+
}
49+
50+
if req.URL.Path == "/v2/perms/rules/" && req.Method == "POST" {
5351
body, err := io.ReadAll(req.Body)
5452

5553
if err != nil {
@@ -58,8 +56,8 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
5856
res.Write(nil)
5957
}
6058

61-
if string(body) != adminCreateExpected {
62-
fmt.Printf("Expected '%s', Got '%s'\n", adminCreateExpected, body)
59+
if string(body) != createUserPermExpected {
60+
fmt.Printf("Expected '%s', Got '%s'\n", createUserPermExpected, body)
6361
res.WriteHeader(http.StatusInternalServerError)
6462
res.Write(nil)
6563
return
@@ -70,55 +68,23 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
7068
return
7169
}
7270

73-
if req.URL.Path == "/v2/admin/perms/test" && req.Method == "DELETE" {
74-
res.WriteHeader(http.StatusNoContent)
75-
res.Write(nil)
76-
return
77-
}
78-
79-
if req.URL.Path == "/v2/apps/example-go/perms/" && req.Method == "GET" {
80-
res.Write([]byte(appPermsFixture))
81-
return
82-
}
83-
84-
if req.URL.Path == "/v2/apps/example-go/perms/foo" && req.Method == "DELETE" {
71+
if req.URL.Path == "/v2/perms/rules/1/" && req.Method == "DELETE" {
8572
res.WriteHeader(http.StatusNoContent)
8673
res.Write(nil)
8774
return
8875
}
8976

90-
if req.URL.Path == "/v2/apps/example-go/perms/" && req.Method == "POST" {
91-
body, err := io.ReadAll(req.Body)
92-
93-
if err != nil {
94-
fmt.Println(err)
95-
res.WriteHeader(http.StatusInternalServerError)
96-
res.Write(nil)
97-
}
98-
99-
if string(body) != appCreateExpected {
100-
fmt.Printf("Expected '%s', Got '%s'\n", appCreateExpected, body)
101-
res.WriteHeader(http.StatusInternalServerError)
102-
res.Write(nil)
103-
return
104-
}
105-
106-
res.WriteHeader(http.StatusCreated)
107-
res.Write(nil)
108-
return
109-
}
110-
11177
fmt.Printf("Unrecognized URL %s\n", req.URL)
11278
res.WriteHeader(http.StatusNotFound)
11379
res.Write(nil)
11480
}
11581

116-
func TestList(t *testing.T) {
82+
func TestNames(t *testing.T) {
11783
t.Parallel()
11884

119-
expected := []string{
120-
"test",
121-
"testing",
85+
expected := []api.PermCodeResponse{
86+
{Codename: "use_app", Description: "Can use app"},
87+
{Codename: "use_cert", Description: "Can use cert"},
12288
}
12389

12490
handler := fakeHTTPServer{}
@@ -130,7 +96,7 @@ func TestList(t *testing.T) {
13096
t.Fatal(err)
13197
}
13298

133-
actual, err := List(drycc, "example-go")
99+
actual, _, err := Codes(drycc, 300)
134100

135101
if err != nil {
136102
t.Fatal(err)
@@ -141,12 +107,12 @@ func TestList(t *testing.T) {
141107
}
142108
}
143109

144-
func TestListAdmins(t *testing.T) {
110+
func TestList(t *testing.T) {
145111
t.Parallel()
146112

147-
expected := []string{
148-
"test",
149-
"foo",
113+
expected := []api.UserPermResponse{
114+
{ID: 1, Codename: "use_app", Uniqueid: "autotest-app", Username: "foo"},
115+
{ID: 2, Codename: "use_cert", Uniqueid: "autotest-cert-1", Username: "foo"},
150116
}
151117

152118
handler := fakeHTTPServer{}
@@ -158,7 +124,7 @@ func TestListAdmins(t *testing.T) {
158124
t.Fatal(err)
159125
}
160126

161-
actual, _, err := ListAdmins(drycc, 100)
127+
actual, _, err := List(drycc, "", 300)
162128

163129
if err != nil {
164130
t.Fatal(err)
@@ -169,7 +135,7 @@ func TestListAdmins(t *testing.T) {
169135
}
170136
}
171137

172-
func TestNew(t *testing.T) {
138+
func TestCreate(t *testing.T) {
173139
t.Parallel()
174140

175141
handler := fakeHTTPServer{}
@@ -181,24 +147,7 @@ func TestNew(t *testing.T) {
181147
t.Fatal(err)
182148
}
183149

184-
if err = New(drycc, "example-go", "foo"); err != nil {
185-
t.Fatal(err)
186-
}
187-
}
188-
189-
func TestNewAdmin(t *testing.T) {
190-
t.Parallel()
191-
192-
handler := fakeHTTPServer{}
193-
server := httptest.NewServer(handler)
194-
defer server.Close()
195-
196-
drycc, err := drycc.New(false, server.URL, "abc")
197-
if err != nil {
198-
t.Fatal(err)
199-
}
200-
201-
if err = NewAdmin(drycc, "test"); err != nil {
150+
if err = Create(drycc, "use_app", "autotest", "foo"); err != nil {
202151
t.Fatal(err)
203152
}
204153
}
@@ -215,24 +164,7 @@ func TestDelete(t *testing.T) {
215164
t.Fatal(err)
216165
}
217166

218-
if err = Delete(drycc, "example-go", "foo"); err != nil {
219-
t.Fatal(err)
220-
}
221-
}
222-
223-
func TestDeleteAdmin(t *testing.T) {
224-
t.Parallel()
225-
226-
handler := fakeHTTPServer{}
227-
server := httptest.NewServer(handler)
228-
defer server.Close()
229-
230-
drycc, err := drycc.New(false, server.URL, "abc")
231-
if err != nil {
232-
t.Fatal(err)
233-
}
234-
235-
if err = DeleteAdmin(drycc, "test"); err != nil {
167+
if err = Delete(drycc, "1"); err != nil {
236168
t.Fatal(err)
237169
}
238170
}

0 commit comments

Comments
 (0)