@@ -287,7 +287,6 @@ def get_or_create_user(self, payload):
287287 "familyName": family_name
288288 }
289289 '''
290- self .logger .info (f"Creating (or getting) user { payload ["id" ]} " )
291290 existing_user = self .get_user (payload ["id" ])
292291
293292 if existing_user is not None :
@@ -436,10 +435,38 @@ def add_user_to_workspace(self, user_id, is_workspace_admin=False):
436435
437436 raise Exception (f"{ response .status_code } { response .text } " )
438437
438+ def get_user_application_permissions (self , user_id , application_id ):
439+ self .logger .debug (f"Getting user { user_id } permissions for application { application_id } " )
440+ url = f"{ self .mas_api_url } /workspaces/{ self .mas_workspace_id } /applications/{ application_id } /users/{ user_id } "
441+ headers = {
442+ "Accept" : "application/json" ,
443+ "x-access-token" : self .superuser_auth_token
444+ }
445+ response = requests .get (
446+ url ,
447+ headers = headers ,
448+ verify = self .mas_api_url_ca_chain_file_path
449+ )
450+
451+ if response .status_code == 200 :
452+ return response .json ()
453+
454+ if response .status_code == 404 :
455+ return None
456+
457+ raise Exception (f"{ response .status_code } { response .text } " )
458+
439459 def set_user_application_permission (self , user_id , application_id , role ):
440460 '''
441- TODO: idempotency
461+ No-op if user already has a role established for the application. No attempt will be made to update the role if it differs.
442462 '''
463+
464+ existing_permissions = self .get_user_application_permissions (user_id , application_id )
465+
466+ if existing_permissions is not None :
467+ self .logger .info (f"User { user_id } already has permissions set for application { application_id } " )
468+ return None
469+
443470 self .logger .info (f"Setting user { user_id } role for { application_id } to { role } " )
444471 url = f"{ self .mas_api_url } /workspaces/{ self .mas_workspace_id } /applications/{ application_id } /users/{ user_id } "
445472 querystring = {}
@@ -457,7 +484,11 @@ def set_user_application_permission(self, user_id, application_id, role):
457484 params = querystring ,
458485 verify = self .mas_api_url_ca_chain_file_path
459486 )
460- return response .json ()
487+
488+ if response .status_code == 200 :
489+ return None
490+
491+ raise Exception (f"{ response .status_code } { response .text } " )
461492
462493 def check_user_sync (self , user_id , application_id , timeout_secs = 60 * 10 ):
463494 t_end = time .time () + timeout_secs
0 commit comments