Repository: e-valuation/EvaP

Issue:
Username case sensitivity
Usernames are case sensitive. The importer makes all usernames lowercase, but automatically created accounts when logging in with Kerberos authentification can have uppercase letters.
This can lead to two users having the same username and then the system crashed on login.

Automatically created accounts should also get lowercase usernames, even if the user enters the name differently.



Discussion:
```
Internal Server Error: /

MultipleObjectsReturned at /
get() returned more than one UserProfile -- it returned 2!

Traceback:  

File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/opt/evap/evap/evaluation/views.py" in index
  35.         elif login_username_form.is_valid():

File "/usr/local/lib/python3.4/dist-packages/django/forms/forms.py" in is_valid
  161.         return self.is_bound and not self.errors

File "/usr/local/lib/python3.4/dist-packages/django/forms/forms.py" in errors
  153.             self.full_clean()

File "/usr/local/lib/python3.4/dist-packages/django/forms/forms.py" in full_clean
  362.         self._clean_fields()

File "/usr/local/lib/python3.4/dist-packages/django/forms/forms.py" in _clean_fields
  383.                     value = getattr(self, 'clean_%s' % name)()

File "/usr/local/lib/python3.4/dist-packages/django/views/decorators/debug.py" in sensitive_variables_wrapper
  36.             return func(*func_args, **func_kwargs)

File "/opt/evap/evap/evaluation/forms.py" in clean_password
  97.             self.user_cache = authenticate(username=username, password=password)

File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/__init__.py" in authenticate
  74.             user = backend.authenticate(**credentials)

File "/usr/local/lib/python3.4/dist-packages/django_auth_kerberos/backends.py" in authenticate
  29.                     "defaults": { UserModel.USERNAME_FIELD: username }

File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py" in get_or_create
  465.             return self.get(**lookup), False

File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py" in get
  391.             (self.model._meta.object_name, num)

Exception Type: MultipleObjectsReturned at /
Exception Value: get() returned more than one UserProfile -- it returned 2!
```


Resolving patch:
diff --git a/evap/evaluation/forms.py b/evap/evaluation/forms.py
--- a/evap/evaluation/forms.py
+++ b/evap/evaluation/forms.py
@@ -29,6 +29,9 @@ def clean_password(self):
         username = self.cleaned_data.get('username')
         password = self.cleaned_data.get('password')
 
+        # django-auth-kerberos might create a new userprofile. make sure it gets a lowercase username.
+        username = username.lower()
+
         if username and password:
             self.user_cache = authenticate(username=username, password=password)
             if self.user_cache is None:


Test patch: