44from django .contrib import admin
55from django .contrib .admin import AdminSite
66from django .contrib .auth import REDIRECT_FIELD_NAME
7- from django .contrib .auth .views import redirect_to_login
87from django .core .urlresolvers import reverse
98from django .shortcuts import resolve_url
109from django .utils .http import is_safe_url
1110from django .utils .translation import ugettext
1211
1312from .models import PhoneDevice
14- from .utils import monkeypatch_method
1513from .views import BackupTokensView , LoginView , ProfileView , SetupView
1614
1715
@@ -129,6 +127,9 @@ def has_permission(self, request):
129127 return False
130128 return request .user .is_verified ()
131129
130+
131+ class AdminSiteOTPMixin (object ):
132+
132133 def get_urls (self ):
133134 from django .conf .urls import include , url
134135
@@ -147,7 +148,7 @@ def wrapper(*args, **kwargs):
147148 urlpatterns = [
148149 url (r'^two_factor/' , include (urlpatterns_2fa , namespace = 'two_factor' ))
149150 ]
150- urlpatterns += super (AdminSiteOTPRequiredMixin , self ).get_urls ()
151+ urlpatterns += super (AdminSiteOTPMixin , self ).get_urls ()
151152 return urlpatterns
152153
153154 def login (self , request , extra_context = None ):
@@ -163,32 +164,36 @@ def two_factor_backup_tokens(self, request):
163164 return admin_backup_tokens_view (request )
164165
165166
166- class AdminSiteOTPRequired ( AdminSiteOTPRequiredMixin , AdminSite ):
167+ class AdminSiteOTP ( AdminSiteOTPMixin , AdminSite ):
167168 """
168- AdminSite enforcing OTP verified staff users .
169+ AdminSite using OTP login .
169170 """
170171 pass
171172
172173
173- def patch_admin ():
174- @monkeypatch_method (AdminSite )
175- def login (self , request , extra_context = None ):
176- """
177- Redirects to the site login page for the given HttpRequest.
178- """
179- redirect_to = request .POST .get (REDIRECT_FIELD_NAME , request .GET .get (REDIRECT_FIELD_NAME ))
174+ class AdminSiteOTPRequired (AdminSiteOTPMixin , AdminSiteOTPRequiredMixin , AdminSite ):
175+ """
176+ AdminSite enforcing OTP verified staff users.
177+ """
178+ pass
180179
181- if not redirect_to or not is_safe_url (url = redirect_to , host = request .get_host ()):
182- redirect_to = resolve_url (settings .LOGIN_REDIRECT_URL )
183180
184- return redirect_to_login ( redirect_to )
181+ __default_admin_site__ = None
185182
186183
187- def unpatch_admin ():
188- setattr (AdminSite , 'login' , original_login )
184+ def patch_admin ():
185+ global __default_admin_site__
186+ __default_admin_site__ = admin .site .__class__
187+ if getattr (settings , 'TWO_FACTOR_FORCE_OTP_ADMIN' , False ):
188+ admin .site .__class__ = AdminSiteOTPRequired
189+ else :
190+ admin .site .__class__ = AdminSiteOTP
189191
190192
191- original_login = AdminSite .login
193+ def unpatch_admin ():
194+ global __default_admin_site__
195+ admin .site .__class__ = __default_admin_site__
196+ __default_admin_site__ = None
192197
193198
194199class PhoneDeviceAdmin (admin .ModelAdmin ):
0 commit comments