Created
May 25, 2015 15:50
-
-
Save josephabrahams/2fcaf93fe67d12d255fb to your computer and use it in GitHub Desktop.
Prevent permission escalation in Django admin when granting “user change” permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from django.contrib.auth.admin import UserAdmin | |
from django.contrib.auth.models import User | |
class RestrictedUserAdmin(UserAdmin): | |
""" | |
Prevent permission escalation in when granting “user change” permissions | |
See: http://stackoverflow.com/a/2298268 | |
""" | |
staff_fieldsets = ( | |
(None, {'fields': ('username', 'password')}), | |
('Personal info', {'fields': ('first_name', 'last_name', 'email')}), | |
# No permissions | |
('Important dates', {'fields': ('last_login', 'date_joined')}), | |
('Groups', {'fields': ('groups',)}), | |
) | |
def change_view(self, request, *args, **kwargs): | |
# for non-superuser | |
if not request.user.is_superuser: | |
try: | |
self.fieldsets = self.staff_fieldsets | |
response = UserAdmin.change_view(self, request, *args, **kwargs) | |
finally: | |
# Reset fieldsets to its original value | |
self.fieldsets = UserAdmin.fieldsets | |
return response | |
else: | |
return UserAdmin.change_view(self, request, *args, **kwargs) | |
admin.site.unregister(User) | |
admin.site.register(User, RestrictedUserAdmin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment