Skip to content

Commit

Permalink
add add_if_not_exist
Browse files Browse the repository at this point in the history
  • Loading branch information
imwhatiam committed Jun 7, 2024
1 parent 4ad87f2 commit f014ff0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
32 changes: 17 additions & 15 deletions seahub/auth/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
# Copyright (c) 2012-2016 Seafile Ltd.
import datetime
import hashlib
import urllib.request, urllib.parse, urllib.error
import logging

# import auth
from django.core.exceptions import ImproperlyConfigured
from registration.signals import user_deleted
from django.db import models
from django.db.models.manager import EmptyManager
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_str
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from django.dispatch import receiver
from django.utils.encoding import smart_str
from django.db.models.manager import EmptyManager

logger = logging.getLogger(__name__)
UNUSABLE_PASSWORD = '!' # This will never be a valid hash
Expand Down Expand Up @@ -130,15 +125,26 @@ def is_authenticated(self):


class SocialAuthUserManager(models.Manager):

def add(self, username, provider, uid, extra_data=''):
try:
social_auth_user = self.model(username=username, provider=provider, uid=uid, extra_data=extra_data)
social_auth_user = self.model(username=username, provider=provider,
uid=uid, extra_data=extra_data)
social_auth_user.save()
return social_auth_user
except Exception as e:
logger.error(e)
return None

def add_if_not_exist(self, username, provider, uid, extra_data=''):

social_auth_user = self.get_by_provider_and_uid(provider, uid)
if not social_auth_user:
social_auth_user = self.add(username, provider,
uid, extra_data=extra_data)

return social_auth_user

def get_by_provider_and_uid(self, provider, uid):
try:
social_auth_user = self.get(provider=provider, uid=uid)
Expand Down Expand Up @@ -186,11 +192,7 @@ class Meta:
db_table = 'external_department'


# # handle signals
from django.dispatch import receiver
from registration.signals import user_deleted


# handle signals
@receiver(user_deleted)
def user_deleted_cb(sender, **kwargs):
username = kwargs['username']
Expand Down
15 changes: 9 additions & 6 deletions thirdpart/shibboleth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@ def authenticate(self, remote_user, shib_meta, second_uid=''):
user = None

if user and second_uid:
SocialAuthUser.objects.add(user.username, SHIBBOLETH_PROVIDER_IDENTIFIER,
second_uid)
SocialAuthUser.objects.add_if_not_exist(user.username,
SHIBBOLETH_PROVIDER_IDENTIFIER,
second_uid)

if not user and self.create_unknown_user:
try:
user = User.objects.create_shib_user(is_active=self.activate_after_creation)
SocialAuthUser.objects.add(user.username, SHIBBOLETH_PROVIDER_IDENTIFIER,
remote_user)
SocialAuthUser.objects.add_if_not_exist(user.username,
SHIBBOLETH_PROVIDER_IDENTIFIER,
remote_user)
if second_uid:
SocialAuthUser.objects.add(user.username, SHIBBOLETH_PROVIDER_IDENTIFIER,
second_uid)
SocialAuthUser.objects.add_if_not_exist(user.username,
SHIBBOLETH_PROVIDER_IDENTIFIER,
second_uid)
except Exception as e:
logger.error('create shib user failed: %s' % e)
return None
Expand Down
6 changes: 3 additions & 3 deletions thirdpart/shibboleth/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def process_request(self, request):
if shib_user:
remote_user = shib_user.username
if second_uid:
SocialAuthUser.objects.add(shib_user.username,
SHIBBOLETH_PROVIDER_IDENTIFIER,
second_uid)
SocialAuthUser.objects.add_if_not_exists(shib_user.username,
SHIBBOLETH_PROVIDER_IDENTIFIER,
second_uid)

if request.user.username == remote_user:
if request.user.is_staff:
Expand Down

0 comments on commit f014ff0

Please sign in to comment.