Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a new custom permission that will show in the user admin. #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/wagtail_2fa/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def process_request(self, request):

# Add an attribute to the user so we can easily determine if 2FA should
# be enabled for them.
request.user.enable_2fa = request.user.has_perms(["wagtailadmin.enable_2fa"])
request.user.enable_2fa = request.user.has_perms(["wagtail_2fa.enable_2fa"])

return result

Expand All @@ -107,7 +107,7 @@ def _require_verified_user(self, request):
# 2FA disabled.
user_has_device = django_otp.user_has_device(request.user, confirmed=True)
if not user_has_device and not request.user.has_perms(
["wagtailadmin.enable_2fa"]
["wagtail_2fa.enable_2fa"]
):
return False

Expand Down
43 changes: 43 additions & 0 deletions src/wagtail_2fa/migrations/0002_custom_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.db import migrations


def create_2fa_permissions(apps, schema_editor):
ContentType = apps.get_model('contenttypes.ContentType')
Permission = apps.get_model('auth.Permission')

wagtail_2fa_content_type, created = ContentType.objects.get_or_create(
app_label='wagtail_2fa',
model='admin'
)

# Create 2FA permission
enable_2fa_permission, created = Permission.objects.get_or_create(
content_type=wagtail_2fa_content_type,
codename='enable_2fa',
name='Enable 2FA'
)


def remove_2fa_permissions(apps, schema_editor):
"""Reverse the above additions of permissions."""
ContentType = apps.get_model('contenttypes.ContentType')
Permission = apps.get_model('auth.Permission')
wagtail_2fa_content_type = ContentType.objects.get(
app_label='wagtail_2fa',
model='admin',
)

# This also removes the permission from all groups
Permission.objects.filter(
content_type=wagtail_2fa_content_type,
codename='enable_2fa',
).delete()


class Migration(migrations.Migration):

dependencies = []

operations = [
migrations.RunPython(create_2fa_permissions, remove_2fa_permissions),
]
2 changes: 1 addition & 1 deletion src/wagtail_2fa/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def register_user_listing_buttons(context, user):
def register_2fa_permission():
if "wagtail_2fa.middleware.VerifyUserPermissionsMiddleware" in settings.MIDDLEWARE:
return Permission.objects.filter(
content_type__app_label="wagtailadmin", codename="enable_2fa"
content_type__app_label="wagtail_2fa", codename="enable_2fa"
)

return Permission.objects.none()