commit 78775031d473998abfd9a390798f467e97cbbeed
Author: Serghei Mihai <smihai@entrouvert.com>
Date:   Sat Jul 9 13:33:13 2016 +0200

    authentication views fix

diff --git a/corbo/settings.py b/corbo/settings.py
index a70dacc..96947da 100644
--- a/corbo/settings.py
+++ b/corbo/settings.py
@@ -107,18 +107,36 @@ RSS_LINK_TEMPLATE = '/#announce{0}'
 # default mass emails expeditor
 CORBO_DEFAULT_FROM_EMAIL = 'webmaster@localhost'
 
-# django-mellon settings
+
+# Authentication settings
+try:
+    import mellon
+except ImportError:
+    mellon = None
+
+if mellon is not None:
+    AUTHENTICATION_BACKENDS = (
+        'mellon.backends.SAMLBackend',
+        'django.contrib.auth.backends.ModelBackend',
+    )
+
+LOGIN_URL = '/login/'
+LOGIN_REDIRECT_URL = '/'
+LOGOUT_URL = '/logout/'
+
 MELLON_ATTRIBUTE_MAPPING = {
-    'username': '{attributes[username][0]}',
     'email': '{attributes[email][0]}',
     'first_name': '{attributes[first_name][0]}',
     'last_name': '{attributes[last_name][0]}',
 }
 
-if 'mellon' in INSTALLED_APPS:
-    AUTHENTICATION_BACKENDS = ('mellon.backends.SAMLBackend', )
-    LOGIN_URL = 'mellon_login'
-    LOGOUT_URL = 'mellon_logout'
+MELLON_SUPERUSER_MAPPING = {
+    'is_superuser': 'true',
+}
+
+MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}'
+
+MELLON_IDENTITY_PROVIDERS = []
 
 local_settings_file = os.environ.get('CORBO_SETTINGS_FILE',
         os.path.join(os.path.dirname(__file__), 'local_settings.py'))
diff --git a/corbo/templates/corbo/base.html b/corbo/templates/corbo/base.html
index 6cd2d77..c4bc7b4 100644
--- a/corbo/templates/corbo/base.html
+++ b/corbo/templates/corbo/base.html
@@ -7,7 +7,7 @@
 {% endblock %}
 
 {% block logout-url %}
-{% url "logout" %}
+{% url "auth_logout" %}
 {% endblock %}
 
 {% block site-title %} {% trans "Announces" %}{% endblock %}
diff --git a/corbo/urls.py b/corbo/urls.py
index c52dfd4..227499f 100644
--- a/corbo/urls.py
+++ b/corbo/urls.py
@@ -5,7 +5,7 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns
 from django.contrib import admin
 
 from .urls_utils import decorated_includes, manager_required
-from .views import homepage, atom, unsubscribe, unsubscription_done
+from .views import homepage, atom, unsubscribe, unsubscription_done, login, logout
 
 from manage_urls import urlpatterns as manage_urls
 from api_urls import urlpatterns as api_urls
@@ -22,18 +22,12 @@ urlpatterns = patterns('',
         name='unsubscription_done'),
     url(r'^unsubscribe/(?P<unsubscription_token>[\w:-]+)$', unsubscribe,
         name='unsubscribe'),
+    url(r'^logout/$', logout, name='auth_logout'),
+    url(r'^login/$', login, name='auth_login'),
 )
 
 if 'mellon' in settings.INSTALLED_APPS:
-    # authentication with django-mellon
-    urlpatterns += patterns('',
-        url(r'^accounts/mellon/', include('mellon.urls')),
-    )
-else:
-    urlpatterns += patterns('',
-        url('^accounts/', include('django.contrib.auth.urls')),
-    )
-
+    urlpatterns += patterns('', url(r'^accounts/mellon/', include('mellon.urls')))
 
 urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
 urlpatterns += staticfiles_urlpatterns()
diff --git a/corbo/views.py b/corbo/views.py
index 27335bf..73109ab 100644
--- a/corbo/views.py
+++ b/corbo/views.py
@@ -1,4 +1,5 @@
 from datetime import datetime
+import urllib
 
 from django.conf import settings
 from django.core import signing
@@ -6,13 +7,41 @@ from django.core.urlresolvers import reverse
 from django.views.generic import CreateView, UpdateView, DeleteView, \
                                  ListView, TemplateView
 from django.contrib.syndication.views import Feed
+from django.shortcuts import resolve_url
 from django.utils.feedgenerator import Atom1Feed
 from django.utils.http import urlencode
 from django.http import Http404
+from django.contrib.auth import logout as auth_logout
+from django.contrib.auth import views as auth_views
+from django.http import HttpResponseRedirect
 
 import models
 from .forms import AnnounceForm, CategoryForm
 
+try:
+    from mellon.utils import get_idps
+except ImportError:
+    get_idps = lambda: []
+
+def login(request, *args, **kwargs):
+    if any(get_idps()):
+        if not 'next' in request.GET:
+            return HttpResponseRedirect(resolve_url('mellon_login'))
+        return HttpResponseRedirect(resolve_url('mellon_login') + '?next='
+                                    + urllib.quote(request.GET.get('next')))
+    return auth_views.login(request, *args, **kwargs)
+
+def logout(request, next_page=None):
+    if any(get_idps()):
+        return HttpResponseRedirect(resolve_url('mellon_logout'))
+    auth_logout(request)
+    if next_page is not None:
+        next_page = resolve_url(next_page)
+    else:
+        next_page = '/'
+    return HttpResponseRedirect(next_page)
+
+
 class HomepageView(ListView):
     model = models.Announce
     template_name = 'corbo/homepage.html'
