1
2
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
23
103
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
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
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
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
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
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
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
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
244
246
247
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
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
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
331
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