Skip to content

Commit

Permalink
Add custom admin login handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmaanT committed Sep 8, 2019
1 parent 9289282 commit 8b5d0a8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -60,6 +60,7 @@ Example:
'CLIENT_SECRET': 'secret',
'REDIRECT_URI': 'example',
'ADMIN_PERMISSION': 'example_admin'
'CUSTOM_ADMIN': True
}
The available settings are:
Expand All @@ -76,6 +77,8 @@ The available settings are:

``ADMIN_PERMISSION`` The name of the permission on platform to grant admin access. Defaults to ``example_admin``

``CUSTOM_ADMIN`` enable the custom admin login page to log in users through platform. Defaults to ``True``

|
When developing with an http (not https) callback URL, it may be helpful to set the ``OAUTHLIB_INSECURE_TRANSPORT`` environment variable.
Expand Down
21 changes: 21 additions & 0 deletions accounts/admin.py
@@ -0,0 +1,21 @@
from django.contrib import admin
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.contrib.auth.models import Group, User
from django.shortcuts import redirect
from django.urls import reverse

from accounts.settings import accounts_settings


class LabsAdminSite(admin.AdminSite):

def login(self, request, extra_context=None):
if not request.user.is_authenticated:
return redirect(reverse('accounts:login') + '?next=' + request.GET.get('next'))
return super().login(request, extra_context)


if accounts_settings.CUSTOM_ADMIN:
admin.site = LabsAdminSite()
admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)
3 changes: 2 additions & 1 deletion accounts/settings.py
Expand Up @@ -11,7 +11,8 @@
'REDIRECT_URI': os.environ.get('LABS_REDIRECT_URI'),
'SCOPE': ['read', 'introspection'],
'PLATFORM_URL': 'https://platform.pennlabs.org',
'ADMIN_PERMISSION': 'example_admin'
'ADMIN_PERMISSION': 'example_admin',
'CUSTOM_ADMIN': True
}


Expand Down
17 changes: 17 additions & 0 deletions tests/test_admin.py
@@ -0,0 +1,17 @@
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse


class AppsTestCase(TestCase):
def test_admin_not_logged_in(self):
response = self.client.get(reverse('admin:login') + '?next=/admin/')
redirect = '/accounts/login/?next=/admin/'
self.assertRedirects(response, redirect, fetch_redirect_response=False)

def test_admin_logged_in(self):
get_user_model().objects.create_user(username='user', password='password', is_staff=True)
self.client.login(username='user', password='password')
response = self.client.get(reverse('admin:login') + '?next=/admin/')
redirect = '/admin/'
self.assertRedirects(response, redirect, fetch_redirect_response=False)
6 changes: 5 additions & 1 deletion tests/urls.py
@@ -1,4 +1,8 @@
from django.contrib import admin
from django.urls import include, path


urlpatterns = [path('accounts/', include('accounts.urls', namespace='accounts'))]
urlpatterns = [
path('accounts/', include('accounts.urls', namespace='accounts')),
path('admin/', admin.site.urls),
]

0 comments on commit 8b5d0a8

Please sign in to comment.