forked from charlires/go-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
136 lines (115 loc) · 4.4 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
package management
// Role is used to assign roles to a User.
type Role struct {
// A unique ID for the role.
ID *string `json:"id,omitempty"`
// The name of the role created.
Name *string `json:"name,omitempty"`
// A description of the role created.
Description *string `json:"description,omitempty"`
}
// RoleList holds a list of Roles.
type RoleList struct {
List
Roles []*Role `json:"roles"`
}
// Permission is granted to a Role.
type Permission struct {
// The resource server that the permission is attached to.
ResourceServerIdentifier *string `json:"resource_server_identifier,omitempty"`
// The name of the resource server.
ResourceServerName *string `json:"resource_server_name,omitempty"`
// The name of the permission.
Name *string `json:"permission_name,omitempty"`
// The description of the permission.
Description *string `json:"description,omitempty"`
}
// PermissionList holds a list of Permissions.
type PermissionList struct {
List
Permissions []*Permission `json:"permissions"`
}
// RoleManager manages Auth0 Role resources.
type RoleManager struct {
*Management
}
func newRoleManager(m *Management) *RoleManager {
return &RoleManager{m}
}
// Create a new role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/post_roles
func (m *RoleManager) Create(r *Role, opts ...RequestOption) error {
return m.Request("POST", m.URI("roles"), r, opts...)
}
// Retrieve a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/get_roles_by_id
func (m *RoleManager) Read(id string, opts ...RequestOption) (r *Role, err error) {
err = m.Request("GET", m.URI("roles", id), &r, opts...)
return
}
// Update a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/patch_roles_by_id
func (m *RoleManager) Update(id string, r *Role, opts ...RequestOption) (err error) {
return m.Request("PATCH", m.URI("roles", id), r, opts...)
}
// Delete a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/delete_roles_by_id
func (m *RoleManager) Delete(id string, opts ...RequestOption) (err error) {
// Deleting a role results in a 200 status code instead of 204 which
// triggers decoding of the response payload.
//
// In order to avoid Unmarshal(nil) errors, we pass an empty &Role{}.
return m.Request("DELETE", m.URI("roles", id), &Role{}, opts...)
}
// List all roles that can be assigned to users or groups.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/get_roles
func (m *RoleManager) List(opts ...RequestOption) (r *RoleList, err error) {
err = m.Request("GET", m.URI("roles"), &r, applyListDefaults(opts))
return
}
// AssignUsers assigns users to a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/post_role_users
func (m *RoleManager) AssignUsers(id string, users []*User, opts ...RequestOption) error {
u := make(map[string][]*string)
u["users"] = make([]*string, len(users))
for i, user := range users {
u["users"][i] = user.ID
}
return m.Request("POST", m.URI("roles", id, "users"), &u, opts...)
}
// Users retrieves all users associated with a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/get_role_user
func (m *RoleManager) Users(id string, opts ...RequestOption) (u *UserList, err error) {
err = m.Request("GET", m.URI("roles", id, "users"), &u, applyListDefaults(opts))
return
}
// AssociatePermissions associates permissions to a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/post_role_permission_assignment
func (m *RoleManager) AssociatePermissions(id string, permissions []*Permission, opts ...RequestOption) error {
p := make(map[string][]*Permission)
p["permissions"] = permissions
return m.Request("POST", m.URI("roles", id, "permissions"), &p, opts...)
}
// Permissions retrieves all permissions granted by a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/get_role_permission
func (m *RoleManager) Permissions(id string, opts ...RequestOption) (p *PermissionList, err error) {
err = m.Request("GET", m.URI("roles", id, "permissions"), &p, applyListDefaults(opts))
return
}
// RemovePermissions removes permissions associated to a role.
//
// See: https://auth0.com/docs/api/management/v2#!/Roles/delete_role_permission_assignment
func (m *RoleManager) RemovePermissions(id string, permissions []*Permission, opts ...RequestOption) error {
p := make(map[string][]*Permission)
p["permissions"] = permissions
return m.Request("DELETE", m.URI("roles", id, "permissions"), &p, opts...)
}