Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/kb-audiences.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
12 changes: 8 additions & 4 deletions kitsune/wiki/kb_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions kitsune/wiki/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
30 changes: 24 additions & 6 deletions kitsune/wiki/tests/test_kb_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,42 @@ 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",
[
("Your knowledge bases", ["Northwind"]),
("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]))
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions kitsune/wiki/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand Down
Loading