diff --git a/docs/kb-audiences.md b/docs/kb-audiences.md index 5ca60a8a832..12a2d234bda 100644 --- a/docs/kb-audiences.md +++ b/docs/kb-audiences.md @@ -106,8 +106,8 @@ the same way. | `Audience.objects` | Returns | Staff bypass | | --- | --- | --- | -| `listed_for(user)` | Menu, directory and search filter: listed public KBs, and listed private KBs with one of the viewer's groups | No, so Staff menus don't list every customer | -| `browsable_by(user)` | KB pages and the `kb=` search scope | Yes | +| `listed_for(user)` | Menu and search filter: listed public KBs, and listed private KBs with one of the viewer's groups | No, so Staff menus don't list every customer | +| `browsable_by(user)` | The directory, KB pages and the `kb=` search scope | Yes: every listed KB, with or without groups | | `private_for(user)` | Private KBs the viewer can read, listed or not; the KBs they can upload private images to | Yes | | `has_private_kb_access(user)` (function) | Whether the viewer can read any private KB | Yes | diff --git a/kitsune/wiki/kb_views.py b/kitsune/wiki/kb_views.py index c4fe2bb34b7..8a08174e274 100644 --- a/kitsune/wiki/kb_views.py +++ b/kitsune/wiki/kb_views.py @@ -8,7 +8,7 @@ from kitsune.products.models import Product, Topic from kitsune.sumo.templatetags.jinja_helpers import urlparams from kitsune.sumo.urlresolvers import reverse -from kitsune.sumo.utils import mark_private, paginate +from kitsune.sumo.utils import in_staff_group, mark_private, paginate from kitsune.wiki.config import DOCUMENTS_PER_PAGE, REDIRECT_HTML from kitsune.wiki.models import Audience, Document, DocumentAudience from kitsune.wiki.utils import group_by_visibility @@ -19,13 +19,17 @@ @require_GET def knowledge_bases(request): - """The directory of the knowledge bases listed for the user.""" - listed = list(Audience.objects.listed_for(request.user)) + """The directory of the knowledge bases the user can open: every listed one for Staff.""" + sees_every_kb = request.user.is_superuser or in_staff_group(request.user) + listed = list(Audience.objects.browsable_by(request.user)) shows_private = any(not knowledge_base.is_public for knowledge_base in listed) response = render( request, "wiki/knowledge_bases.html", - {"sections": group_by_visibility(listed), "suppress_analytics": shows_private}, + { + "sections": group_by_visibility(listed, all_private=sees_every_kb), + "suppress_analytics": shows_private, + }, ) if shows_private: mark_private(response) diff --git a/kitsune/wiki/managers.py b/kitsune/wiki/managers.py index dbaf4f7b485..7cfac6e99c3 100644 --- a/kitsune/wiki/managers.py +++ b/kitsune/wiki/managers.py @@ -229,9 +229,9 @@ def _listed(self, condition): def listed_for(self, user): """ - The knowledge bases the user sees in menus, the directory and search filters: listed - public ones, and listed private ones with a group in the user's effective groups. - Membership only, so Staff don't list every customer's knowledge base. + The knowledge bases the user sees in menus and search filters: listed public ones, and + listed private ones with a group in the user's effective groups. Membership only, so + Staff menus don't list every customer's knowledge base. """ condition = Q(is_public=True) group_ids = effective_group_ids(user) @@ -241,8 +241,8 @@ def listed_for(self, user): def browsable_by(self, user): """ - The listed knowledge bases whose page and search filter the user may open: those the - user lists, plus every listed one for Staff and superusers. + The listed knowledge bases whose page, directory entry and search filter the user may + open: those the user lists, plus every listed one for Staff and superusers. """ if user and (user.is_superuser or in_staff_group(user)): return self._listed(Q()) diff --git a/kitsune/wiki/tests/test_kb_views.py b/kitsune/wiki/tests/test_kb_views.py index 16aeff82303..9ddc16185b5 100644 --- a/kitsune/wiki/tests/test_kb_views.py +++ b/kitsune/wiki/tests/test_kb_views.py @@ -288,7 +288,12 @@ def test_empty_knowledge_base(self): class KnowledgeBaseDirectoryTests(TestCase): @parameterized.expand( [ - ("anonymous", [("Open to everyone", ["Alpha", "Zulu"])], []), + ( + "anonymous", + [("Open to everyone", ["Alpha", "Zulu"])], + [], + ["Northwind", "Another company", "Staff handbook"], + ), ( "member", [ @@ -296,16 +301,29 @@ class KnowledgeBaseDirectoryTests(TestCase): ("Open to everyone", ["Alpha", "Zulu"]), ], ["Northwind"], + ["Another company", "Staff handbook"], + ), + ( + "staff", + [ + ( + "Private knowledge bases", + ["Another company", "Northwind", "Staff handbook"], + ), + ("Open to everyone", ["Alpha", "Zulu"]), + ], + ["Another company", "Northwind", "Staff handbook"], + [], ), - ("staff", [("Open to everyone", ["Alpha", "Zulu"])], []), ] ) - def test_groups_by_visibility(self, viewer, expected_groups, locked): + def test_groups_by_visibility(self, viewer, expected_groups, locked, hidden): company = GroupFactory() AudienceFactory(name="Zulu", is_public=True, is_listed=True) AudienceFactory(name="Alpha", is_public=True, is_listed=True) AudienceFactory(name="Northwind", is_listed=True, groups=[company]) AudienceFactory(name="Another company", is_listed=True, groups=[GroupFactory()]) + AudienceFactory(name="Staff handbook", is_listed=True) AudienceFactory(name="Not listed", is_public=True) if viewer == "member": self.client.force_login(UserFactory(groups=[company])) @@ -324,10 +342,10 @@ def test_groups_by_visibility(self, viewer, expected_groups, locked): ) self.assertEqual(_locked_names(doc, "#kb-directory .card--kb", ".card--title"), locked) content = response.content.decode() - self.assertNotIn("Another company", content) - self.assertNotIn("Not listed", content) + for name in [*hidden, "Not listed"]: + self.assertNotIn(name, content) - @parameterized.expand([("anonymous", False), ("staff", False), ("member", True)]) + @parameterized.expand([("anonymous", False), ("staff", True), ("member", True)]) @override_settings(CACHE_MIDDLEWARE_SECONDS=3600, GTM_CONTAINER_ID="GTM-TEST") def test_listing_private_knowledge_bases_is_not_cached_referred_or_tracked( self, viewer, private diff --git a/kitsune/wiki/utils.py b/kitsune/wiki/utils.py index 0ad5ff4fc76..1fc9b12cfb6 100644 --- a/kitsune/wiki/utils.py +++ b/kitsune/wiki/utils.py @@ -384,19 +384,26 @@ def build_topics_data(request: HttpRequest, product: Product, topics: list[Topic return topics_data -def group_by_visibility(knowledge_bases): +def group_by_visibility(knowledge_bases, all_private=False): """ Group knowledge bases as the menu and the directory show them: the private ones, then the public ones, each group sorted by name. Groups without knowledge bases are left out. + "all_private" heads the private group for a viewer who sees every private one, not only + their own. """ private = [] public = [] for knowledge_base in sorted(knowledge_bases, key=lambda kb: kb.name.casefold()): (public if knowledge_base.is_public else private).append(knowledge_base) - groups = [ + if all_private: + # L10n: Heading over every private knowledge base, in the directory Staff see. + private_heading = _lazy("Private knowledge bases") + else: # L10n: Heading over the private knowledge bases the user belongs to, in the main menu and the directory. - (_lazy("Your knowledge bases"), private), + private_heading = _lazy("Your knowledge bases") + groups = [ + (private_heading, private), # L10n: Heading over the public knowledge bases, in the main menu and the directory. (_lazy("Open to everyone"), public), ]