From c9ab64223f2972c93460459982afbaa92d47fc56 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 31 Jul 2020 17:08:36 +0200
Subject: [PATCH] manager: display parent roles OU on the role members page
 (#43269)

---
 src/authentic2/manager/role_views.py                      | 6 ++++--
 .../templates/authentic2/manager/role_members.html        | 8 +++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/authentic2/manager/role_views.py b/src/authentic2/manager/role_views.py
index 22594e1c..7aa9df54 100644
--- a/src/authentic2/manager/role_views.py
+++ b/src/authentic2/manager/role_views.py
@@ -19,17 +19,17 @@ import json
 from django.core.exceptions import PermissionDenied
 from django.utils.translation import ugettext_lazy as _
 from django.urls import reverse
 from django.views.generic import FormView, TemplateView
 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
 
 from authentic2.utils import redirect
 from authentic2 import hooks, data_transfer
 
 from . import tables, views, resources, forms, app_settings
@@ -190,17 +190,19 @@ class RoleMembersView(views.HideOUColumnMixin, RoleViewMixin, views.BaseSubTable
         if app_settings.ROLE_MEMBERS_FROM_OU:
             kwargs['ou'] = self.object.ou
         return kwargs
 
     def get_context_data(self, **kwargs):
         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').desc(nulls_first=True)))
+        ctx['is_several_parent_ous'] = len(set(x.ou for x in self.object.parents(include_self=False)))
         ctx['admin_roles'] = views.filter_view(self.request,
                                                self.object.get_admin_role().children(include_self=False,
                                                                                      annotate=True))
         ctx['from_ldap'] = self._can_manage_members and not self.can_manage_members
         return ctx
 
 members = RoleMembersView.as_view()
 
diff --git a/src/authentic2/manager/templates/authentic2/manager/role_members.html b/src/authentic2/manager/templates/authentic2/manager/role_members.html
index 6ccfe7bf..da22a7ea 100644
--- a/src/authentic2/manager/templates/authentic2/manager/role_members.html
+++ b/src/authentic2/manager/templates/authentic2/manager/role_members.html
@@ -112,17 +112,23 @@
     <a rel="popup" href="{% url "a2-manager-role-add-child" pk=object.pk %}" class="role-add icon-add-sign"></a>
   {% else %}
     <a title="{% trans "Permission denied" %}" class="disabled role-add icon-add-sign"></a>
   {% endif %}
  </div>
  <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 is_several_parent_ous %}
+       {{ parent.ou }} - {{ parent }}
+       {% else %}
+       {{ parent }}
+       {% endif %}
+     </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>
        {% else %}
          <a title="{% trans "This role is technical, you cannot modify its permissions." %}" class="disabled role-add icon-minus-sign"></a>
        {% endif %}
      {% else %}
        <a title="{% trans "Indirect parent role" %}" class="disabled role-remove icon-minus-sign"></a>
-- 
2.27.0

