From d9df94bbff36ef1f943c9ab1995cd60831e572ec Mon Sep 17 00:00:00 2001 From: Kenneth Gillen Date: Wed, 18 Mar 2020 08:48:56 +0100 Subject: [PATCH] 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)