Skip to content

Commit 4c432a9

Browse files
committed
feat: remove monkey patching
1 parent 60b44d2 commit 4c432a9

5 files changed

Lines changed: 27 additions & 62 deletions

File tree

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66

77
### Changed
88

9+
### Removed
10+
11+
- Admin Monkey Patching
12+
13+
The Admin UI will not longer be automatically patched. The `TwoFactorSiteAdmin` will need to be explicitly
14+
configured in urls.py.
15+
16+
```py
17+
# urls.py
18+
from django.urls import path
19+
from two_factor.admin import TwoFactorAdminSite
20+
url_patterns = [
21+
path('admin/', TwoFactorAdminSite().urls),
22+
]
23+
```
24+
25+
Custom admin sites can extend `TwoFactorSiteAdmin` or `TwoFactorSideAdminMixin` to inherit the behavior.
26+
27+
```py
28+
# admin.py
29+
class MyCustomAdminSite(TwoFactorSiteAdminMixin, AdminSite):
30+
# implement your customizations here.
31+
pass
32+
```
33+
934

1035
## 1.14.0
1136

@@ -35,6 +60,7 @@
3560
- The QR code now always uses a white background to support pages displayed
3661
with a dark theme.
3762

63+
3864
### Removed
3965

4066
- Python 3.5 and 3.6 support

docs/configuration.rst

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ Configuration
44
General Settings
55
----------------
66

7-
``TWO_FACTOR_PATCH_ADMIN`` (default: ``True``)
8-
Whether the Django admin is patched to use the default login view.
9-
10-
.. warning::
11-
The admin currently does not enforce one-time passwords being set for
12-
admin users.
13-
147
``LOGIN_URL``
158
Should point to the login view provided by this application as described in
169
setup. This login view handles password authentication followed by a one-time
@@ -123,7 +116,7 @@ Next, add additional urls to your config:
123116
124117
# urls.py
125118
from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls
126-
119+
127120
urlpatterns = [
128121
path('', include(tf_twilio_urls)),
129122
...

tests/test_admin.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@
55
from django.test import TestCase
66
from django.test.utils import override_settings
77

8-
from two_factor.admin import patch_admin, unpatch_admin
9-
108
from .utils import UserMixin
119

1210

1311
@override_settings(ROOT_URLCONF='tests.urls_admin')
1412
class TwoFactorAdminSiteTest(UserMixin, TestCase):
15-
16-
def setUp(self):
17-
patch_admin()
18-
19-
def tearDown(self):
20-
unpatch_admin()
21-
22-
def test(self):
23-
response = self.client.get('/admin/', follow=True)
24-
redirect_to = '%s?next=/admin/' % reverse('admin:login')
25-
self.assertRedirects(response, redirect_to)
26-
27-
28-
@override_settings(ROOT_URLCONF='tests.urls_admin')
29-
class AdminPatchTest(TestCase):
3013
"""
3114
otp_admin is admin console that needs OTP for access.
3215
Only admin users (is_staff and is_active)
@@ -55,7 +38,6 @@ def test_anonymous_get_admin_login(self):
5538
response = self.client.get(login_url, follow=True)
5639
self.assertEqual(response.status_code, 200)
5740

58-
5941
def test_is_staff_not_verified_not_setup_get_admin_index_redirects_to_setup(self):
6042
"""
6143
admins without MFA setup should be redirected to the setup page.

two_factor/admin.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def redirect_to_mfa_setup(self, request):
4343
# have MFA enabled on their account. We're going to redirect them
4444
# to the MFA setup.
4545

46-
# TODO: Add redirect_to functionality to MFA setup.
4746
# TODO: Add message indicating why the user was directed or setup and MFA required
4847
# interstitial page to explain to the user they need to setup MFA.
4948
setup_url = reverse('two_factor:setup')
@@ -133,32 +132,3 @@ class AdminSiteOTPRequired(TwoFactorAdminSite):
133132
warnings.warn('AdminSiteOTPRequired is deprecated by TwoFactorAdminSite, please update.',
134133
category=DeprecationWarning)
135134
pass
136-
137-
138-
def patch_admin():
139-
warnings.warn('two-factor admin patching will be removed, use TwoFactorAdminSite or TwoFactorAdminSiteMixin.',
140-
category=DeprecationWarning)
141-
# overrides
142-
setattr(AdminSite, 'login', TwoFactorAdminSiteMixin.login)
143-
setattr(AdminSite, 'admin_view', TwoFactorAdminSiteMixin.admin_view)
144-
setattr(AdminSite, 'has_permission', TwoFactorAdminSiteMixin.has_permission)
145-
# additions
146-
setattr(AdminSite, 'has_admin_permission', original_has_permission)
147-
setattr(AdminSite, 'has_mfa_setup', TwoFactorAdminSiteMixin.has_mfa_setup)
148-
setattr(AdminSite, 'redirect_to_mfa_setup', TwoFactorAdminSiteMixin.redirect_to_mfa_setup)
149-
150-
151-
def unpatch_admin():
152-
warnings.warn('django-two-factor admin patching is deprecated, use TwoFactorAdminSite or TwoFactorAdminSiteMixin.',
153-
category=DeprecationWarning)
154-
# we really only need unpatching in our tests so this can be a noop.
155-
# overrides
156-
setattr(AdminSite, 'login', original_login)
157-
setattr(AdminSite, 'admin_view', original_admin_view)
158-
setattr(AdminSite, 'has_permission', original_has_permission)
159-
# NOTE: this unpatching doesn't really work, but becuase it just patches in our mixin it isn't harmful.
160-
161-
162-
original_login = AdminSite.login
163-
original_admin_view = AdminSite.admin_view
164-
original_has_permission = AdminSite.has_permission

two_factor/apps.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from django.apps import AppConfig
2-
from django.conf import settings
32

43

54
class TwoFactorConfig(AppConfig):
65
name = 'two_factor'
76
verbose_name = "Django Two Factor Authentication"
8-
9-
def ready(self):
10-
if getattr(settings, 'TWO_FACTOR_PATCH_ADMIN', True):
11-
from .admin import patch_admin
12-
patch_admin()

0 commit comments

Comments
 (0)