Skip to content

Commit

Permalink
add user login/logout logging signals (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Nov 6, 2023
1 parent a9fb873 commit 15174e8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Added
- ``_project_badge.html`` template (#1300)
- ``InvalidFormMixin`` helper mixin (#1310)
- Temporary ``user_name`` param in remote sync app settings (#1320)
- User login/logout logging signals (#1326)

Changed
-------
Expand Down
1 change: 1 addition & 0 deletions docs/source/major_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ v0.13.3 (WIP)

- Add common project badge template
- Add InvalidFormMixin helper mixin
- Add user login/logout logging signals
- Prevent updating global app settings for remote projects
- Fix hidden JSON project setting reset on non-superuser project update
- Fix custom app setting validation calls in forms
Expand Down
49 changes: 44 additions & 5 deletions projectroles/signals.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
"""Django signals for the projectroles app"""

from django.contrib.auth.signals import user_logged_in
import logging

from django.conf import settings
from django.contrib.auth.signals import (
user_logged_in,
user_logged_out,
user_login_failed,
)


logger = logging.getLogger(__name__)


# User signals -----------------------------------------------------------------


def handle_ldap_login(sender, user, **kwargs):
"""Signal for LDAP login handling"""
if hasattr(user, 'ldap_username'):
user.update_full_name()
user.update_ldap_username()
try:
if hasattr(user, 'ldap_username'):
user.update_full_name()
user.update_ldap_username()
except Exception as ex:
logger.error('Exception in handle_ldap_login(): {}'.format(ex))
if settings.DEBUG:
raise ex


def assign_user_group(sender, user, **kwargs):
"""Signal for user group assignment"""
user.set_group()
try:
user.set_group()
except Exception as ex:
logger.error('Exception in assign_user_group(): {}'.format(ex))
if settings.DEBUG:
raise ex


def log_user_login(sender, user, **kwargs):
"""Signal for logging user login"""
logger.info('User logged in: {}'.format(user.username))


def log_user_logout(sender, user, **kwargs):
"""Signal for logging user logout"""
if user:
logger.info('User logged out: {}'.format(user.username))


def log_user_login_failure(sender, credentials, **kwargs):
"""Signal for user login failure"""
logger.info('User login failed: {}'.format(credentials.get('username')))


user_logged_in.connect(handle_ldap_login)
user_logged_in.connect(assign_user_group)
user_logged_in.connect(log_user_login)
user_logged_out.connect(log_user_logout)
user_login_failed.connect(log_user_login_failure)

0 comments on commit 15174e8

Please sign in to comment.