1
+ from .rest import RestClient
2
+
3
+
4
+ class Roles (object ):
5
+
6
+ """Auth0 roles endpoints
7
+
8
+ Args:
9
+ domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
10
+
11
+ token (str): Management API v2 Token
12
+
13
+ telemetry (bool, optional): Enable or disable Telemetry
14
+ (defaults to True)
15
+ """
16
+
17
+ def __init__ (self , domain , token , telemetry = True ):
18
+ self .domain = domain
19
+ self .client = RestClient (jwt = token , telemetry = telemetry )
20
+
21
+ def _url (self , id = None ):
22
+ url = 'https://{}/api/v2/roles' .format (self .domain )
23
+ if id is not None :
24
+ return '{}/{}' .format (url , id )
25
+ return url
26
+
27
+ def list (self , page = 0 , per_page = 25 , include_totals = True , name_filter = None ):
28
+ """List or search roles.
29
+
30
+ Args:
31
+ page (int, optional): The result's page number (zero based).
32
+
33
+ per_page (int, optional): The amount of entries per page.
34
+
35
+ include_totals (bool, optional): True if the query summary is
36
+ to be included in the result, False otherwise.
37
+
38
+ name_filter (str, optional): A case-insensitive filter to apply
39
+ to search for roles by name
40
+
41
+ See: https://auth0.com/docs/api/management/v2#!/Roles/get_roles
42
+ """
43
+ params = {
44
+ 'per_page' : per_page ,
45
+ 'page' : page ,
46
+ 'include_totals' : str (include_totals ).lower (),
47
+ 'name_filter' : name_filter
48
+ }
49
+ return self .client .get (self ._url (), params = params )
50
+
51
+ def create (self , body ):
52
+ """Creates a new role.
53
+
54
+ Args:
55
+ body (dict): Please see: https://auth0.com/docs/api/v2#!/Roles/post_roles
56
+ """
57
+ return self .client .post (self ._url (), data = body )
58
+
59
+ def get (self , id ):
60
+ """Get a role.
61
+
62
+ Args:
63
+ id (str): The id of the role to retrieve.
64
+
65
+ See: https://auth0.com/docs/api/management/v2#!/Roles/get_roles_by_id
66
+ """
67
+
68
+ return self .client .get (self ._url (id ))
69
+
70
+ def delete (self , id ):
71
+ """Delete a role.
72
+
73
+ Args:
74
+ id (str): The id of the role to delete.
75
+
76
+ See: https://auth0.com/docs/api/management/v2#!/Roles/delete_roles_by_id
77
+ """
78
+ return self .client .delete (self ._url (id ))
79
+
80
+ def update (self , id , body ):
81
+ """Update a role with the attributes passed in 'body'
82
+
83
+ Args:
84
+ id (str): The id of the role to update.
85
+
86
+ body (dict): Please see: https://auth0.com/docs/api/management/v2#!/Roles/patch_roles_by_id
87
+ """
88
+ return self .client .patch (self ._url (id ), data = body )
89
+
90
+ def list_users (self , id , page = 0 , per_page = 25 , include_totals = True ):
91
+ """Lists the users that have been associated with a given role.
92
+
93
+ Args:
94
+ id (str): The role's id.
95
+
96
+ page (int, optional): The result's page number (zero based).
97
+
98
+ per_page (int, optional): The amount of entries per page.
99
+
100
+ include_totals (bool, optional): True if the query summary is
101
+ to be included in the result, False otherwise.
102
+
103
+ See https://auth0.com/docs/api/management/v2#!/Roles/get_role_user
104
+ """
105
+
106
+ params = {
107
+ 'per_page' : per_page ,
108
+ 'page' : page ,
109
+ 'include_totals' : str (include_totals ).lower ()
110
+ }
111
+ url = self ._url ('{}/users' .format (id ))
112
+ return self .client .get (url , params = params )
113
+
114
+ def add_users (self , id , users ):
115
+ """Assign users to a role.
116
+
117
+ Args:
118
+ id (str): The role's id.
119
+
120
+ users (list of str): A list of users ids to add to this role
121
+
122
+ See https://auth0.com/docs/api/management/v2#!/Roles/post_role_users
123
+ """
124
+ url = self ._url ('{}/users' .format (id ))
125
+ body = {'users' : users }
126
+ return self .client .post (url , data = body )
127
+
128
+ def list_permissions (self , id , page = 0 , per_page = 25 , include_totals = True ):
129
+ """Gets the permissions for a role.
130
+
131
+ Args:
132
+ id (str): The role's id.
133
+
134
+ page (int, optional): The result's page number (zero based).
135
+
136
+ per_page (int, optional): The amount of entries per page.
137
+
138
+ include_totals (bool, optional): True if the query summary is
139
+ to be included in the result, False otherwise.
140
+
141
+ See https://auth0.com/docs/api/management/v2#!/Roles/get_role_permission
142
+ """
143
+ params = {
144
+ 'per_page' : per_page ,
145
+ 'page' : page ,
146
+ 'include_totals' : str (include_totals ).lower ()
147
+ }
148
+ url = self ._url ('{}/permissions' .format (id ))
149
+ return self .client .get (url , params = params )
150
+
151
+ def remove_permissions (self , id , permissions ):
152
+ """Removes permissions from a role.
153
+
154
+ Args:
155
+ id (str): The role's id.
156
+
157
+ permissions (list of str): A list of permissions ids to unassociate from the role.
158
+
159
+ See https://auth0.com/docs/api/management/v2#!/Roles/delete_role_permission_assignment
160
+ """
161
+ url = self ._url ('{}/permissions' .format (id ))
162
+ body = {'permissions' : permissions }
163
+ return self .client .delete (url , data = body )
164
+
165
+
166
+ def add_permissions (self , id , permissions ):
167
+ """Adds permissions from a role.
168
+
169
+ Args:
170
+ id (str): The role's id.
171
+
172
+ permissions (list of str): A list of permissions ids to associate to the role.
173
+
174
+ See https://auth0.com/docs/api/management/v2#!/Roles/post_role_permission_assignment
175
+ """
176
+ url = self ._url ('{}/permissions' .format (id ))
177
+ body = {'permissions' : permissions }
178
+ return self .client .post (url , data = body )
0 commit comments