Skip to content

QuerySets: check for .is_superuser instead of has_perm #8181

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

Merged
merged 1 commit into from
May 13, 2021
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
16 changes: 9 additions & 7 deletions readthedocs/builds/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from django.db.models import Q
from django.utils import timezone

from readthedocs.builds.constants import (
BUILD_STATE_FINISHED,
BUILD_STATE_TRIGGERED,
)
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.projects import constants
from readthedocs.projects.models import Project

from .constants import BUILD_STATE_FINISHED, BUILD_STATE_TRIGGERED

log = logging.getLogger(__name__)


Expand All @@ -25,7 +27,7 @@ class VersionQuerySetBase(models.QuerySet):
use_for_related_fields = True

def _add_user_repos(self, queryset, user):
if user.has_perm('builds.view_version'):
if user.is_superuser:
return self.all()
if user.is_authenticated:
projects_pk = user.projects.all().values_list('pk', flat=True)
Expand Down Expand Up @@ -72,8 +74,8 @@ class BuildQuerySetBase(models.QuerySet):

use_for_related_fields = True

def _add_user_repos(self, queryset, user=None):
if user.has_perm('builds.view_version'):
def _add_user_repos(self, queryset, user):
if user.is_superuser:
return self.all()
if user.is_authenticated:
projects_pk = user.projects.all().values_list('pk', flat=True)
Expand Down Expand Up @@ -163,8 +165,8 @@ class RelatedBuildQuerySetBase(models.QuerySet):

use_for_related_fields = True

def _add_user_repos(self, queryset, user=None):
if user.has_perm('builds.view_version'):
def _add_user_repos(self, queryset, user):
if user.is_superuser:
return self.all()
if user.is_authenticated:
projects_pk = user.projects.all().values_list('pk', flat=True)
Expand Down
9 changes: 4 additions & 5 deletions readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from readthedocs.builds.constants import EXTERNAL
from readthedocs.core.utils.extend import SettingsOverrideObject

from . import constants
from readthedocs.projects import constants


class ProjectQuerySetBase(models.QuerySet):
Expand All @@ -17,7 +16,7 @@ class ProjectQuerySetBase(models.QuerySet):
use_for_related_fields = True

def _add_user_repos(self, queryset, user):
if user.has_perm('projects.view_project'):
Copy link
Member

Choose a reason for hiding this comment

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

Does anyone have these permissions on .com or .org? I assume there was a reason we did it this way, but I don't remember :x

Copy link
Member

@ericholscher ericholscher May 13, 2021

Choose a reason for hiding this comment

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

Looks like we had a comment:

# Hack around get_objects_for_user not supporting global perms

https://github.com/readthedocs/readthedocs.org/pull/2954/files#diff-05611882195df182851df952b312f031a4314bce04a104e10a09655e570515f3R82

Looks like get_objects_for_user isn't used anymore, so probably safe.

Copy link
Member Author

Choose a reason for hiding this comment

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

Only the build user has that permission set

In [10]: User.objects.filter(user_permissions__isnull=False).distinct()
Out[10]: <QuerySet [<User: build>]>

That's in .org, in .com we have 0 users.

if user.is_superuser:
return self.all()
if user.is_authenticated:
user_queryset = user.projects.all()
Expand Down Expand Up @@ -157,8 +156,8 @@ class RelatedProjectQuerySetBase(models.QuerySet):
use_for_related_fields = True
project_field = 'project'

def _add_user_repos(self, queryset, user=None):
if user.has_perm('projects.view_project'):
def _add_user_repos(self, queryset, user):
if user.is_superuser:
return self.all()
if user.is_authenticated:
projects_pk = user.projects.all().values_list('pk', flat=True)
Expand Down