Package omero :: Package plugins :: Module group
[hide private]
[frames] | no frames]

Source Code for Module omero.plugins.group

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  """ 
  4     Group administration plugin 
  5   
  6     Copyright 2009 Glencoe Software, Inc. All rights reserved. 
  7     Use is subject to license terms supplied in LICENSE.txt 
  8   
  9  """ 
 10   
 11  import sys 
 12   
 13  from omero.cli import UserGroupControl, CLI, ExceptionHandler 
 14   
 15  HELP = """Group administration methods""" 
 16  defaultperms = { 
 17      'private': 'rw----', 
 18      'read-only': 'rwr---', 
 19      'read-annotate': 'rwra--'} 
 20   
 21   
22 -class GroupControl(UserGroupControl):
23
24 - def _configure(self, parser):
25 26 self.exc = ExceptionHandler() 27 28 PERM_TXT = """ 29 30 Group permissions come in several styles: 31 32 * private (rw----) [DEFAULT] 33 * read-only (rwr---) 34 * read-annotate (rwra--) [Previously known as 'collaborative'] 35 36 In private groups, only group and system administrators will be able 37 to view someone else's data. In read-only groups, other group members 38 can see data but not annotate or modify it. In read-annotate groups, 39 annotation is permitted by group members. 40 41 More information is available at: 42 https://www.openmicroscopy.org/site/support/omero4/sysadmins/\ 43 server-permissions.html 44 """ 45 46 parser.add_login_arguments() 47 sub = parser.sub() 48 add = parser.add(sub, self.add, 49 "Add a new group with given permissions " + PERM_TXT) 50 add.add_argument( 51 "--ignore-existing", action="store_true", default=False, 52 help="Do not fail if user already exists") 53 add.add_argument("name", help="Name of the group") 54 self.add_permissions_arguments(add) 55 56 perms = parser.add(sub, self.perms, 57 "Modify a group's permissions " + PERM_TXT) 58 self.add_group_arguments(perms) 59 self.add_permissions_arguments(perms) 60 61 list = parser.add(sub, self.list, "List current groups") 62 printgroup = list.add_mutually_exclusive_group() 63 printgroup.add_argument( 64 "--count", action="store_true", default=True, 65 help="Print count of all users and owners (default)") 66 printgroup.add_argument( 67 "--long", action="store_true", default=False, 68 help="Print comma-separated list of all users and owners") 69 sortgroup = list.add_mutually_exclusive_group() 70 sortgroup.add_argument( 71 "--sort-by-id", action="store_true", default=True, 72 help="Sort groups by ID (default)") 73 sortgroup.add_argument( 74 "--sort-by-name", action="store_true", default=False, 75 help="Sort groups by name") 76 77 copyusers = parser.add(sub, self.copyusers, "Copy the users of one" 78 " group to another group") 79 copyusers.add_argument("from_group", help="ID or name of the source" 80 " group whose users will be copied") 81 copyusers.add_argument("to_group", help="ID or name of the target" 82 " group which will have new users added") 83 copyusers.add_argument( 84 "--as-owner", action="store_true", 85 default=False, help="Copy the group owners only") 86 87 adduser = parser.add(sub, self.adduser, 88 "Add one or more users to a group") 89 self.add_group_arguments(adduser) 90 group = self.add_user_arguments(adduser, "add to the group") 91 group.add_argument("--as-owner", action="store_true", default=False, 92 help="Add the users as owners of the group") 93 94 removeuser = parser.add(sub, self.removeuser, 95 "Remove one or more users from a group") 96 self.add_group_arguments(removeuser) 97 group = self.add_user_arguments(removeuser, "remove from the group") 98 group.add_argument("--as-owner", action="store_true", default=False, 99 help="Remove the users from the group owner list") 100 101 for x in (add, perms, list, copyusers, adduser, removeuser): 102 x.add_login_arguments()
103
104 - def add_permissions_arguments(self, parser):
105 group = parser.add_mutually_exclusive_group() 106 group.add_argument( 107 "--perms", help="Group permissions set as string, e.g. 'rw----' ") 108 group.add_argument( 109 "--type", help="Group permissions set symbolically", 110 default="private", choices=defaultperms.keys())
111
112 - def add_group_arguments(self, parser):
113 group = parser.add_mutually_exclusive_group() 114 group.add_argument("--id", help="ID of the group") 115 group.add_argument("--name", help="Name of the group")
116
117 - def add_user_arguments(self, parser, action="join", owner_desc=""):
118 group = parser.add_argument_group('User arguments') 119 group.add_argument("user_id_or_name", metavar="user", nargs="*", 120 help="ID or name of the user(s) to %s" % action) 121 group.add_argument("--user-id", metavar="user", nargs="+", 122 help="ID of the user(s) to %s" % action) 123 group.add_argument("--user-name", metavar="user", nargs="+", 124 help="Name of the user(s) to %s" % action) 125 return group
126
127 - def parse_perms(self, args):
128 from omero_model_PermissionsI import PermissionsI as Perms 129 perms = getattr(args, "perms", None) 130 if not perms: 131 perms = defaultperms[args.type] 132 try: 133 return Perms(perms) 134 except ValueError, ve: 135 self.ctx.die(505, str(ve))
136
137 - def add(self, args):
138 139 import omero 140 from omero.rtypes import rstring 141 from omero_model_ExperimenterGroupI import ExperimenterGroupI as Grp 142 143 perms = self.parse_perms(args) 144 c = self.ctx.conn(args) 145 g = Grp() 146 g.name = rstring(args.name) 147 g.details.permissions = perms 148 admin = c.getSession().getAdminService() 149 try: 150 grp = admin.lookupGroup(args.name) 151 if grp: 152 if args.ignore_existing: 153 self.ctx.out("Group exists: %s (id=%s)" 154 % (args.name, grp.id.val)) 155 return 156 else: 157 self.ctx.die(3, "Group exists: %s (id=%s)" 158 % (args.name, grp.id.val)) 159 except omero.ApiUsageException: 160 pass # Apparently no such group exists 161 162 try: 163 id = admin.createGroup(g) 164 self.ctx.out("Added group %s (id=%s) with permissions %s" 165 % (args.name, id, perms)) 166 except omero.ValidationException, ve: 167 # Possible, though unlikely after previous check 168 if self.exc.is_constraint_violation(ve): 169 self.ctx.die(66, "Group already exists: %s" % args.name) 170 else: 171 self.ctx.die(67, "Unknown ValidationException: %s" 172 % ve.message) 173 except omero.SecurityViolation, se: 174 self.ctx.die(68, "Security violation: %s" % se.message) 175 except omero.ServerError, se: 176 self.ctx.die(4, "%s: %s" % (type(se), se.message))
177
178 - def perms(self, args):
179 180 import omero 181 from omero_model_ExperimenterGroupI import ExperimenterGroupI as Grp 182 183 perms = self.parse_perms(args) 184 c = self.ctx.conn(args) 185 a = c.sf.getAdminService() 186 187 gid, g = self.parse_groupid(a, args) 188 189 old_perms = str(g.details.permissions) 190 if old_perms == str(perms): 191 self.ctx.out("Permissions for group %s (id=%s) already %s" 192 % (g.name.val, gid, perms)) 193 else: 194 try: 195 a.changePermissions(Grp(gid, False), perms) 196 self.ctx.out("Changed permissions for group %s (id=%s) to %s" 197 % (g.name.val, gid, perms)) 198 except omero.GroupSecurityViolation: 199 import traceback 200 self.ctx.dbg(traceback.format_exc()) 201 self.ctx.die(504, "Cannot change permissions for group %s" 202 " (id=%s) to %s" % (g.name.val, gid, perms))
203
204 - def list(self, args):
205 c = self.ctx.conn(args) 206 groups = c.sf.getAdminService().lookupGroups() 207 from omero.util.text import TableBuilder 208 209 # Sort groups 210 if args.sort_by_name: 211 groups.sort(key=lambda x: x.name.val) 212 elif args.sort_by_id: 213 groups.sort(key=lambda x: x.id.val) 214 215 if args.long: 216 tb = TableBuilder("id", "name", "perms", "owner ids", 217 "member ids") 218 else: 219 tb = TableBuilder("id", "name", "perms", "# of owners", 220 "# of members") 221 for group in groups: 222 row = [group.id.val, group.name.val, 223 str(group.details.permissions)] 224 ownerids = self.getownerids(group) 225 memberids = self.getmemberids(group) 226 if args.long: 227 row.append(",".join(sorted([str(x) for x in ownerids]))) 228 row.append(",".join(sorted([str(x) for x in memberids]))) 229 else: 230 row.append(len(ownerids)) 231 row.append(len(memberids)) 232 tb.row(*tuple(row)) 233 self.ctx.out(str(tb.build()))
234
235 - def parse_groupid(self, a, args):
236 if args.id: 237 group = getattr(args, "id", None) 238 return self.find_group_by_id(a, group, fatal=True) 239 elif args.name: 240 group = getattr(args, "name", None) 241 return self.find_group_by_name(a, group, fatal=True) 242 else: 243 self.error_no_input_group(fatal=True)
244
245 - def list_users(self, a, args):
246 247 # Check input arguments 248 if not args.user_id_or_name and not args.user_id \ 249 and not args.user_name: 250 self.error_no_input_user(fatal=True) 251 252 # Retrieve groups by id or name 253 uid_list = [] 254 if args.user_id_or_name: 255 for user in args.user_id_or_name: 256 [uid, u] = self.find_user(a, user, fatal=False) 257 if uid is not None: 258 uid_list.append(uid) 259 260 if args.user_id: 261 for user_id in args.user_id: 262 [uid, u] = self.find_user_by_id(a, user_id, fatal=False) 263 if uid is not None: 264 uid_list.append(uid) 265 266 if args.user_name: 267 for user_name in args.user_name: 268 [uid, u] = self.find_user_by_name(a, user_name, fatal=False) 269 if uid is not None: 270 uid_list.append(uid) 271 272 if not uid_list: 273 self.error_no_user_found(fatal=True) 274 275 return uid_list
276
277 - def filter_users(self, uids, group, owner=False, join=True):
278 279 if owner: 280 uid_list = self.getownerids(group) 281 relation = "owner of" 282 else: 283 uid_list = self.getuserids(group) 284 relation = "in" 285 286 for uid in list(uids): 287 if join: 288 if uid in uid_list: 289 self.ctx.out("%s is already %s group %s" 290 % (uid, relation, group.id.val)) 291 uids.remove(uid) 292 else: 293 if uid not in uid_list: 294 self.ctx.out("%s is not %s group %s" 295 % (uid, relation, group.id.val)) 296 uids.remove(uid) 297 return uids
298
299 - def copyusers(self, args):
300 c = self.ctx.conn(args) 301 a = c.sf.getAdminService() 302 f_gid, f_grp = self.find_group(a, args.from_group, fatal=True) 303 t_gid, t_grp = self.find_group(a, args.to_group, fatal=True) 304 305 if args.as_owner: 306 uids = self.getownerids(f_grp) 307 else: 308 uids = self.getuserids(f_grp) 309 uids = self.filter_users(uids, t_grp, args.as_owner, True) 310 311 if args.as_owner: 312 self.addownersbyid(a, t_grp, uids) 313 self.ctx.out("Owners of %s copied to %s" 314 % (args.from_group, args.to_group)) 315 else: 316 self.addusersbyid(a, t_grp, uids) 317 self.ctx.out("Users of %s copied to %s" 318 % (args.from_group, args.to_group))
319
320 - def adduser(self, args):
321 c = self.ctx.conn(args) 322 a = c.sf.getAdminService() 323 group = self.parse_groupid(a, args)[1] 324 uids = self.list_users(a, args) 325 uids = self.filter_users(uids, group, args.as_owner, True) 326 327 if args.as_owner: 328 self.addownersbyid(a, group, uids) 329 else: 330 self.addusersbyid(a, group, uids)
331
332 - def removeuser(self, args):
333 c = self.ctx.conn(args) 334 a = c.sf.getAdminService() 335 group = self.parse_groupid(a, args)[1] 336 uids = self.list_users(a, args) 337 uids = self.filter_users(uids, group, args.as_owner, False) 338 339 if args.as_owner: 340 self.removeownersbyid(a, group, uids) 341 else: 342 self.removeusersbyid(a, group, uids)
343 344 try: 345 register("group", GroupControl, HELP) 346 except NameError: 347 if __name__ == "__main__": 348 cli = CLI() 349 cli.register("group", GroupControl, HELP) 350 cli.invoke(sys.argv[1:]) 351