1

I'm using CAS for authentication on my Django site (via django-cas-ng). If I log in through CAS and then visit the admin site, it works fine, but if I visit the admin site when not logged in, it redirects to the admin site's login form instead of to the CAS login page. How can I make it redirect to the CAS login page instead?

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • 1
    something similar to this may work? http://stackoverflow.com/questions/11341663/django-override-admin-sites-login-form – warath-coder Jan 23 '15 at 23:59
  • Adding django-cas-ng middleware to MIDDLEWARE_CLASSES did the trick for me (the doc of the project is not clear on that point) – bformet Jun 25 '15 at 14:47

2 Answers2

3

Adding 'django_cas_ng.middleware.CASMiddleware' to your MIDDLEWARE_CLASSES will automatically redirect the user to the CAS login page instead of the django admin login form.

Example:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django_cas_ng.middleware.CASMiddleware',
    ...
)
bformet
  • 13,465
  • 1
  • 22
  • 25
1

The way I ended up dealing with this was by subclassing AdminSite and overriding admin_view, login, and logout. Instead of redirecting unauthorized users to the admin interface's login view, admin_view now redirects anonymous users to settings.LOGIN_URL, and raises PermissionDenied for authenticated users who aren't staff. login and logout are now simply redirects to settings.LOGIN_URL and settings.LOGOUT_URL.

I put my code to do this on GitHub, and also uploaded it to PyPI under the name django-admin-external-auth, in case anyone else ever needs to solve this problem.

Taymon
  • 24,950
  • 9
  • 62
  • 84