From 122267a11d6a1bd8fafce8430e5bd2f1c5b8faa9 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 14 Jun 2019 11:22:02 +0200 Subject: [PATCH] manager: hide the username column (#33971) --- src/authentic2/manager/user_views.py | 10 ++++++- src/authentic2/manager/utils.py | 9 ++++++- src/authentic2/manager/views.py | 4 ++- tests/test_user_manager.py | 39 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/authentic2/manager/user_views.py b/src/authentic2/manager/user_views.py index 79428d4f..10f73415 100644 --- a/src/authentic2/manager/user_views.py +++ b/src/authentic2/manager/user_views.py @@ -43,7 +43,7 @@ from .tables import UserTable, UserRolesTable, OuUserRolesTable from .forms import (UserSearchForm, UserAddForm, UserEditForm, UserChangePasswordForm, ChooseUserRoleForm, UserRoleSearchForm, UserChangeEmailForm) from .resources import UserResource -from .utils import get_ou_count +from .utils import get_ou_count, has_show_username from . import app_settings @@ -78,6 +78,13 @@ class UsersView(HideOUColumnMixin, BaseTableView): return qs def get_table(self, **kwargs): + show_username = has_show_username() + if show_username and self.is_ou_specified(): + show_username = self.is_ou_specified().show_username + if not show_username: + exclude = kwargs.setdefault('exclude', []) + if 'username' not in exclude: + exclude.append('username') table = super(UsersView, self).get_table(**kwargs) if self.search_form.not_enough_chars(): user_qs = self.search_form.filter_by_ou(self.get_queryset()) @@ -86,6 +93,7 @@ class UsersView(HideOUColumnMixin, BaseTableView): 'limit': self.search_form.minimum_chars, 'user_count': user_qs.count(), } + return table def get_context_data(self, **kwargs): diff --git a/src/authentic2/manager/utils.py b/src/authentic2/manager/utils.py index bf3d00a0..56d52901 100644 --- a/src/authentic2/manager/utils.py +++ b/src/authentic2/manager/utils.py @@ -18,6 +18,8 @@ from django_rbac.utils import get_ou_model from authentic2.decorators import GlobalCache +OU = get_ou_model() + def label_from_user(user): labels = [] @@ -39,4 +41,9 @@ def label_from_user(user): @GlobalCache(timeout=10) def get_ou_count(): - return get_ou_model().objects.count() + return OU.objects.count() + + +@GlobalCache(timeout=10) +def has_show_username(): + return OU.objects.filter(show_username=True).exists() diff --git a/src/authentic2/manager/views.py b/src/authentic2/manager/views.py index cd2873c5..bc9674e1 100644 --- a/src/authentic2/manager/views.py +++ b/src/authentic2/manager/views.py @@ -666,7 +666,9 @@ class HideOUColumnMixin(object): if OU.objects.count() < 2: exclude_ou = True if exclude_ou: - kwargs['exclude'] = ['ou'] + exclude = kwargs.setdefault('exclude', []) + if 'ou' not in exclude: + exclude.append('ou') return super(HideOUColumnMixin, self).get_table(**kwargs) diff --git a/tests/test_user_manager.py b/tests/test_user_manager.py index 8ec21e55..b05de0a2 100644 --- a/tests/test_user_manager.py +++ b/tests/test_user_manager.py @@ -20,12 +20,16 @@ from django.core.urlresolvers import reverse from django.contrib.contenttypes.models import ContentType from django.utils.six import text_type +from django_rbac.utils import get_ou_model + from authentic2.custom_user.models import User from authentic2.models import Attribute, AttributeValue from authentic2.a2_rbac.utils import get_default_ou + from utils import login, get_link_from_mail, skipif_sqlite +OU = get_ou_model() def visible_users(response): @@ -171,3 +175,38 @@ def test_export_csv_disabled_attribute(settings, app, superuser): # disabled attribute should not show up for line in table: assert len(line) == num_col + + +def test_user_table(app, admin, user_ou1, ou1): + from authentic2.manager.utils import has_show_username + + # base state, username are shown + response = login(app, admin, '/manage/users/') + assert response.pyquery('td.username') + + # hide all usernames, from specific and general view + OU.objects.update(show_username=False) + has_show_username.cache.clear() + + response = app.get('/manage/users/') + assert not response.pyquery('td.username') + + response = app.get('/manage/users/?search-ou=%s' % get_default_ou().id) + assert not response.pyquery('td.username') + + response = app.get('/manage/users/?search-ou=%s' % ou1.id) + assert not response.pyquery('td.username') + + # hide username only in the default OU + ou1.show_username = True + ou1.save() + has_show_username.cache.clear() + + response = app.get('/manage/users/') + assert response.pyquery('td.username') + + response = app.get('/manage/users/?search-ou=%s' % get_default_ou().id) + assert not response.pyquery('td.username') + + response = app.get('/manage/users/?search-ou=%s' % ou1.id) + assert response.pyquery('td.username') -- 2.20.1