Improve redaction and anonymous user handling

This commit is contained in:
Mark Veidemanis 2022-08-03 21:56:27 +01:00
parent 8b7fef59c5
commit fc86aae119
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 20 additions and 20 deletions

View File

@ -1,5 +1,4 @@
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from opensearchpy import OpenSearch from opensearchpy import OpenSearch
from opensearchpy.exceptions import RequestError from opensearchpy.exceptions import RequestError
@ -80,9 +79,11 @@ def filter_blacklisted(user, response):
""" """
response["redacted"] = 0 response["redacted"] = 0
response["exemption"] = None response["exemption"] = None
is_anonymous = isinstance(user, AnonymousUser) if user.is_superuser:
response["exemption"] = True
# is_anonymous = isinstance(user, AnonymousUser)
# For every hit from ES # For every hit from ES
for item in list(response["hits"]["hits"]): for index, item in enumerate(list(response["hits"]["hits"])):
# For every blacklisted type # For every blacklisted type
for blacklisted_type in settings.OPENSEARCH_BLACKLISTED.keys(): for blacklisted_type in settings.OPENSEARCH_BLACKLISTED.keys():
# Check this field we are matching exists # Check this field we are matching exists
@ -92,17 +93,20 @@ def filter_blacklisted(user, response):
for blacklisted_item in settings.OPENSEARCH_BLACKLISTED[ for blacklisted_item in settings.OPENSEARCH_BLACKLISTED[
blacklisted_type blacklisted_type
]: ]:
if blacklisted_item in str(content): if blacklisted_item == str(content):
# Remove the item # Remove the item
if item in response["hits"]["hits"]: if item in response["hits"]["hits"]:
# Anonymous # Anonymous
if is_anonymous: if user.is_anonymous:
response["hits"]["hits"].remove(item) response["hits"]["hits"].remove(item)
else: else:
if not user.is_superuser: if not user.is_superuser:
response["hits"]["hits"].remove(item) response["hits"]["hits"].remove(item)
else: else:
response["exemption"] = True response["hits"]["hits"][index]["_source"][
"exemption"
] = True
# Let the UI know something was redacted # Let the UI know something was redacted
response["redacted"] += 1 response["redacted"] += 1
@ -136,9 +140,9 @@ def query_results(request, size=None):
Accept a HTTP request object. Run the query, and annotate the Accept a HTTP request object. Run the query, and annotate the
results with the other data we have. results with the other data we have.
""" """
is_anonymous = isinstance(request.user, AnonymousUser) # is_anonymous = isinstance(request.user, AnonymousUser)
if is_anonymous: if request.user.is_anonymous:
sizes = ["5", "10", "15", "20"] sizes = settings.OPENSEARCH_MAIN_SIZES_ANON
else: else:
sizes = settings.OPENSEARCH_MAIN_SIZES sizes = settings.OPENSEARCH_MAIN_SIZES
if not size: if not size:
@ -148,13 +152,10 @@ def query_results(request, size=None):
return False return False
if "query" in request.POST: if "query" in request.POST:
query = request.POST["query"] query = request.POST["query"]
if hasattr(request, "user"):
user = request.user
else:
user = None
results = run_main_query( results = run_main_query(
client, client,
user, # passed through run_main_query to filter_blacklisted request.user, # passed through run_main_query to filter_blacklisted
query, query,
size=size, size=size,
) )

View File

@ -28,8 +28,9 @@
</thead> </thead>
<tbody> <tbody>
{% for item in results %} {% for item in results %}
<tr> {% if item.exemption == True %}
{% if item.type == 'join' %} <tr class="has-background-grey-lighter">
{% elif item.type == 'join' %}
<tr class="has-background-success-light"> <tr class="has-background-success-light">
{% elif item.type == 'quit' %} {% elif item.type == 'quit' %}
<tr class="has-background-danger-light"> <tr class="has-background-danger-light">

View File

@ -1,7 +1,6 @@
import json import json
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
@ -24,9 +23,8 @@ class Drilldown(View):
def get(self, request): def get(self, request):
# if not request.user.has_plan(self.plan_name): # if not request.user.has_plan(self.plan_name):
# return render(request, "denied.html") # return render(request, "denied.html")
is_anonymous = isinstance(request.user, AnonymousUser) if request.user.is_anonymous:
if is_anonymous: sizes = settings.OPENSEARCH_MAIN_SIZES_ANON
sizes = ["5", "10", "15", "20"]
else: else:
sizes = settings.OPENSEARCH_MAIN_SIZES sizes = settings.OPENSEARCH_MAIN_SIZES
context = { context = {