2018-11-09 06:54:38 +00:00
|
|
|
from django.http.request import QueryDict
|
|
|
|
from django.conf import settings
|
2018-11-27 02:21:47 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.contrib.auth.signals import user_logged_out
|
|
|
|
from django_auth_ldap.backend import populate_user
|
2019-02-27 00:45:00 +00:00
|
|
|
|
2019-12-09 08:12:48 +00:00
|
|
|
from users.models import User
|
2019-02-28 03:58:48 +00:00
|
|
|
from .backends.openid import new_client
|
|
|
|
from .backends.openid.signals import (
|
2019-12-09 08:12:48 +00:00
|
|
|
post_create_or_update_openid_user, post_openid_login_success
|
2019-02-27 00:45:00 +00:00
|
|
|
)
|
2019-11-05 10:46:29 +00:00
|
|
|
from .signals import post_auth_success
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_logged_out)
|
|
|
|
def on_user_logged_out(sender, request, user, **kwargs):
|
|
|
|
if not settings.AUTH_OPENID:
|
|
|
|
return
|
2019-07-01 03:04:15 +00:00
|
|
|
if not settings.AUTH_OPENID_SHARE_SESSION:
|
|
|
|
return
|
2018-11-09 06:54:38 +00:00
|
|
|
query = QueryDict('', mutable=True)
|
|
|
|
query.update({
|
|
|
|
'redirect_uri': settings.BASE_SITE_URL
|
|
|
|
})
|
2019-02-28 03:58:48 +00:00
|
|
|
client = new_client()
|
2018-11-09 06:54:38 +00:00
|
|
|
openid_logout_url = "%s?%s" % (
|
2019-07-01 03:04:15 +00:00
|
|
|
client.get_url_end_session_endpoint(),
|
2018-11-09 06:54:38 +00:00
|
|
|
query.urlencode()
|
|
|
|
)
|
|
|
|
request.COOKIES['next'] = openid_logout_url
|
|
|
|
|
|
|
|
|
2019-12-09 08:12:48 +00:00
|
|
|
@receiver(post_create_or_update_openid_user)
|
|
|
|
def on_post_create_or_update_openid_user(sender, user=None, created=True, **kwargs):
|
|
|
|
if created and user and user.username != 'admin':
|
2018-11-09 06:54:38 +00:00
|
|
|
user.source = user.SOURCE_OPENID
|
|
|
|
user.save()
|
|
|
|
|
2018-11-27 02:21:47 +00:00
|
|
|
|
2019-02-28 03:58:48 +00:00
|
|
|
@receiver(post_openid_login_success)
|
|
|
|
def on_openid_login_success(sender, user=None, request=None, **kwargs):
|
|
|
|
post_auth_success.send(sender=sender, user=user, request=request)
|
|
|
|
|
|
|
|
|
2018-11-27 02:21:47 +00:00
|
|
|
@receiver(populate_user)
|
|
|
|
def on_ldap_create_user(sender, user, ldap_user, **kwargs):
|
2019-11-11 08:41:32 +00:00
|
|
|
if user and user.username not in ['admin']:
|
2019-12-09 08:12:48 +00:00
|
|
|
exists = User.objects.filter(username=user.username).exists()
|
|
|
|
if not exists:
|
|
|
|
user.source = user.SOURCE_LDAP
|
|
|
|
user.save()
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
2019-07-24 07:49:37 +00:00
|
|
|
|