-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path18-archive-users.py
More file actions
executable file
·57 lines (47 loc) · 2.19 KB
/
18-archive-users.py
File metadata and controls
executable file
·57 lines (47 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
from datetime import datetime
# the python library for elabftw
import elabapi_python
from client import api_client
from dateutil.relativedelta import relativedelta
#####################
# DESCRIPTION #
# ################################################################################################
# In this script, we look at the last time a user logged in and archive their account after 8 months of inactivity #
# This script would typically be run by an Sysadmin user #
####################################################################################################################
# this is the date format we get back from API
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
# we want to archive users not logged in after 8 months
INACTIVE_PERIOD_MONTHS = 8
# The team ID of the user we want to archive
USER_TEAM_ID = 1
def should_be_archived(date_string):
date_format = "%Y-%m-%d %H:%M:%S"
input_date = datetime.strptime(date_string, date_format)
current_date = datetime.now()
threshold_date = current_date - relativedelta(months=INACTIVE_PERIOD_MONTHS)
return input_date < threshold_date
# Load the users api
usersApi = elabapi_python.UsersApi(api_client)
# loop over all users
users = usersApi.read_users()
for user in users:
# uncomment to print emails and last login time
# print(f'{user.email}: {user.last_login}')
# if user never logged in, it'll be NULL in MySQL, so None in python
# also prevent archival of sysadmin accounts (server will throw error anyway)
if user.last_login is not None and user.is_sysadmin != 1:
if should_be_archived(user.last_login):
print(f"Archiving user {user.email}. Last login: {user.last_login}")
usersApi.patch_user(
user.userid,
body={
'action': 'patchuser2team',
'team': USER_TEAM_ID,
'target': 'is_archived',
'content': 1,
},
)
# The user can un-archive by flipping the "content" value to 0
# TODO: fix last_login info is not provided anymore when user is not admin!