From e7f1b4fd6ce254fa09576724f4e7aa576dcb9e46 Mon Sep 17 00:00:00 2001 From: terrtia Date: Tue, 13 Aug 2024 14:47:23 +0200 Subject: [PATCH] chg: [ail users] save last api usage --- bin/lib/ail_api.py | 1 + bin/lib/ail_users.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bin/lib/ail_api.py b/bin/lib/ail_api.py index 56918924..58915088 100755 --- a/bin/lib/ail_api.py +++ b/bin/lib/ail_api.py @@ -73,6 +73,7 @@ def authenticate_user(token, ip_address): return {'status': 'error', 'reason': 'Malformed Authentication String'}, 400 if is_valid_token(token): + ail_users.update_user_last_seen_api(get_user_from_token(token)) return True, 200 # Failed Login else: diff --git a/bin/lib/ail_users.py b/bin/lib/ail_users.py index 398cb8bd..f335fc72 100755 --- a/bin/lib/ail_users.py +++ b/bin/lib/ail_users.py @@ -283,6 +283,12 @@ def get_user_last_login(user_id): def get_user_last_seen(user_id): return r_serv_db.hget(f'ail:user:metadata:{user_id}', 'last_seen') +def get_user_last_seen_api(user_id): + return r_serv_db.hget(f'ail:user:metadata:{user_id}', 'last_seen_api') + +def update_user_last_seen_api(user_id): + r_serv_db.hset(f'ail:user:metadata:{user_id}', 'last_seen_api', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) + def get_disabled_users(): return r_serv_db.smembers(f'ail:users:disabled') @@ -366,8 +372,7 @@ def edit_user(user_id, password_hash, chg_passwd=False, otp=True): ######################################################################################################################## ######################################################################################################################## -# TODO USER: - Creation Date -# - Last API Usage +# TODO USER: # - Organisation ??? # - Disabled / Lock @@ -400,6 +405,9 @@ class AILUser(UserMixin): def update_last_seen(self): r_serv_db.hset(f'ail:user:metadata:{self.user_id}', 'last_seen', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) + def update_last_seen_api(self): + update_user_last_seen_api(self.user_id) + def update_last_login(self): r_serv_db.hset(f'ail:user:metadata:{self.user_id}', 'last_login', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) @@ -415,6 +423,8 @@ class AILUser(UserMixin): meta['last_login'] = get_user_last_login(self.user_id) if 'last_seen' in options: meta['last_seen'] = get_user_last_seen(self.user_id) + if 'last_seen_api' in options: + meta['last_seen_api'] = get_user_last_seen_api(self.user_id) if 'api_key' in options: # TODO add option to censor key meta['api_key'] = self.get_api_key() if 'role' in options: @@ -543,7 +553,7 @@ class AILUser(UserMixin): def api_get_users_meta(): meta = {'users': []} - options = {'api_key', 'creator', 'created_at', 'is_logged', 'last_edit', 'last_login', 'last_seen', 'role', '2fa', 'otp_setup'} + options = {'api_key', 'creator', 'created_at', 'is_logged', 'last_edit', 'last_login', 'last_seen', 'last_seen_api', 'role', '2fa', 'otp_setup'} for user_id in get_users(): user = AILUser(user_id) meta['users'].append(user.get_meta(options=options))