Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
Allow patching/unpatching admin at any time
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Mar 23, 2014
1 parent 663d225 commit 9b7df6e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README.rst
Expand Up @@ -73,8 +73,6 @@ pages. To do this, configure the ``LOGIN_`` settings:
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/federated/login/'

urls.py:
from federated_login import patch_admin

Settings
========
Expand All @@ -98,3 +96,10 @@ These are the customizable settings:

``FL_USER_CLASS`` (Default: ``'django.contrib.auth.models.User'``)
Django model class to used to create and query for users.

``FL_PATCH_ADMIN`` (Default: ``True``)
Django's admin site is automatically patched to use federated login. Set
this setting to ``False`` to disable this feature. You can call
``federated_login.admin.patch_admin()`` and
``federated_login.admin.unpatch_admin()`` to respectively patch and
unpatch the admin site.
1 change: 0 additions & 1 deletion example/urls.py
Expand Up @@ -4,7 +4,6 @@
from example.views import HomeView, ExampleSecretView
import federated_login

from federated_login import patch_admin

admin.autodiscover()

Expand Down
3 changes: 3 additions & 0 deletions federated_login/__init__.py
@@ -1,6 +1,9 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from . import patches # noqa


__all__ = ['FL_SSO_ENDPOINT', 'auth_backends', 'urls', 'views', 'UserClass',
'UserFactory', 'user_factories']

Expand Down
23 changes: 23 additions & 0 deletions federated_login/admin.py
@@ -0,0 +1,23 @@
from django.conf import settings
from django.contrib.admin import AdminSite
from django.shortcuts import redirect

from .utils import monkeypatch_method


def patch_admin():
@monkeypatch_method(AdminSite)
def login(self, request, extra_context=None):
"""
Redirects to the site login for the given HttpRequest
"""
return redirect(str(settings.LOGIN_URL))


def unpatch_admin():
setattr(AdminSite, 'login', original_login)


original_login = AdminSite.login
if getattr(settings, 'FL_PATCH_ADMIN', True):
patch_admin()
14 changes: 4 additions & 10 deletions federated_login/patch_admin.py
@@ -1,11 +1,5 @@
from django.conf import settings
from django.contrib.admin import sites
import warnings

#monkey-patch admin login
from django.shortcuts import redirect


def redirect_admin_login(self, request):
return redirect(settings.LOGIN_URL)

sites.AdminSite.login = redirect_admin_login
warnings.warn("federeated_login.patch_admin has been deprecated in favor of a "
"setting called FL_PATCH_ADMIN, which is set to auto-patch. "
"Please update your settings accordingly.", DeprecationWarning)
9 changes: 2 additions & 7 deletions federated_login/patches.py
Expand Up @@ -15,13 +15,8 @@
from openid.message import OPENID2_NS, OPENID1_NS, no_default
from openid import oidutil

# decorator by Guido
# http://mail.python.org/pipermail/python-dev/2008-January/076194.html
def monkeypatch_method(cls):
def decorator(func):
setattr(cls, func.__name__, func)
return func
return decorator
from .utils import monkeypatch_method


@monkeypatch_method(consumer.GenericConsumer)
def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
Expand Down
6 changes: 6 additions & 0 deletions federated_login/utils.py
@@ -0,0 +1,6 @@
# from http://mail.python.org/pipermail/python-dev/2008-January/076194.html
def monkeypatch_method(cls):
def decorator(func):
setattr(cls, func.__name__, func)
return func
return decorator

0 comments on commit 9b7df6e

Please sign in to comment.