From f3f73cf1614a05fe1ef388dbf32be99b714d200c Mon Sep 17 00:00:00 2001 From: Tom-TBT Date: Tue, 3 Feb 2026 15:07:11 +0100 Subject: [PATCH] ensure user -as-default only accepts a single provided group and always sets it as new default group --- src/omero/plugins/user.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/omero/plugins/user.py b/src/omero/plugins/user.py index 7b0790d42..76322f184 100755 --- a/src/omero/plugins/user.py +++ b/src/omero/plugins/user.py @@ -93,7 +93,7 @@ def _configure(self, parser): help="Include all users, including deactivated accounts") joingroup = parser.add(sub, self.joingroup, - "Join one or more groups") + "Join one or more groups and set default group") self.add_id_name_arguments( joingroup, "user. Default to the current user") group = self.add_group_arguments(joingroup, " to join") @@ -102,7 +102,9 @@ def _configure(self, parser): help="Join the group(s) as an owner") group.add_argument( "--as-default", action="store_true", default=False, - help="Sets the group as the user's default group") + help="Sets the group as the user's default group. " + "Requires a single group. Can be used to change " + "default group even if already a member.") leavegroup = parser.add( sub, self.leavegroup, "Leave one or more groups") @@ -346,20 +348,17 @@ def joingroup(self, args): uid, username = self.parse_userid(a, args) [gid, groups] = self.list_groups(a, args, use_context=False) - groups = self.filter_groups(groups, uid, args.as_owner, True) - # The list `groups` has members removed by filter_groups if the uid - # is already a member, so counting them here. - number_of_groups_passed = len(groups) - # so doing this logic before that group list filtering. if args.as_default: - # check if there's only one group defined - if number_of_groups_passed == 1: + # Check group count BEFORE filtering + # Otherwise we can't tell if multiple groups were passed + # for setting default group. + if len(groups) == 1: # If that's the case, set that to be the default group # which will happen in the rest of the function. pass else: - if number_of_groups_passed > 1: + if len(groups) > 1: # Message to user that we can only # set the default group if only one group # is supplied as an argument. @@ -368,16 +367,23 @@ def joingroup(self, args): # Exit the function making no changes. return None - # Performing the count and funtion exit above, so we don't even hit this - # if we don't need to, since it would print statements about group - # membership. + if args.as_default: + # Store the group to be set as default as filter_groups + # will remove it from the list if the user is already a member. + default_group = groups[0] + + groups = self.filter_groups(groups, uid, args.as_owner, True) for group in groups: if args.as_owner: self.addownersbyid(a, group, [uid]) else: self.addusersbyid(a, group, [uid]) - if args.as_default: - self.set_users_default_group_by_user_id(a, group, [uid]) + + if args.as_default: + # Here, we know only one group is being processed. + # Even if user was already a member of the group, + # we still want to set it as the default group. + self.set_users_default_group_by_user_id(a, default_group, [uid]) def leavegroup(self, args): c = self.ctx.conn(args)