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

Source Code for Module omero.plugins.user

  1  #!/usr/bin/env python 
  2  """ 
  3     User administration plugin 
  4   
  5     Copyright 2009 Glencoe Software, Inc. All rights reserved. 
  6     Use is subject to license terms supplied in LICENSE.txt 
  7   
  8  """ 
  9   
 10  import os 
 11  import sys 
 12   
 13  from omero.cli import BaseControl, CLI 
 14   
 15  HELP = "Support for adding and managing users" 
 16   
17 -class UserControl(BaseControl):
18
19 - def _configure(self, parser):
20 sub = parser.sub() 21 22 add = parser.add(sub, self.add, help = "Add users") 23 add.add_argument("-m", "--middlename", help = "Middle name, if available") 24 add.add_argument("-e", "--email") 25 add.add_argument("-i", "--institution") 26 # Capitalized since conflict with main values 27 add.add_argument("-P", "--userpassword", help = "Password for user") 28 add.add_argument("-a", "--admin", help = "Whether the user should be an admin") 29 add.add_argument("username", help = "User's login name") 30 add.add_argument("firstname", help = "User's given name") 31 add.add_argument("lastname", help = "User's surname name") 32 add.add_argument("member_of", nargs="+", help = "Groups which the user is to be a member of") 33 34 list = parser.add(sub, self.list, help = "List current users") 35 36 password = parser.add(sub, self.password, help = "Set user's password") 37 password.add_argument("username", nargs="?", help = "Username if not the current user")
38
39 - def password(self, args):
40 from omero.rtypes import rstring 41 client = self.ctx.conn(args) 42 admin = client.sf.getAdminService() 43 pw = self._ask_for_password(" to be set") 44 pw = rstring(pw) 45 if args.username: 46 admin.changeUserPassword(args.username, pw) 47 else: 48 admin.changePassword(pw) 49 self.ctx.out("Password changed")
50
51 - def list(self, args):
52 c = self.ctx.conn(args) 53 users = c.sf.getAdminService().lookupExperimenters() 54 from omero.util.text import TableBuilder 55 tb = TableBuilder("id", "omeName", "firstName", "lastName", "email", "member of", "leader of") 56 for user in users: 57 row = [user.id.val, user.omeName.val, user.firstName.val, user.lastName.val] 58 row.append(user.email and user.email.val or "") 59 member_of = [str(x.parent.id.val) for x in user.copyGroupExperimenterMap() if not x.owner.val] 60 leader_of = [str(x.parent.id.val) for x in user.copyGroupExperimenterMap() if x.owner.val] 61 if member_of: 62 row.append(",".join(member_of)) 63 else: 64 row.append("") 65 if leader_of: 66 row.append(",".join(leader_of)) 67 else: 68 row.append("") 69 70 tb.row(*tuple(row)) 71 self.ctx.out(str(tb.build()))
72
73 - def add(self, args):
74 email = args.email 75 login = args.username 76 first = args.firstname 77 middle = args.middlename 78 last = args.lastname 79 inst = args.institution 80 pasw = args.userpassword 81 82 import omero 83 from omero.rtypes import rstring 84 from omero_model_ExperimenterI import ExperimenterI as Exp 85 from omero_model_ExperimenterGroupI import ExperimenterGroupI as Grp 86 c = self.ctx.conn(args) 87 p = c.ic.getProperties() 88 e = Exp() 89 e.omeName = rstring(login) 90 e.firstName = rstring(first) 91 e.lastName = rstring(last) 92 e.middleName = rstring(middle) 93 e.email = rstring(email) 94 e.institution = rstring(inst) 95 admin = c.getSession().getAdminService() 96 97 try: 98 groups = [admin.lookupGroup(group) for group in args.member_of] 99 except omero.ApiUsageException, aue: 100 self.ctx.die(68, aue.message) 101 102 roles = admin.getSecurityRoles() 103 groups.append(Grp(roles.userGroupId, False)) 104 if args.admin: 105 groups.append(Grp(roles.systemGroupId, False)) 106 107 group = groups.pop(0) 108 109 try: 110 if pasw is None: 111 id = admin.createExperimenter(e, group, groups) 112 self.ctx.out("Added user %s" % id) 113 else: 114 id = admin.createExperimenterWithPassword(e, rstring(pasw), group, groups) 115 self.ctx.out("Added user %s with password" % id) 116 except omero.ValidationException, ve: 117 if "org.hibernate.exception.ConstraintViolationException: could not insert" in str(ve): 118 self.ctx.die(66, "User already exists: %s" % login) 119 else: 120 self.ctx.die(67, "Unknown ValidationException: %s" % ve.message)
121 122 try: 123 register("user", UserControl, HELP) 124 except NameError: 125 if __name__ == "__main__": 126 cli = CLI() 127 cli.register("user", UserControl, HELP) 128 cli.invoke(sys.argv[1:]) 129