mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-03 16:10:26 +00:00
[shibboleth] Modify user creation
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
from django.db import connection
|
from django.db import connection
|
||||||
from django.contrib.auth.models import User, Permission
|
|
||||||
from django.contrib.auth.backends import RemoteUserBackend
|
from django.contrib.auth.backends import RemoteUserBackend
|
||||||
|
|
||||||
|
from seahub.base.accounts import User
|
||||||
|
|
||||||
class ShibbolethRemoteUserBackend(RemoteUserBackend):
|
class ShibbolethRemoteUserBackend(RemoteUserBackend):
|
||||||
"""
|
"""
|
||||||
This backend is to be used in conjunction with the ``RemoteUserMiddleware``
|
This backend is to be used in conjunction with the ``RemoteUserMiddleware``
|
||||||
@@ -17,6 +18,13 @@ class ShibbolethRemoteUserBackend(RemoteUserBackend):
|
|||||||
# Create a User object if not already in the database?
|
# Create a User object if not already in the database?
|
||||||
create_unknown_user = True
|
create_unknown_user = True
|
||||||
|
|
||||||
|
def get_user(self, username):
|
||||||
|
try:
|
||||||
|
user = User.objects.get(email=username)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
user = None
|
||||||
|
return user
|
||||||
|
|
||||||
def authenticate(self, remote_user, shib_meta):
|
def authenticate(self, remote_user, shib_meta):
|
||||||
"""
|
"""
|
||||||
The username passed as ``remote_user`` is considered trusted. This
|
The username passed as ``remote_user`` is considered trusted. This
|
||||||
@@ -30,17 +38,14 @@ class ShibbolethRemoteUserBackend(RemoteUserBackend):
|
|||||||
return
|
return
|
||||||
user = None
|
user = None
|
||||||
username = self.clean_username(remote_user)
|
username = self.clean_username(remote_user)
|
||||||
shib_user_params = dict([(k, shib_meta[k]) for k in User._meta.get_all_field_names() if k in shib_meta])
|
|
||||||
# Note that this could be accomplished in one try-except clause, but
|
# Note that this could be accomplished in one try-except clause, but
|
||||||
# instead we use get_or_create when creating unknown users since it has
|
# instead we use get_or_create when creating unknown users since it has
|
||||||
# built-in safeguards for multiple threads.
|
# built-in safeguards for multiple threads.
|
||||||
if self.create_unknown_user:
|
if self.create_unknown_user:
|
||||||
user, created = User.objects.get_or_create(username=shib_user_params.get('username'), defaults=shib_user_params)
|
user = User.objects.create_user(email=username)
|
||||||
if created:
|
|
||||||
user = self.configure_user(user)
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(**shib_user_params)
|
user = User.objects.get(email=username)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
return user
|
return user
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
from django.contrib.auth.middleware import RemoteUserMiddleware
|
from django.contrib.auth.middleware import RemoteUserMiddleware
|
||||||
from django.contrib import auth
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
from shibboleth.app_settings import SHIB_ATTRIBUTE_MAP, LOGOUT_SESSION_KEY
|
from shibboleth.app_settings import SHIB_ATTRIBUTE_MAP, LOGOUT_SESSION_KEY
|
||||||
|
|
||||||
|
from seahub import auth
|
||||||
|
|
||||||
class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
|
class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
|
||||||
"""
|
"""
|
||||||
Authentication Middleware for use with Shibboleth. Uses the recommended pattern
|
Authentication Middleware for use with Shibboleth. Uses the recommended pattern
|
||||||
@@ -39,7 +40,7 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
|
|||||||
# getting passed in the headers, then the correct user is already
|
# getting passed in the headers, then the correct user is already
|
||||||
# persisted in the session and we don't need to continue.
|
# persisted in the session and we don't need to continue.
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated():
|
||||||
if request.user.username == self.clean_username(username, request):
|
if request.user.username == username:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Make sure we have all required Shiboleth elements before proceeding.
|
# Make sure we have all required Shiboleth elements before proceeding.
|
||||||
|
Reference in New Issue
Block a user