Merge pull request #5769 from smithellis/1566-anonymous-user-zd-for-main

Anonymous user doesn't overwrite username
This commit is contained in:
smith 2023-11-27 23:07:42 -05:00 коммит произвёл GitHub
Родитель 6eae139e3c 9834dc3db4
Коммит 0fbbced29c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 23 добавлений и 0 удалений

Просмотреть файл

@ -21,6 +21,14 @@ class ZendeskClient(object):
self.client = Zenpy(**creds)
def _user_to_zendesk_user(self, user, email):
"""Given a Django user, return a Zendesk user."""
# If the user already exists in Zendesk return
# the Zendesk user object
# instead of creating a new one
if zuser := self.get_user_by_email(email):
return zuser
# If the user is not authenticated, we can't save anything to
# AnonymousUser Profile as it has none
if not user.is_authenticated:
name = "Anonymous User"
locale = "en-US"
@ -45,6 +53,21 @@ class ZendeskClient(object):
external_id=external_id,
)
def get_user_by_email(self, email):
"""Given an email, return a user from Zendesk."""
# This returns a generator, but we only want/expect one user
# If it returns more than one, we should fail
# Otherwise return the Zendesk user object
search_results = self.client.search(type="user", query=f"email:{email}")
user_found = None
for user in search_results:
if user_found is not None:
raise ValueError(f"Found more than one user with email {email}")
user_found = user
return user_found
def create_user(self, user, email=""):
"""Given a Django user, create a user in Zendesk."""
zendesk_user = self._user_to_zendesk_user(user, email=email)