|
| 1 | +#!/usr/bin/env python |
| 2 | +import datetime |
| 3 | +import json |
| 4 | +from datetime import datetime |
| 5 | +from dateutil.relativedelta import relativedelta |
| 6 | +# the python library for elabftw |
| 7 | +import elabapi_python |
| 8 | + |
| 9 | +##################### |
| 10 | +# DESCRIPTION # |
| 11 | +# ################################################################################################ |
| 12 | +# In this script, we look at the last time a user logged in and archive their account after 8 months of inactivity # |
| 13 | +# This script would typically be run by an Sysadmin user # |
| 14 | +#################################################################################################################### |
| 15 | + |
| 16 | +######################### |
| 17 | +# CONFIG # |
| 18 | +######################### |
| 19 | +# replace with the URL of your instance |
| 20 | +API_HOST_URL = 'https://elab.local:3148/api/v2' |
| 21 | +# replace with your api key |
| 22 | +API_KEY = 'apiKey4Test' |
| 23 | + |
| 24 | +# this is the date format we get back from API |
| 25 | +DATE_FORMAT = '%Y-%m-%d %H:%M:%S' |
| 26 | +# we want to archive users not logged in after 8 months |
| 27 | +INACTIVE_PERIOD_MONTHS = 8 |
| 28 | +######################### |
| 29 | +# END CONFIG # |
| 30 | +######################### |
| 31 | + |
| 32 | +# Configure the api client |
| 33 | +configuration = elabapi_python.Configuration() |
| 34 | +configuration.api_key['api_key'] = API_KEY |
| 35 | +configuration.api_key_prefix['api_key'] = 'Authorization' |
| 36 | +configuration.host = API_HOST_URL |
| 37 | +configuration.debug = False |
| 38 | +configuration.verify_ssl = False |
| 39 | + |
| 40 | +# create an instance of the API class |
| 41 | +api_client = elabapi_python.ApiClient(configuration) |
| 42 | +# fix issue with Authorization header not being properly set by the generated lib |
| 43 | +api_client.set_default_header(header_name='Authorization', header_value=API_KEY) |
| 44 | + |
| 45 | + |
| 46 | +#### SCRIPT START ################## |
| 47 | +def should_be_archived(date_string): |
| 48 | + date_format = "%Y-%m-%d %H:%M:%S" |
| 49 | + input_date = datetime.strptime(date_string, date_format) |
| 50 | + current_date = datetime.now() |
| 51 | + threshold_date = current_date - relativedelta(months=INACTIVE_PERIOD_MONTHS) |
| 52 | + return input_date < threshold_date |
| 53 | + |
| 54 | +# Load the users api |
| 55 | +usersApi = elabapi_python.UsersApi(api_client) |
| 56 | + |
| 57 | +# loop over all users |
| 58 | +users = usersApi.read_users() |
| 59 | +for user in users: |
| 60 | + # uncomment to print emails and last login time |
| 61 | + # print(f'{user.email}: {user.last_login}') |
| 62 | + |
| 63 | + # if user never logged in, it'll be NULL in MySQL, so None in python |
| 64 | + # also prevent archival of sysadmin accounts (server will throw error anyway) |
| 65 | + if user.last_login is not None and user.is_sysadmin != 1: |
| 66 | + if should_be_archived(user.last_login): |
| 67 | + print(f'Archiving user {user.email}. Last login: {user.last_login}') |
| 68 | + usersApi.patch_user(user.userid, body={'action': 'archive'}) |
0 commit comments