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, ExceptionHandler 
 14  from omero.rtypes import unwrap as _ 
 15   
 16  HELP = "Support for adding and managing users" 
 17   
18 -class UserControl(BaseControl):
19
20 - def _configure(self, parser):
21 22 self.exc = ExceptionHandler() 23 24 sub = parser.sub() 25 26 add = parser.add(sub, self.add, help = "Add users") 27 add.add_argument("--ignore-existing", action="store_true", default=False, help="Do not fail if user already exists") 28 add.add_argument("-m", "--middlename", help = "Middle name, if available") 29 add.add_argument("-e", "--email") 30 add.add_argument("-i", "--institution") 31 # Capitalized since conflict with main values 32 add.add_argument("-P", "--userpassword", help = "Password for user") 33 add.add_argument("-a", "--admin", action="store_true", help = "Whether the user should be an admin") 34 add.add_argument("username", help = "User's login name") 35 add.add_argument("firstname", help = "User's given name") 36 add.add_argument("lastname", help = "User's surname name") 37 add.add_argument("member_of", nargs="+", help = "Groups which the user is to be a member of") 38 39 list = parser.add(sub, self.list, help = "List current users") 40 41 password = parser.add(sub, self.password, help = "Set user's password") 42 password.add_argument("username", nargs="?", help = "Username if not the current user") 43 44 email = parser.add(sub, self.email, help = "List users' email addresses") 45 email.add_argument("-n", "--names", action="store_true", default=False, help = "Print user names along with email addresses") 46 email.add_argument("-1", "--one", action="store_true", default=False, help = "Print one user per line") 47 email.add_argument("-i", "--ignore", action="store_true", default=False, help = "Ignore users without email addresses")
48
49 - def format_name(self, exp):
50 record = "" 51 fn = _(exp.firstName) 52 mn = " " 53 if _(exp.middleName): 54 mn = " %s " % _(exp.middleName) 55 ln = _(exp.lastName) 56 record += "%s%s%s" % (fn, mn, ln) 57 return record
58
59 - def email(self, args):
60 c = self.ctx.conn(args) 61 a = c.sf.getAdminService() 62 63 skipped = [] 64 records = [] 65 for exp in a.lookupExperimenters(): 66 67 # Handle users without email 68 if not _(exp.email): 69 if not args.ignore: 70 skipped.append(exp) 71 continue 72 73 record = "" 74 if args.names: 75 record += '"%s"' % self.format_name(exp) 76 record += " <%s>" % _(exp.email) 77 else: 78 record += _(exp.email) 79 80 records.append(record) 81 82 if args.one: 83 for record in records: 84 self.ctx.out(record) 85 else: 86 self.ctx.out(", ".join(records)) 87 88 if skipped: 89 self.ctx.err("Missing email addresses:") 90 for s in skipped: 91 self.ctx.err(self.format_name(s))
92
93 - def password(self, args):
94 import omero 95 from omero.rtypes import rstring 96 client = self.ctx.conn(args) 97 own_name = self.ctx._event_context.userName 98 admin = client.sf.getAdminService() 99 100 # tickets 3202, 5841 101 own_pw = self._ask_for_password(" for your user (%s)" % own_name, strict = False) 102 try: 103 client.sf.setSecurityPassword(own_pw) 104 self.ctx.out("Verified password.\n") 105 except omero.SecurityViolation, sv: 106 import traceback 107 self.ctx.die(456, "SecurityViolation: Bad credentials") 108 self.ctx.dbg(traceback.format_exc(sv)) 109 110 if args.username: 111 self.ctx.out("Changing password for %s" % args.username) 112 else: 113 self.ctx.out("Changing password for %s" % own_name) 114 115 pw = self._ask_for_password(" to be set") 116 pw = rstring(pw) 117 if args.username: 118 admin.changeUserPassword(args.username, pw) 119 else: 120 admin.changePassword(pw) 121 self.ctx.out("Password changed")
122
123 - def list(self, args):
124 c = self.ctx.conn(args) 125 users = c.sf.getAdminService().lookupExperimenters() 126 from omero.util.text import TableBuilder 127 tb = TableBuilder("id", "omeName", "firstName", "lastName", "email", "member of", "leader of") 128 for user in users: 129 row = [user.id.val, user.omeName.val, user.firstName.val, user.lastName.val] 130 row.append(user.email and user.email.val or "") 131 member_of = [str(x.parent.id.val) for x in user.copyGroupExperimenterMap() if not x.owner.val] 132 leader_of = [str(x.parent.id.val) for x in user.copyGroupExperimenterMap() if x.owner.val] 133 if member_of: 134 row.append(",".join(member_of)) 135 else: 136 row.append("") 137 if leader_of: 138 row.append(",".join(leader_of)) 139 else: 140 row.append("") 141 142 tb.row(*tuple(row)) 143 self.ctx.out(str(tb.build()))
144
145 - def add(self, args):
146 email = args.email 147 login = args.username 148 first = args.firstname 149 middle = args.middlename 150 last = args.lastname 151 inst = args.institution 152 pasw = args.userpassword 153 154 import omero 155 from omero.rtypes import rstring 156 from omero_model_ExperimenterI import ExperimenterI as Exp 157 from omero_model_ExperimenterGroupI import ExperimenterGroupI as Grp 158 c = self.ctx.conn(args) 159 p = c.ic.getProperties() 160 e = Exp() 161 e.omeName = rstring(login) 162 e.firstName = rstring(first) 163 e.lastName = rstring(last) 164 e.middleName = rstring(middle) 165 e.email = rstring(email) 166 e.institution = rstring(inst) 167 admin = c.getSession().getAdminService() 168 169 try: 170 usr = admin.lookupExperimenter(login) 171 if usr: 172 if args.ignore_existing: 173 self.ctx.out("User exists: %s (id=%s)" % (login, usr.id.val)) 174 return 175 else: 176 self.ctx.die(3, "User exists: %s (id=%s)" % (login, usr.id.val)) 177 except omero.ApiUsageException, aue: 178 pass # Apparently no such user exists 179 180 try: 181 groups = [admin.lookupGroup(group) for group in args.member_of] 182 except omero.ApiUsageException, aue: 183 self.ctx.die(68, aue.message) 184 185 roles = admin.getSecurityRoles() 186 groups.append(Grp(roles.userGroupId, False)) 187 if args.admin: 188 groups.append(Grp(roles.systemGroupId, False)) 189 190 group = groups.pop(0) 191 192 try: 193 if pasw is None: 194 id = admin.createExperimenter(e, group, groups) 195 self.ctx.out("Added user %s" % id) 196 else: 197 id = admin.createExperimenterWithPassword(e, rstring(pasw), group, groups) 198 self.ctx.out("Added user %s with password" % id) 199 except omero.ValidationException, ve: 200 # Possible, though unlikely after previous check 201 if self.exc.is_constraint_violation(ve): 202 self.ctx.die(66, "User already exists: %s" % login) 203 else: 204 self.ctx.die(67, "Unknown ValidationException: %s" % ve.message) 205 except omero.SecurityViolation, se: 206 self.ctx.die(68, "Security violation: %s" % se.message)
207 208 try: 209 register("user", UserControl, HELP) 210 except NameError: 211 if __name__ == "__main__": 212 cli = CLI() 213 cli.register("user", UserControl, HELP) 214 cli.invoke(sys.argv[1:]) 215