Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user: allow setting default group #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/omero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 33 additions & 3 deletions src/omero/plugins/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down