Created
February 9, 2024 15:24
-
-
Save Johnetordoff/d1af3651733dc33a249c9e637469b257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import ( | |
exceptions, | |
permissions, | |
) | |
from addon_service.models import ( | |
ResourceReference, | |
UserReference, | |
) | |
from app.authentication import authenticate_resource | |
class UserReferenceIsAuthenticatedBase(permissions.BasePermission): | |
""" | |
A permission class that ensures the user_reference_iri in the session matches | |
the iri of the user associated with the user. | |
""" | |
def has_permission(self, request, view): | |
user_reference_iri = request.session.get("user_reference_iri") | |
referenced_user = self.get_referenced_user(request, view) | |
return user_reference_iri == referenced_user.user | |
def get_referenced_user(self, request): | |
try: | |
return UserReference.objects.get( | |
user_uri=request.session.get("user_reference_iri") | |
) | |
except UserReference.DoesNotExist: | |
raise exceptions.NotAuthenticated() | |
class BaseConfiguredStorageAddonPermission(UserReferenceIsAuthenticatedBase): | |
""" | |
A permission class that ensures the resource_reference_iri in the session matches | |
the iri of the user associated with the resource. | |
""" | |
def get_referenced_user(self, request, view): | |
if request.method == "POST" and request.data: | |
resource = authenticate_resource( | |
request=request, | |
iri=request.data["authorized_resource"]["id"], | |
required_permission="admin", | |
create=True, | |
) | |
csa_with_this_resource = resource.configured_storage_addons.filter( | |
base_account__authorized_resource=resource | |
) | |
return csa_with_this_resource.get(owners__user_uri=request.session['user_reference_uri']) | |
... # other methods maybe class based |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment