From d9df94bbff36ef1f943c9ab1995cd60831e572ec Mon Sep 17 00:00:00 2001 From: Kenneth Gillen Date: Wed, 18 Mar 2020 08:48:56 +0100 Subject: [PATCH 1/2] user: allow setting default group migrate code from openmicroscopy and fix flake8 see: https://github.com/ome/openmicroscopy/pull/5914/commits/8442eedafb964fec72ac3b6a8b08a67799bfcab8 --- src/omero/cli.py | 6 ++++++ src/omero/plugins/user.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/omero/cli.py b/src/omero/cli.py index 3c04502a6..bada1489f 100755 --- a/src/omero/cli.py +++ b/src/omero/cli.py @@ -2392,6 +2392,12 @@ def find_user(self, admin, id_or_name, fatal=False): return u.id.val, u + def set_users_default_group_by_user_id(self, admin, group, users): + import omero + for user in list(users): + admin.setDefaultGroup(omero.model.ExperimenterI(user, False), group) + self.ctx.out("Set the default group of user %s to group %s" % (user, group.id.val)) + def addusersbyid(self, admin, group, users): import omero for user in list(users): diff --git a/src/omero/plugins/user.py b/src/omero/plugins/user.py index a4f6a1144..7b0790d42 100755 --- a/src/omero/plugins/user.py +++ b/src/omero/plugins/user.py @@ -92,13 +92,17 @@ def _configure(self, parser): "--all", action="store_true", default=False, help="Include all users, including deactivated accounts") - joingroup = parser.add(sub, self.joingroup, "Join one or more groups") + joingroup = parser.add(sub, self.joingroup, + "Join one or more groups") self.add_id_name_arguments( joingroup, "user. Default to the current user") group = self.add_group_arguments(joingroup, " to join") group.add_argument( "--as-owner", action="store_true", default=False, 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") leavegroup = parser.add( sub, self.leavegroup, "Leave one or more groups") @@ -180,7 +184,8 @@ def password(self, args): except omero.SecurityViolation as sv: import traceback self.ctx.die(456, "SecurityViolation: Bad credentials") - self.ctx.dbg(traceback.format_exception(None, e, sys.exc_info()[2])) + self.ctx.dbg(traceback.format_exception(None, sv, + sys.exc_info()[2])) if args.username: try: @@ -342,12 +347,37 @@ 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: + # 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: + # Message to user that we can only + # set the default group if only one group + # is supplied as an argument. + self.ctx.out("--as-default requires a " + "single group ID to be supplied.") + # 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. 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]) def leavegroup(self, args): c = self.ctx.conn(args) From f3f73cf1614a05fe1ef388dbf32be99b714d200c Mon Sep 17 00:00:00 2001 From: Tom-TBT Date: Tue, 3 Feb 2026 15:07:11 +0100 Subject: [PATCH 2/2] 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)