manager: display parent roles OU on the role members page (#43269)

This commit is contained in:
Nicolas Roche 2020-07-31 17:08:36 +02:00 committed by Frédéric Péters
parent 9a695869af
commit d1dda9c392
3 changed files with 49 additions and 3 deletions

View File

@ -24,7 +24,7 @@ from django.views.generic.detail import SingleObjectMixin
from django.contrib import messages
from django.contrib.contenttypes.models import ContentType
from django.db.models.query import Q, Prefetch
from django.db.models import Count
from django.db.models import Count, F
from django.contrib.auth import get_user_model
from django_rbac.utils import get_role_model, get_permission_model, get_ou_model
@ -196,7 +196,9 @@ class RoleMembersView(views.HideOUColumnMixin, RoleViewMixin, views.BaseSubTable
ctx = super(RoleMembersView, self).get_context_data(**kwargs)
ctx['children'] = views.filter_view(self.request,
self.object.children(include_self=False, annotate=True))
ctx['parents'] = views.filter_view(self.request, self.object.parents(include_self=False, annotate=True))
ctx['parents'] = views.filter_view(self.request, self.object.parents(
include_self=False, annotate=True).order_by(F('ou').asc(nulls_first=True), 'name'))
ctx['has_multiple_ou'] = get_ou_model().objects.count() > 1
ctx['admin_roles'] = views.filter_view(self.request,
self.object.get_admin_role().children(include_self=False,
annotate=True))

View File

@ -117,7 +117,9 @@
<div class="role-inheritance gadjo-folding">
{% trans "Parent roles:" %}
{% for parent in parents %}
<a class="role" href="{% url "a2-manager-role-members" pk=parent.pk %}">{{ parent }}</a>
<a class="role" href="{% url "a2-manager-role-members" pk=parent.pk %}">
{% if parent.ou and has_multiple_ou %}{{ parent.ou }} - {% endif %}{{ parent }}
</a>
{% if parent.direct %}
{% if not object.is_internal %}
<a rel="popup" href="{% url "a2-manager-role-remove-parent" pk=object.pk parent_pk=parent.pk %}" class="role-remove icon-minus-sign"></a>

View File

@ -1048,3 +1048,45 @@ def test_manager_widgets_field_id_other_user(app, admin, simple_user, simple_rol
# anymous user receive 404
app.session.flush()
select2_json = request_select2(app, response, get_kwargs={'status': 404})
def test_display_parent_roles_on_role_page(app, superuser, settings):
ou1 = get_default_ou()
ou1.name = ('ou1')
ou1.save()
child = Role.objects.create(name='child', slug='role', ou=ou1)
parent1 = Role.objects.create(name='parent1', slug='role1', ou=None)
parent2 = Role.objects.create(name='parent2', slug='role2', ou=ou1)
child.add_parent(parent1)
child.add_parent(parent2)
child.save()
# do not display roles if we have a single OU
url = reverse('a2-manager-role-members', kwargs={'pk': child.pk})
login(app, superuser)
response = app.get(url, status=200)
parent_roles_html = response.html.find_all('div', {'class': 'role-inheritance'})[3]
assert 'Parent roles:' in parent_roles_html.text
assert [x.text.strip() for x in parent_roles_html.find_all('a', {'class': 'role'})] == \
['parent1', 'parent2']
# display parent roles if we have multiple OUs
ou2 = OU.objects.create(name='ou2')
response = app.get(url, status=200)
parent_roles_html = response.html.find_all('div', {'class': 'role-inheritance'})[3]
assert 'Parent roles:' in parent_roles_html.text
assert [x.text.strip() for x in parent_roles_html.find_all('a', {'class': 'role'})] == \
['parent1', 'ou1 - parent2']
# display parent roles sorted by OU
parent3 = Role.objects.create(name='parent3', slug='role3', ou=ou2)
child.add_parent(parent3)
parent4 = Role.objects.create(name='parent4', slug='role4', ou=ou1)
child.add_parent(parent4)
child.save()
response = app.get(url, status=200)
parent_roles_html = response.html.find_all('div', {'class': 'role-inheritance'})[3]
assert 'Parent roles:' in parent_roles_html.text
assert [x.text.strip() for x in parent_roles_html.find_all('a', {'class': 'role'})] == \
['parent1', 'ou1 - parent2', 'ou1 - parent4', 'ou2 - parent3']