#!/usr/bin/env python2 # Copyright (C) 2019 University of Dundee & Open Microscopy Environment. # All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from omero.callbacks import CmdCallbackI from omero.gateway import BlitzGateway from omero.sys import ParametersI from omero.cmd import Delete2, Delete2Response import sys from time import time # Fill in administrator login credentials. conn = BlitzGateway('username', 'password', host='omero.server.host') # Do not inactivate users who logged out within recent days. minimum_days = 30 * 6 # -=-=- Users need not adjust code below -=-=- # Report configuration. print('Ignoring users who have logged out within the past {} days.' .format(minimum_days)) # Obtain gateway connection and query service. conn.connect() query_service = conn.getQueryService() def submit(request, expected): # Submit a request and wait for it to complete. # Returns with the response only if it was of the given type. prx = conn.c.sf.submit(request) rsp = CmdCallbackI(conn.c, prx).loop(500, 500) if not isinstance(rsp, expected): conn._closeSession() sys.exit('unexpected response: {}'.format(rsp)) return rsp # Determine which users to consider inactivating. users = {} for result in query_service.projection( ("SELECT child.id, child.omeName FROM GroupExperimenterMap " "WHERE parent.name = 'user'"), None): user_id = result[0].val user_name = result[1].val users[user_id] = user_name for result in query_service.projection( "SELECT DISTINCT owner.id FROM Session WHERE closed IS NULL", None): user_id = result[0].val print('Ignoring "{}" (#{}) who is logged in.' .format(users[user_id], user_id)) del users[user_id] now = time() for result in query_service.projection( "SELECT owner.id, MAX(closed) FROM Session GROUP BY owner.id", None): user_id = result[0].val if user_id not in users: continue if result[1] is None: # never logged in user_logout = 0 else: # note time in seconds since epoch user_logout = result[1].val / 1000 days = (now - user_logout) / (60 * 60 * 24) if (days < minimum_days): print('Ignoring "{}" (#{}) who logged in recently.' .format(users[user_id], user_id)) del users[user_id] if not users: print('No users need inactivating.') exit() # Inactivate users. for id, name in users.items(): print('Inactivating "{}" (#{}).'.format(name, id)) # Find the IDs of the links that mark the users active. params = ParametersI() params.addIds(users.keys()) link_ids = [] for result in query_service.projection( ("SELECT id FROM GroupExperimenterMap " "WHERE parent.name = 'user' AND child.id IN (:ids)"), params): link_ids.append(result[0].val) if link_ids: # Inactivate the users. delete = Delete2(targetObjects={'GroupExperimenterMap': link_ids}) submit(delete, Delete2Response) else: print('No users need inactivating.') conn._closeSession()