diff --git a/scrutiny/settings.py b/scrutiny/settings.py index 8bc91a9..6466b85 100644 --- a/scrutiny/settings.py +++ b/scrutiny/settings.py @@ -107,6 +107,38 @@ TEMPLATES = [ }, ] +# Authentication settings +try: + import mellon +except ImportError: + mellon = None + +if mellon is not None: + INSTALLED_APPS += ('mellon',) + AUTHENTICATION_BACKENDS = ( + 'mellon.backends.SAMLBackend', + 'django.contrib.auth.backends.ModelBackend', + ) + +LOGIN_URL = '/login/' +LOGIN_REDIRECT_URL = '/' +LOGOUT_URL = '/logout/' + +MELLON_ATTRIBUTE_MAPPING = { + 'email': '{attributes[email][0]}', + 'first_name': '{attributes[first_name][0]}', + 'last_name': '{attributes[last_name][0]}', +} + +MELLON_SUPERUSER_MAPPING = { + 'is_superuser': 'true', +} + +MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}' + +MELLON_IDENTITY_PROVIDERS = [] + + REDMINE_REFERENCE_PLATFORM = 'SaaS2 / Test' diff --git a/scrutiny/templates/registration/login.html b/scrutiny/templates/registration/login.html new file mode 100644 index 0000000..d72d47e --- /dev/null +++ b/scrutiny/templates/registration/login.html @@ -0,0 +1,10 @@ +{% extends "scrutiny/base.html" %} +{% load gadjo i18n %} + +{% block content %} +
+{% csrf_token %} +{{ form|as_template }} + +
+{% endblock %} diff --git a/scrutiny/urls.py b/scrutiny/urls.py index eb4d29f..8341693 100644 --- a/scrutiny/urls.py +++ b/scrutiny/urls.py @@ -4,9 +4,6 @@ from django.conf.urls.static import static from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns -from django.contrib.auth.views import logout_then_login -from django.contrib.auth.urls import urlpatterns as auth_urls - import scrutiny.views from scrutiny.projects.urls import urlpatterns as projects_urls @@ -16,9 +13,12 @@ urlpatterns = [ url(r'^$', scrutiny.views.home, name='home'), url(r'^projects/', include(projects_urls)), url(r'^admin/', include(admin.site.urls)), - url(r'^accounts/logout/', logout_then_login), - url(r'^accounts/', include(auth_urls)), + url(r'^logout/$', scrutiny.views.logout, name='auth_logout'), + url(r'^login/$', scrutiny.views.login, name='auth_login'), ] urlpatterns += staticfiles_urlpatterns() urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + +if 'mellon' in settings.INSTALLED_APPS: + urlpatterns.append(url(r'^accounts/mellon/', include('mellon.urls'))) diff --git a/scrutiny/views.py b/scrutiny/views.py index 8bf707e..d901c9a 100644 --- a/scrutiny/views.py +++ b/scrutiny/views.py @@ -1,7 +1,40 @@ +from django.conf import settings +from django.contrib.auth import logout as auth_logout +from django.contrib.auth import views as auth_views +from django.http import HttpResponseRedirect +from django.shortcuts import resolve_url +from django.utils.six.moves.urllib.parse import quote from django.views.generic.base import TemplateView from .projects.models import Project + +if 'mellon' in settings.INSTALLED_APPS: + from mellon.utils import get_idps +else: + 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=' + + 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 Home(TemplateView): template_name = 'scrutiny/home.html'