From 4be7cccadb6d87142c75aaec20fd656d5fe0b4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Fri, 12 May 2023 17:11:18 +0200 Subject: [PATCH] (auth_ldap) add get_last_modified from provider --- flaschengeist/controller/userController.py | 9 +++++++-- flaschengeist/plugins/__init__.py | 10 ++++++++++ flaschengeist/plugins/auth_ldap/__init__.py | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/flaschengeist/controller/userController.py b/flaschengeist/controller/userController.py index b5e427d..9d8b2ac 100644 --- a/flaschengeist/controller/userController.py +++ b/flaschengeist/controller/userController.py @@ -2,7 +2,7 @@ import re import secrets from io import BytesIO -from typing import Optional +from typing import Optional, Union from sqlalchemy import exc from sqlalchemy_utils import merge_references from datetime import datetime, timedelta, timezone @@ -262,7 +262,12 @@ def register(data, passwd=None): return user -def load_avatar(user: User): +def get_last_modified(user: User): + """Get the last modification date of the user""" + return get_provider(user.userid).get_last_modified(user) + + +def load_avatar(user: User, etag: Union[str, None] = None): if user.avatar_ is not None: return imageController.send_image(image=user.avatar_) else: diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index 2907166..23d3783 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -248,6 +248,16 @@ class AuthPlugin(Plugin): """ raise NotImplementedError + def get_modified_time(self, user): + """If backend is using external data, then return the timestamp of the last modification + + Args: + user: User object + Returns: + Timestamp of last modification + """ + pass + def get_avatar(self, user) -> _Avatar: """Retrieve avatar for given user (if supported by auth backend) diff --git a/flaschengeist/plugins/auth_ldap/__init__.py b/flaschengeist/plugins/auth_ldap/__init__.py index db2823a..4574929 100644 --- a/flaschengeist/plugins/auth_ldap/__init__.py +++ b/flaschengeist/plugins/auth_ldap/__init__.py @@ -10,6 +10,8 @@ from ldap3.core.exceptions import LDAPPasswordIsMandatoryError, LDAPBindError from werkzeug.exceptions import BadRequest, InternalServerError, NotFound from werkzeug.datastructures import FileStorage +from datetime import datetime + from flaschengeist import logger from flaschengeist.config import config from flaschengeist.controller import userController @@ -158,6 +160,17 @@ class AuthLDAP(AuthPlugin): except (LDAPPasswordIsMandatoryError, LDAPBindError): raise BadRequest + def get_modified_time(self, user): + self.ldap.connection.search( + self.search_dn, + "(uid={})".format(user.userid), + SUBTREE, + attributes=["modifyTimestamp"], + ) + r = self.ldap.connection.response[0]["attributes"] + modified_time = r["modifyTimestamp"][0] + return datetime.strptime(modified_time, "%Y%m%d%H%M%SZ") + def get_avatar(self, user): self.ldap.connection.search( self.search_dn,