Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use our standard auth mixin for proxito downloads #6572

Merged
merged 5 commits into from
Jan 23, 2020
Merged
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
74 changes: 25 additions & 49 deletions readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from readthedocs.builds.constants import LATEST
from readthedocs.builds.models import Version
from readthedocs.builds.views import BuildTriggerMixin
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.projects.models import Project
from readthedocs.projects.templatetags.projects_tags import sort_version_aware
from readthedocs.proxito.views.mixins import ServeDocsMixin
Expand Down Expand Up @@ -268,7 +269,7 @@ def project_downloads(request, project_slug):
)


class ProjectDownloadMedia(ServeDocsMixin, View):
class ProjectDownloadMediaBase(ServeDocsMixin, View):

# Use new-style URLs (same domain as docs) or old-style URLs (dashboard URL)
same_domain_url = False
Expand Down Expand Up @@ -299,19 +300,30 @@ def get(
not the actual Project permissions.
"""
if self.same_domain_url:
version = self._version_same_domain_url(
# It uses the request to get the ``project``. The rest of arguments come
# from the URL.
final_project, lang_slug, version_slug, filename = _get_project_data_from_request( # noqa
request,
type_,
lang_slug,
version_slug,
subproject_slug,
project_slug=None,
subproject_slug=subproject_slug,
lang_slug=lang_slug,
version_slug=version_slug,
)

if not self.allowed_user(request, final_project, version_slug):
return self.get_unauthed_response(request, final_project)

version = get_object_or_404(
final_project.versions.public(user=request.user),
slug=version_slug,
)

else:
version = self._version_dashboard_url(
request,
project_slug,
type_,
version_slug,
# All the arguments come from the URL.
version = get_object_or_404(
Version.objects.public(user=request.user),
project__slug=project_slug,
slug=version_slug,
)

# Send media download to analytics - sensitive data is anonymized
Expand Down Expand Up @@ -342,45 +354,9 @@ def get(
download=True,
)

def _version_same_domain_url(
self,
request,
type_,
lang_slug,
version_slug,
subproject_slug=None,
):
"""
Return the version to be served (new-style URLs).

It uses the request to get the ``project``. The rest of arguments come
from the URL.
"""
final_project, lang_slug, version_slug, filename = _get_project_data_from_request( # noqa
request,
project_slug=None,
subproject_slug=subproject_slug,
lang_slug=lang_slug,
version_slug=version_slug,
)
version = get_object_or_404(
final_project.versions.public(user=request.user),
slug=version_slug,
)
return version

def _version_dashboard_url(self, request, project_slug, type_, version_slug):
"""
Return the version to be served (old-style URLs).

All the arguments come from the URL.
"""
version = get_object_or_404(
Version.objects.public(user=request.user),
project__slug=project_slug,
slug=version_slug,
)
return version
class ProjectDownloadMedia(SettingsOverrideObject):
_default_class = ProjectDownloadMediaBase


def project_versions(request, project_slug):
Expand Down
3 changes: 3 additions & 0 deletions readthedocs/proxito/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def _serve_401(self, request, project):
log.debug('Unauthorized access to %s documentation', project.slug)
return res

def allowed_user(self, *args, **kwargs):
return True
Comment on lines +121 to +122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was to default this to False to avoid mistakes if it's not overwritten in the class that inherit from this. Forcing the class that inherit from this to re-define it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, this worried me a bit, we should fix it in another PR.



class ServeRedirectMixin:

Expand Down
3 changes: 0 additions & 3 deletions readthedocs/proxito/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ def get(self,
path=final_url,
)

def allowed_user(self, *args, **kwargs):
return True


class ServeDocs(SettingsOverrideObject):
_default_class = ServeDocsBase
Expand Down