@@ -237,52 +237,82 @@ def update_sign_users(self, directory_users, sign_connector: SignConnector, org_
237237 desired_groups = set ([self .get_primary_group (sign_user , self .sign_user_groups [org_name ]).name .lower ()])
238238 if not is_umg :
239239 desired_groups = set ([directory_user ['sign_roups' ][0 ].group_name .lower ()])
240- target_groups = set ([g .group_name .lower () for g in self .target_groups_by_org [org_name ]])
241-
242- groups_to_assign = []
243- for dg in desired_groups :
244- group_info = self .sign_groups [org_name ].get (dg )
245- if group_info is None :
246- raise AssertionException (f"'{ dg } ' isn't a valid Sign group" )
247-
248- assigned_group = assigned_groups .get (group_info .groupName .lower ())
249-
250- wants_group_admin = False
251- if is_umg :
252- wants_group_admin = directory_user ['is_group_admin' ]
253- else :
254- wants_group_admin = dg in directory_user ['admin_groups' ]
255240
256- change_group_admin = (assigned_group is None and
257- wants_group_admin ) or \
258- (assigned_group is not None and
259- assigned_group .isGroupAdmin is not wants_group_admin )
241+ groups_to_update = {}
242+ admin_groups = set ([g .group_name for g in directory_user ['admin_groups' ] if g .umapi_name == org_name ])
260243
261- if assigned_group is not None and not change_group_admin :
262- continue
244+ # identify groups to add for user
245+ groups_to_assign = desired_groups .difference (set (assigned_groups .keys ()))
246+ for group_name in groups_to_assign :
247+ group_info = self .sign_groups [org_name ].get (group_name )
248+ if group_info is None :
249+ raise AssertionException (f"'{ group_name } ' isn't a valid Sign group" )
263250
264- if assigned_group is None :
265- self .logger .info (f"Assigning group '{ group_info .groupId } ' to user { sign_user .email } " )
266- self .sign_users_group_updates .add (sign_user .email )
251+ is_group_admin = ((not is_umg and directory_user ['is_admin_group' ])
252+ or (group_name in admin_groups ))
253+ groups_to_update [group_name ] = UserGroupInfo (
254+ id = group_info .groupId ,
255+ name = group_info .groupName ,
256+ isGroupAdmin = is_group_admin ,
257+ isPrimaryGroup = False ,
258+ status = 'ACTIVE' ,
259+ )
260+ self .logger .info (f"Assigning group '{ group_info .groupName } ' to user { sign_user .email } " )
261+ if group_name in admin_groups :
262+ self .logger .info (f"Assigning group admin privileges to user { sign_user .email } for group '{ group_info .groupName } '" )
267263
268- admin_status = False if not wants_group_admin and assigned_group .isGroupAdmin is not wants_group_admin else True
264+ # identify groups to remove for user
265+ target_groups = set ([g .group_name .lower () for g in self .target_groups_by_org [org_name ]])
266+ assigned_group_names = set (assigned_groups .keys ())
267+ # first, get groups that are assigned but not in the desired list
268+ # then see what that has in common with overall target groups - this is the list
269+ # of groups to remove. non-targeted groups remain untouched
270+ remove_groups = target_groups .intersection (assigned_group_names .difference (desired_groups ))
271+ for group_name in remove_groups :
272+ # this should never happen but we need to check
273+ if group_name in groups_to_assign :
274+ raise AssertionException (f"Cannot remove group '{ group_name } ' because it is in the assignment list" )
275+ group_info = self .sign_groups [org_name ].get (group_name )
276+ if group_info is None :
277+ raise AssertionException (f"'{ group_name } ' isn't a valid Sign group" )
269278
270- if change_group_admin :
271- self .logger .info (f"Changing group Admin role for user '{ sign_user .email } ', status? { admin_status } " )
272- self .sign_users_role_updates .add (sign_user .email )
279+ assigned_group = assigned_groups .get (group_info .groupName .lower ())
273280
274- groups_to_assign . append ( UserGroupInfo (
281+ groups_to_update [ group_name ] = UserGroupInfo (
275282 id = group_info .groupId ,
276283 name = group_info .groupName ,
277- isGroupAdmin = admin_status ,
278- isPrimaryGroup = False ,
279- status = 'ACTIVE' ,
280- ))
284+ isGroupAdmin = assigned_group .isGroupAdmin ,
285+ isPrimaryGroup = assigned_group .isPrimaryGroup ,
286+ status = 'DELETED' ,
287+ )
288+ self .logger .info (f"Removing group '{ group_info .groupName } ' for user { sign_user .email } " )
289+
290+ # get a full list of groups the user is an admin for
291+ current_admin_groups = set ([g .name .lower () for g in assigned_groups .values () if g .isGroupAdmin ]).\
292+ union (set ([g .name .lower () for g in groups_to_update .values () if g .status == 'ACTIVE' and g .isGroupAdmin ]))
293+
294+ # if a user is group admin to any group they're not mapped to be admin for, then they
295+ # need to have their status removed
296+ non_admin_groups = current_admin_groups .difference (set ([g .group_name for g in directory_user ['admin_groups' ]]))
281297
282- if groups_to_assign :
283- group_update_data = UserGroupsInfo (groupInfoList = groups_to_assign )
298+ for group_name in non_admin_groups :
299+ if group_name in groups_to_update :
300+ groups_to_update [group_name ].isGroupAdmin = False
301+ else :
302+ group_info = assigned_groups .get (group_name )
303+ groups_to_update [group_name ] = UserGroupInfo (
304+ id = group_info .id ,
305+ name = group_info .name ,
306+ isGroupAdmin = False ,
307+ isPrimaryGroup = group_info .isPrimaryGroup ,
308+ status = 'DELETED' ,
309+ )
310+
311+
312+ if groups_to_update :
313+ group_update_data = UserGroupsInfo (groupInfoList = groups_to_update )
284314 user_groups_update_list .append ((sign_user .id , group_update_data ))
285-
315+
286316 sign_connector .update_users (users_update_list )
287317 sign_connector .update_user_groups (user_groups_update_list )
288318 self .sign_only_users_by_org [org_name ] = {}
0 commit comments