-
Notifications
You must be signed in to change notification settings - Fork 31
/
permission_client.go
134 lines (110 loc) · 3.69 KB
/
permission_client.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
package stream_chat
import (
"context"
"errors"
"net/http"
"path"
"time"
)
type Permission struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Action string `json:"action"`
Owner bool `json:"owner"`
SameTeam bool `json:"same_team"`
Condition map[string]interface{} `json:"condition"`
Custom bool `json:"custom"`
Level string `json:"level"`
}
type Role struct {
Name string `json:"name"`
Custom bool `json:"custom"`
Scopes []string `json:"scoped"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type PermissionClient struct {
client *Client
}
// CreateRole creates a new role.
func (p *PermissionClient) CreateRole(ctx context.Context, name string) (*Response, error) {
if name == "" {
return nil, errors.New("name is required")
}
var resp Response
err := p.client.makeRequest(ctx, http.MethodPost, "roles", nil, map[string]interface{}{
"name": name,
}, &resp)
return &resp, err
}
// DeleteRole deletes an existing role by name.
func (p *PermissionClient) DeleteRole(ctx context.Context, name string) (*Response, error) {
if name == "" {
return nil, errors.New("name is required")
}
uri := path.Join("roles", name)
var resp Response
err := p.client.makeRequest(ctx, http.MethodDelete, uri, nil, nil, &resp)
return &resp, err
}
type RolesResponse struct {
Roles []*Role `json:"roles"`
Response
}
// ListRole returns all roles.
func (p *PermissionClient) ListRoles(ctx context.Context) (*RolesResponse, error) {
var r RolesResponse
err := p.client.makeRequest(ctx, http.MethodGet, "roles", nil, nil, &r)
return &r, err
}
// CreatePermission creates a new permission.
func (p *PermissionClient) CreatePermission(ctx context.Context, perm *Permission) (*Response, error) {
var resp Response
err := p.client.makeRequest(ctx, http.MethodPost, "permissions", nil, perm, &resp)
return &resp, err
}
type GetPermissionResponse struct {
Permission *Permission `json:"permission"`
Response
}
// GetPermission returns a permission by id.
func (p *PermissionClient) GetPermission(ctx context.Context, id string) (*GetPermissionResponse, error) {
if id == "" {
return nil, errors.New("id is required")
}
uri := path.Join("permissions", id)
var perm GetPermissionResponse
err := p.client.makeRequest(ctx, http.MethodGet, uri, nil, nil, &perm)
return &perm, err
}
// UpdatePermission updates an existing permission by id. Only custom permissions can be updated.
func (p *PermissionClient) UpdatePermission(ctx context.Context, id string, perm *Permission) (*Response, error) {
if id == "" {
return nil, errors.New("id is required")
}
uri := path.Join("permissions", id)
var resp Response
err := p.client.makeRequest(ctx, http.MethodPut, uri, nil, perm, &resp)
return &resp, err
}
type ListPermissionsResponse struct {
Permissions []*Permission `json:"permissions"`
Response
}
// ListPermissions returns all permissions of an app.
func (p *PermissionClient) ListPermissions(ctx context.Context) (*ListPermissionsResponse, error) {
var perm ListPermissionsResponse
err := p.client.makeRequest(ctx, http.MethodGet, "permissions", nil, nil, &perm)
return &perm, err
}
// DeletePermission deletes a permission by id.
func (p *PermissionClient) DeletePermission(ctx context.Context, id string) (*Response, error) {
if id == "" {
return nil, errors.New("id is required")
}
uri := path.Join("permissions", id)
var resp Response
err := p.client.makeRequest(ctx, http.MethodDelete, uri, nil, nil, &resp)
return &resp, err
}