33 * * See LICENSE in the project root for license information.
44'''
55
6+ import re
67import urllib
78import constant
89from services .rest_api_service import RestApiService
@@ -12,28 +13,37 @@ class AADGraphService(object):
1213 def __init__ (self , tenant_id , access_token ):
1314 self .api_base_uri = constant .Resources .AADGraph + '/' + tenant_id + '/'
1415 self .access_token = access_token
16+ self .skip_token_re = re .compile ('(?<=skiptoken=).*' )
1517 self .rest_api_service = RestApiService ()
1618
1719 def get_service_principal (self ):
1820 url = self .api_base_uri + "servicePrincipals?api-version=1.6&$filter=appId eq '%s'" % constant .client_id
1921 app_content = self .rest_api_service .get_json (url , self .access_token )
20- return app_content ['value' ][ 0 ]
22+ return next ( iter ( app_content ['value' ]), None )
2123
2224 def delete_service_principal (self , service_principal_id ):
2325 version = '?api-version=1.6'
2426 url = self .api_base_uri + 'servicePrincipals/%s' % service_principal_id + version
2527 self .rest_api_service .delete (url , self .access_token )
2628
2729 def add_app_role_assignments (self , service_principal_id , service_principal_id_name ):
28- url = self .api_base_uri + 'users?api-version=1.6&$expand=appRoleAssignments'
29- users_content = self .rest_api_service .get_json (url , self .access_token )
30- users = users_content ['value' ]
31-
3230 count = 0
33- for user in users :
34- if all (a ['resourceId' ] != service_principal_id for a in user ['appRoleAssignments' ]):
35- self ._add_app_role_assignment (user , service_principal_id , service_principal_id_name )
36- count = count + 1
31+ url = self .api_base_uri + 'users?api-version=1.6&$expand=appRoleAssignments'
32+ skip_token = None
33+ while True :
34+ url2 = url + '&$skiptoken=' + skip_token if skip_token else url
35+ users_content = self .rest_api_service .get_json (url2 , self .access_token )
36+ users = users_content ['value' ]
37+ skip_token = self ._get_skip_token (users_content .get ('odata.nextLink' ))
38+ for user in users :
39+ if all (a ['resourceId' ] != service_principal_id for a in user ['appRoleAssignments' ]):
40+ try :
41+ self ._add_app_role_assignment (user , service_principal_id , service_principal_id_name )
42+ count = count + 1
43+ except :
44+ pass
45+ if skip_token == None :
46+ break ;
3747 return count
3848
3949 def _add_app_role_assignment (self , user , service_principal_id , service_principal_id_name ):
@@ -47,3 +57,10 @@ def _add_app_role_assignment(self, user, service_principal_id, service_principal
4757 }
4858 post_url = self .api_base_uri + 'users/%s' % user ['objectId' ] + '/appRoleAssignments?api-version=1.6'
4959 self .rest_api_service .post_json (post_url , self .access_token , data = app_role_assignment )
60+
61+ def _get_skip_token (self , nextlink ):
62+ if nextlink :
63+ matches = self .skip_token_re .findall (nextlink )
64+ if matches :
65+ return matches [0 ]
66+ return None
0 commit comments