Skip to content

Commit 9860cbc

Browse files
ericholscheragjohnson
authored andcommitted
Use our standard auth mixin for proxito downloads (#6572)
* Use our standard auth mixin for proxito downloads This is the same logic as in the Proxito views, it just extends it to the downloads. * re-add 404 logic * Add a bit of janky hacks * Remove functions to remove hacky isinstance * Fix lint
1 parent c3001be commit 9860cbc

File tree

3 files changed

+28
-52
lines changed

3 files changed

+28
-52
lines changed

readthedocs/projects/views/public.py

+25-49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from readthedocs.builds.constants import LATEST
3131
from readthedocs.builds.models import Version
3232
from readthedocs.builds.views import BuildTriggerMixin
33+
from readthedocs.core.utils.extend import SettingsOverrideObject
3334
from readthedocs.projects.models import Project
3435
from readthedocs.projects.templatetags.projects_tags import sort_version_aware
3536
from readthedocs.proxito.views.mixins import ServeDocsMixin
@@ -268,7 +269,7 @@ def project_downloads(request, project_slug):
268269
)
269270

270271

271-
class ProjectDownloadMedia(ServeDocsMixin, View):
272+
class ProjectDownloadMediaBase(ServeDocsMixin, View):
272273

273274
# Use new-style URLs (same domain as docs) or old-style URLs (dashboard URL)
274275
same_domain_url = False
@@ -299,19 +300,30 @@ def get(
299300
not the actual Project permissions.
300301
"""
301302
if self.same_domain_url:
302-
version = self._version_same_domain_url(
303+
# It uses the request to get the ``project``. The rest of arguments come
304+
# from the URL.
305+
final_project, lang_slug, version_slug, filename = _get_project_data_from_request( # noqa
303306
request,
304-
type_,
305-
lang_slug,
306-
version_slug,
307-
subproject_slug,
307+
project_slug=None,
308+
subproject_slug=subproject_slug,
309+
lang_slug=lang_slug,
310+
version_slug=version_slug,
308311
)
312+
313+
if not self.allowed_user(request, final_project, version_slug):
314+
return self.get_unauthed_response(request, final_project)
315+
316+
version = get_object_or_404(
317+
final_project.versions.public(user=request.user),
318+
slug=version_slug,
319+
)
320+
309321
else:
310-
version = self._version_dashboard_url(
311-
request,
312-
project_slug,
313-
type_,
314-
version_slug,
322+
# All the arguments come from the URL.
323+
version = get_object_or_404(
324+
Version.objects.public(user=request.user),
325+
project__slug=project_slug,
326+
slug=version_slug,
315327
)
316328

317329
# Send media download to analytics - sensitive data is anonymized
@@ -342,45 +354,9 @@ def get(
342354
download=True,
343355
)
344356

345-
def _version_same_domain_url(
346-
self,
347-
request,
348-
type_,
349-
lang_slug,
350-
version_slug,
351-
subproject_slug=None,
352-
):
353-
"""
354-
Return the version to be served (new-style URLs).
355-
356-
It uses the request to get the ``project``. The rest of arguments come
357-
from the URL.
358-
"""
359-
final_project, lang_slug, version_slug, filename = _get_project_data_from_request( # noqa
360-
request,
361-
project_slug=None,
362-
subproject_slug=subproject_slug,
363-
lang_slug=lang_slug,
364-
version_slug=version_slug,
365-
)
366-
version = get_object_or_404(
367-
final_project.versions.public(user=request.user),
368-
slug=version_slug,
369-
)
370-
return version
371-
372-
def _version_dashboard_url(self, request, project_slug, type_, version_slug):
373-
"""
374-
Return the version to be served (old-style URLs).
375357

376-
All the arguments come from the URL.
377-
"""
378-
version = get_object_or_404(
379-
Version.objects.public(user=request.user),
380-
project__slug=project_slug,
381-
slug=version_slug,
382-
)
383-
return version
358+
class ProjectDownloadMedia(SettingsOverrideObject):
359+
_default_class = ProjectDownloadMediaBase
384360

385361

386362
def project_versions(request, project_slug):

readthedocs/proxito/views/mixins.py

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def _serve_401(self, request, project):
118118
log.debug('Unauthorized access to %s documentation', project.slug)
119119
return res
120120

121+
def allowed_user(self, *args, **kwargs):
122+
return True
123+
121124

122125
class ServeRedirectMixin:
123126

readthedocs/proxito/views/serve.py

-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ def get(self,
142142
path=final_url,
143143
)
144144

145-
def allowed_user(self, *args, **kwargs):
146-
return True
147-
148145

149146
class ServeDocs(SettingsOverrideObject):
150147
_default_class = ServeDocsBase

0 commit comments

Comments
 (0)