Add support for django-allauth

This commit is contained in:
Marco Ceppi 2019-08-13 11:01:17 -04:00
parent cca2c6b680
commit 2e92df7fa5
8 changed files with 73 additions and 3 deletions

View File

@ -2,6 +2,16 @@ from django.conf import settings
from django.contrib.admin import AdminSite from django.contrib.admin import AdminSite
from django.contrib.auth.admin import GroupAdmin, UserAdmin from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group, User
from django.contrib.sites.admin import SiteAdmin
from django.contrib.sites.models import Site
from allauth.socialaccount.admin import (
SocialApp,
SocialAppAdmin,
SocialAccount,
SocialAccountAdmin,
SocialToken,
SocialTokenAdmin,
)
class NetBoxAdminSite(AdminSite): class NetBoxAdminSite(AdminSite):
@ -18,6 +28,12 @@ admin_site = NetBoxAdminSite(name='admin')
# Register external models # Register external models
admin_site.register(Group, GroupAdmin) admin_site.register(Group, GroupAdmin)
admin_site.register(User, UserAdmin) admin_site.register(User, UserAdmin)
admin_site.register(Site, SiteAdmin)
# Register allauth models
admin_site.register(SocialApp, SocialAppAdmin)
admin_site.register(SocialAccount, SocialAccountAdmin)
admin_site.register(SocialToken, SocialTokenAdmin)
# Modify the template to include an RQ link if django_rq is installed (see RQ_SHOW_ADMIN_LINK) # Modify the template to include an RQ link if django_rq is installed (see RQ_SHOW_ADMIN_LINK)
if settings.WEBHOOKS_ENABLED: if settings.WEBHOOKS_ENABLED:

View File

@ -160,9 +160,14 @@ INSTALLED_APPS = [
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.humanize', 'django.contrib.humanize',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.github',
'cacheops', 'cacheops',
'corsheaders', 'corsheaders',
'debug_toolbar', 'debug_toolbar',
@ -234,6 +239,8 @@ TEMPLATES = [
# Authentication # Authentication
AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [
'utilities.auth_backends.ViewExemptModelBackend', 'utilities.auth_backends.ViewExemptModelBackend',
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
] ]
# Internationalization # Internationalization
@ -391,6 +398,19 @@ FILTERS_NULL_CHOICE_LABEL = 'None'
FILTERS_NULL_CHOICE_VALUE = 'null' FILTERS_NULL_CHOICE_VALUE = 'null'
#
# Django Site
#
SITE_ID = 1
#
# Django Allauth
#
ACCOUNT_DEFAULT_HTTP_PROTOCOL='https'
LOGIN_REDIRECT_URL = 'home'
# #
# Django REST framework (API) # Django REST framework (API)
# #

View File

@ -31,6 +31,9 @@ _patterns = [
path(r'login/', LoginView.as_view(), name='login'), path(r'login/', LoginView.as_view(), name='login'),
path(r'logout/', LogoutView.as_view(), name='logout'), path(r'logout/', LogoutView.as_view(), name='logout'),
# AllAuth
path(r'accounts/', include('allauth.urls')),
# Apps # Apps
path(r'circuits/', include('circuits.urls')), path(r'circuits/', include('circuits.urls')),
path(r'dcim/', include('dcim.urls')), path(r'dcim/', include('dcim.urls')),

View File

@ -0,0 +1,20 @@
{% load socialaccount %}
{% get_providers as socialaccount_providers %}
{% for provider in socialaccount_providers %}
{% if provider.id == "openid" %}
{% for brand in provider.get_brands %}
<li>
<a title="{{brand.name}}" class="btn btn-block btn-social btn-md btn-{{ provider.id }} socialaccount_provider {{provider.id}} {{brand.id}}" href="{% provider_login_url provider.id openid=brand.openid_url process=process %}">
<i class="fa fa-{{ provider.id }}"></i>Log in with {{ brand.name }}
</a>
</li>
{% endfor %}
{% endif %}
<li>
<a title="{{provider.name}}" class="btn btn-block btn-social btn-md btn-{{ provider.id }} socialaccount_provider {{provider.id}}" href="{% provider_login_url provider.id process=process scope=scope auth_params=auth_params %}">
<i class="fa fa-{{ provider.id }}"></i>Log in with {{ provider.name }}
</a>
</li>
{% endfor %}

View File

@ -1,5 +1,7 @@
{% extends '_base.html' %} {% extends '_base.html' %}
{% load form_helpers %} {% load form_helpers %}
{% load account socialaccount %}
{% block content %} {% block content %}
<div class="row" style="margin-top: {% if settings.BANNER_LOGIN %}100{% else %}150{% endif %}px;"> <div class="row" style="margin-top: {% if settings.BANNER_LOGIN %}100{% else %}150{% endif %}px;">
@ -23,6 +25,14 @@
<strong>Log In</strong> <strong>Log In</strong>
</div> </div>
<div class="panel-body"> <div class="panel-body">
{% get_providers as socialaccount_providers %}
{% if socialaccount_providers %}
<div class="socialaccount_ballot">
<ul class="socialaccount_providers">
{% include "extras/provider_list.html" with process="login" %}
</ul>
</div>
{% endif %}
{% csrf_token %} {% csrf_token %}
{% if 'next' in request.GET %}<input type="hidden" name="next" value="{{ request.GET.next }}" />{% endif %} {% if 'next' in request.GET %}<input type="hidden" name="next" value="{{ request.GET.next }}" />{% endif %}
{% if 'next' in request.POST %}<input type="hidden" name="next" value="{{ request.POST.next }}" />{% endif %} {% if 'next' in request.POST %}<input type="hidden" name="next" value="{{ request.POST.next }}" />{% endif %}

View File

@ -23,8 +23,8 @@
</li> </li>
</ul> </ul>
</div> </div>
<div class="col-sm-9 col-md-6"> <div class="col-sm-9 col-md-6">
{% block usercontent %}{% endblock %} {% block usercontent %}{% endblock %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -22,7 +22,7 @@ class LoginRequiredMiddleware(object):
# Redirect unauthenticated requests to the login page. API requests are exempt from redirection as the API # Redirect unauthenticated requests to the login page. API requests are exempt from redirection as the API
# performs its own authentication. Also metrics can be read without login. # performs its own authentication. Also metrics can be read without login.
api_path = reverse('api-root') api_path = reverse('api-root')
if not request.path_info.startswith((api_path, '/metrics')) and request.path_info != settings.LOGIN_URL: if not request.path_info.startswith((api_path, '/metrics', '/accounts')) and request.path_info != settings.LOGIN_URL:
return HttpResponseRedirect( return HttpResponseRedirect(
'{}?next={}'.format( '{}?next={}'.format(
settings.LOGIN_URL, settings.LOGIN_URL,

View File

@ -1,4 +1,5 @@
Django>=2.2,<2.3 Django>=2.2,<2.3
django-allauth==0.39.1
django-cacheops==4.1 django-cacheops==4.1
django-cors-headers==3.0.2 django-cors-headers==3.0.2
django-debug-toolbar==2.0 django-debug-toolbar==2.0