Add "other permissions" to group permissions

- instead of a separate table containing all discrete permissions for each object, show these in the object's table
- Wagtail admin access will still show inside 'other permissions'
- resolves #5482
pull/7444/head
Kamil Marut 2021-09-01 08:05:58 +02:00 zatwierdzone przez LB Johnston
rodzic 1bfe22efcb
commit 8d42bec5e6
7 zmienionych plików z 83 dodań i 4 usunięć

Wyświetl plik

@ -27,6 +27,7 @@ Changelog
* Added `ExcelDateFormatter` to `wagtail.admin.views.mixins` so that dates in Excel exports will appear in the locale's `SHORT_DATETIME_FORMAT` (Andrew Stone)
* Add TIDAL support to the list of oEmbed providers (Wout De Puysseleir)
* Add `label_format` attribute to customise the label shown for a collapsed StructBlock (Matt Westcott)
* User Group permissions will now show all custom object permissions in one row instead of a separate table (Kamil Marut)User Group permissions editing in the admin will now show all custom object permissions in one row instead of a separate table (Kamil Marut)
* Fix: Delete button is now correct colour on snippets and modeladmin listings (Brandon Murch)
* Fix: Ensure that StreamBlock / ListBlock-level validation errors are counted towards error counts (Matt Westcott)
* Fix: InlinePanel add button is now keyboard navigatable (Jesse Menn)

Wyświetl plik

@ -540,6 +540,7 @@ Contributors
* Nick Moreton
* Bryan Williams
* Wout De Puysseleir
* Kamil Marut
Translators
===========

Wyświetl plik

@ -38,6 +38,7 @@ Other features
* Added ``ExcelDateFormatter`` to ``wagtail.admin.views.mixins`` so that dates in Excel exports will appear in the locale's ``SHORT_DATETIME_FORMAT`` (Andrew Stone)
* Add TIDAL support to the list of oEmbed providers (Wout De Puysseleir)
* Add ``label_format`` attribute to customise the label shown for a collapsed StructBlock (Matt Westcott)
* User Group permissions editing in the admin will now show all custom object permissions in one row instead of a separate table (Kamil Marut)
Bug fixes
~~~~~~~~~

Wyświetl plik

@ -16,6 +16,15 @@
// stylelint-disable-next-line declaration-no-important
font-size: 2.5em !important;
}
.custom-permissions {
padding-bottom: 0;
}
.custom-permissions-item {
font-weight: inherit;
width: 100%;
}
}
.page-permissions-listing {

Wyświetl plik

@ -8,12 +8,18 @@
<col width="15%" />
<col width="15%" />
<col width="15%" />
{% if custom_perms_exist %}
<col width="30%" />
{% endif %}
<thead>
<tr>
<th>{% trans "Name" %}</th>
<th>{% trans "Add" %}</th>
<th>{% trans "Change" %}</th>
<th>{% trans "Delete" %}</th>
{% if custom_perms_exist %}
<th>{% trans "Custom permissions" %}</th>
{% endif %}
</tr>
</thead>
<tbody>
@ -38,6 +44,21 @@
{{ content_perms_dict.delete.checkbox.tag }}
{% endif %}
</td>
{% if custom_perms_exist %}
<td>
{% if content_perms_dict.custom %}
<fieldset class="custom-permissions">
<legend>{% trans "Custom permissions" %}</legend>
{% for custom_perm in content_perms_dict.custom %}
<label class="custom-permissions-item">
<input type="checkbox" name="permissions" value="{{ custom_perm.perm.id }}" {% if custom_perm.selected %}checked{% endif %}>
{{ custom_perm.name }}
</label>
{% endfor %}
</fieldset>
{% endif %}
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>

Wyświetl plik

@ -1,4 +1,5 @@
import itertools
import re
from django import VERSION as DJANGO_VERSION
from django import template
@ -21,6 +22,7 @@ def format_permissions(permission_bound_field):
'add': checkbox
'change': checkbox
'delete': checkbox
'custom': list_of_checkboxes_for_custom_permissions
},
]
@ -57,27 +59,40 @@ def format_permissions(permission_bound_field):
object_perms = []
other_perms = []
custom_perms_exist = False
for content_type_id in content_type_ids:
content_perms = permissions.filter(content_type_id=content_type_id)
content_perms_dict = {}
custom_perms = []
if content_perms[0].content_type.name == "admin":
perm = content_perms[0]
other_perms.append((perm, checkboxes_by_id[perm.id]))
continue
for perm in content_perms:
content_perms_dict['object'] = perm.content_type.name
checkbox = checkboxes_by_id[perm.id]
# identify the three main categories of permission, and assign to
# the relevant dict key, else bung in the 'other_perms' list
permission_action = perm.codename.split('_')[0]
if permission_action in ['add', 'change', 'delete']:
content_perms_dict['object'] = perm.content_type.name
content_perms_dict[permission_action] = {
'perm': perm, 'checkbox': checkbox,
}
else:
other_perms.append((perm, checkbox))
if content_perms_dict:
object_perms.append(content_perms_dict)
custom_perms_exist = True
custom_perms.append({'perm': perm,
'name': re.sub(f"{perm.content_type.name}$", "", perm.name, flags=re.I).strip(),
'selected': checkbox.data['selected']})
content_perms_dict['custom'] = custom_perms
object_perms.append(content_perms_dict)
return {
'object_perms': object_perms,
'other_perms': other_perms,
'custom_perms_exist': custom_perms_exist
}

Wyświetl plik

@ -7,6 +7,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.core.exceptions import ImproperlyConfigured
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import Q
from django.http import HttpRequest, HttpResponse
from django.test import TestCase, override_settings
from django.urls import reverse
@ -1232,6 +1233,23 @@ class TestGroupCreateView(TestCase, WagtailTestUtils):
0
)
def test_custom_permissions_hidden(self):
# Remove all permissions that show up in the 'custom permissions' column
Permission.objects.exclude(
Q(codename__startswith="add")
| Q(codename__startswith="change")
| Q(codename__startswith="delete")
).delete()
response = self.get()
self.assertTagInHTML('<fieldset class="custom-permissions">', str(response.content), count=0)
def test_custom_permissions_shown(self):
response = self.get()
self.assertTagInHTML('<fieldset class="custom-permissions">', str(response.content))
class TestGroupEditView(TestCase, WagtailTestUtils):
def setUp(self):
@ -1595,6 +1613,19 @@ class TestGroupEditView(TestCase, WagtailTestUtils):
self.assertEqual(self.test_group.permissions.count(), 1)
self.assertEqual(self.test_group.permissions.all()[0], self.non_registered_perm)
def test_is_custom_permission_checked(self):
# Add a permission from the 'custom permission' column to the user's group
custom_permission = Permission.objects.get(codename="view_person")
self.test_group.permissions.add(custom_permission)
response = self.get()
self.assertTagInHTML(
'<label class="custom-permissions-item"><input type="checkbox" name="permissions" value="%s" checked>'
% custom_permission.id,
str(response.content),
)
class TestGroupViewSet(TestCase):
def setUp(self):