Skip to content
Snippets Groups Projects
Commit 1d11f2f6 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add command to run all maintenance tasks

parent 642cee2e
No related branches found
No related tags found
No related merge requests found
...@@ -48,6 +48,19 @@ def sync_user(connection, user): ...@@ -48,6 +48,19 @@ def sync_user(connection, user):
return user return user
def sync_users():
"""Synchronize all users from the database with information the LDAP server"""
current_app.logger.info('Synchronize database with information from the LDAP server')
try:
connection = ldap_manager.connection
except ldap3.core.exceptions.LDAPException as e:
current_app.logger.warning(f'Failed to connect to the LDAP server: {e}')
return
for user in User.query.all():
sync_user(connection, user)
db.session.commit()
def register_cli(app): def register_cli(app):
@app.cli.command() @app.cli.command()
def initdb(): def initdb():
...@@ -65,16 +78,15 @@ def register_cli(app): ...@@ -65,16 +78,15 @@ def register_cli(app):
@app.cli.command() @app.cli.command()
def syncusers(): def syncusers():
"""Synchronize all users from the database with information the LDAP server""" """Synchronize all users from the database with information the LDAP server"""
try: sync_users()
connection = ldap_manager.connection
except ldap3.core.exceptions.LDAPException as e:
current_app.logger.warning(f'Failed to connect to the LDAP server: {e}')
return
for user in User.query.all():
sync_user(connection, user)
db.session.commit()
@app.cli.command() @app.cli.command()
def delete_expired_tokens(): def delete_expired_tokens():
"""Prune database from expired tokens""" """Prune database from expired tokens"""
tokens.prune_database() tokens.prune_database()
@app.cli.command()
def maintenance():
"""Run maintenance commands"""
sync_users()
tokens.prune_database()
...@@ -90,6 +90,7 @@ def revoke_token(token_id, user_id): ...@@ -90,6 +90,7 @@ def revoke_token(token_id, user_id):
def prune_database(): def prune_database():
"""Delete tokens that have expired from the database""" """Delete tokens that have expired from the database"""
current_app.logger.info('Delete expired tokens')
now = datetime.now() now = datetime.now()
expired = models.Token.query.filter(models.Token.expires < now).all() expired = models.Token.query.filter(models.Token.expires < now).all()
for token in expired: for token in expired:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment