0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 09:33:54 +01:00

Allow Page.permissions_for_user() to be overridden by specific page types

This commit is contained in:
SebCorbin 2023-07-19 15:04:30 +02:00 committed by Sage Abdullah
parent db0ee5992d
commit 094834909d
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
6 changed files with 71 additions and 0 deletions

View File

@ -10,6 +10,7 @@ Changelog
* Improve layout and accessibility of the image URL generator page, reduce reliance on JavaScript (Temidayo Azeez)
* Allow `UniqueConstraint` in place of `unique_together` for `TranslatableMixin`'s system check (Temidayo Azeez, Sage Abdullah)
* Make use of `IndexView.get_add_url()` in snippets index view template (Christer Jensen, Sage Abdullah)
* Allow `Page.permissions_for_user()` to be overridden by specific page types (Sébastien Corbin)
* Fix: Update system check for overwriting storage backends to recognise the `STORAGES` setting introduced in Django 4.2 (phijma-leukeleu)
* Fix: Prevent password change form from raising a validation error when browser autocomplete fills in the "Old password" field (Chiemezuo Akujobi)
* Fix: Ensure that the legacy dropdown options, when closed, do not get accidentally clicked by other interactions wide viewports (CheesyPhoenix, Christer Jensen)

View File

@ -20,6 +20,7 @@ depth: 1
* Improve layout and accessibility of the image URL generator page, reduce reliance on JavaScript (Temidayo Azeez)
* Allow `UniqueConstraint` in place of `unique_together` for {class}`~wagtail.models.TranslatableMixin`'s system check (Temidayo Azeez, Sage Abdullah)
* Make use of `IndexView.get_add_url()` in snippets index view template (Christer Jensen, Sage Abdullah)
* Allow `Page.permissions_for_user()` to be overridden by specific page types (Sébastien Corbin)
### Bug fixes

View File

@ -2377,6 +2377,17 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
"""
Return a PagePermissionsTester object defining what actions the user can perform on this page
"""
# Allow specific classes to override this method, but only cast to the
# specific instance if it's not already specific and if the method has
# been overridden. This helps improve performance when working with
# base Page querysets.
is_overridden = (
self.specific_class
and self.specific_class.permissions_for_user
!= type(self).permissions_for_user
)
if is_overridden and not isinstance(self, self.specific_class):
return self.specific_deferred.permissions_for_user(user)
return PagePermissionTester(user, self)
def is_previewable(self):

View File

@ -0,0 +1,35 @@
# Generated by Django 4.2.3 on 2023-07-19 12:59
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
("wagtailcore", "0088_fix_log_entry_json_timestamps"),
("tests", "0031_searchtestmodel"),
]
operations = [
migrations.CreateModel(
name="CustomPermissionPage",
fields=[
(
"page_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="wagtailcore.page",
),
),
],
options={
"abstract": False,
},
bases=("wagtailcore.page",),
),
]

View File

@ -70,6 +70,7 @@ from wagtail.models import (
Orderable,
Page,
PageManager,
PagePermissionTester,
PageQuerySet,
PreviewableMixin,
RevisionMixin,
@ -2189,3 +2190,12 @@ class SearchTestModel(models.Model):
def __str__(self):
return self.title
class CustomPermissionTester(PagePermissionTester):
pass
class CustomPermissionPage(Page):
def permissions_for_user(self, user):
return CustomPermissionTester(user, self)

View File

@ -16,6 +16,8 @@ from wagtail.models import (
from wagtail.permission_policies.pages import PagePermissionPolicy
from wagtail.test.testapp.models import (
BusinessSubIndex,
CustomPermissionPage,
CustomPermissionTester,
EventIndex,
EventPage,
SingletonPageViaMaxCount,
@ -844,6 +846,17 @@ class TestPagePermission(TestCase):
self.assertFalse(editor_perms.can_lock())
self.assertFalse(editor_perms.can_unlock())
def test_custom_permission_tester_page(self):
homepage = Page.objects.get(url_path="/home/")
instance = CustomPermissionPage(
title="This page has a custom permission tester",
slug="page-with-custom-permission-tester",
)
homepage.add_child(instance=instance)
page = Page.objects.get(pk=instance.pk)
user = get_user_model().objects.get(email="eventeditor@example.com")
self.assertIsInstance(page.permissions_for_user(user), CustomPermissionTester)
class TestPagePermissionTesterCanCopyTo(TestCase):
"""Tests PagePermissionTester.can_copy_to()"""