-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user login/logout logging signals (#1326)
- Loading branch information
Showing
3 changed files
with
46 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |