Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
corps committed Aug 12, 2023
1 parent 623bc23 commit f663d8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
38 changes: 10 additions & 28 deletions src/sentry/features/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Callable, Optional, Sequence
from typing import TYPE_CHECKING, Any, Callable, Sequence

from rest_framework.request import Request
from rest_framework.response import Response
Expand All @@ -18,16 +18,11 @@ def any_organization_has_feature(
return any([features.has(feature, organization, **kwargs) for organization in organizations])


def requires_feature(
feature: str, any_org: Optional[bool] = None
) -> Callable[[EndpointFunc], EndpointFunc]:
def requires_feature(feature: str) -> Callable[[EndpointFunc], EndpointFunc]:
"""
Require a feature flag to access an endpoint.
If ``any_org`` is ``True``, this will check all of the request User's
Organizations for the flag. If any are flagged in, the endpoint is accessible.
Without ``any_org=True``, the endpoint must resolve an Organization via
The endpoint must resolve an Organization via
``convert_args`` (and therefor be in ``kwargs``). The resolved Org must have
the passed feature.
Expand All @@ -41,26 +36,13 @@ def requires_feature(

def decorator(func: EndpointFunc) -> EndpointFunc:
def wrapped(self: Any, request: Request, *args: Any, **kwargs: Any) -> Response:
# The endpoint is accessible if any of the User's Orgs have the feature
# flag enabled.
if any_org:
if not any_organization_has_feature(
feature,
Organization.objects.get_for_user_ids({request.user.id}),
actor=request.user,
):
return Response(status=404)

return func(self, request, *args, **kwargs)
# The Org in scope for the request must have the feature flag enabled.
else:
if "organization" not in kwargs:
return Response(status=404)

if not features.has(feature, kwargs["organization"], actor=request.user):
return Response(status=404)

return func(self, request, *args, **kwargs)
if "organization" not in kwargs:
return Response(status=404)

if not features.has(feature, kwargs["organization"], actor=request.user):
return Response(status=404)

return func(self, request, *args, **kwargs)

return wrapped

Expand Down
13 changes: 0 additions & 13 deletions tests/sentry/features/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ def get(self, request, *args, **kwargs):
response = get(None, self.request, organization=self.org)
assert response.status_code == 200

def test_any_org_true_when_users_other_org_has_flag_succeeds(self):
# The Org in scope of the request does not have the flag, but another
# Org the User belongs to does.
#
with org_with_feature(self.out_of_scope_org, "foo"):

@requires_feature("foo", any_org=True)
def get(self, request, *args, **kwargs):
return Response()

response = get(None, self.request, organization=self.org)
assert response.status_code == 200

def test_any_org_false_when_users_other_org_has_flag_fails(self):
with org_with_feature(self.out_of_scope_org, "foo"):

Expand Down

0 comments on commit f663d8d

Please sign in to comment.