manager: hide the username column (#33971)

This commit is contained in:
Benjamin Dauvergne 2019-06-14 11:22:02 +02:00
parent 407f4c8c22
commit e28e30fe93
4 changed files with 59 additions and 3 deletions

View File

@ -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 not 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):

View File

@ -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 not OU.objects.filter(show_username=False).exists()

View File

@ -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)

View File

@ -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 except in OU1
ou1.show_username = True
ou1.save()
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 response.pyquery('td.username')