-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
© Ocado Group | ||
Created on 08/02/2024 at 11:19:37(+00:00). | ||
""" | ||
|
||
import typing as t | ||
|
||
from rest_framework.permissions import IsAuthenticated | ||
|
||
|
||
class IsSelf(IsAuthenticated): | ||
"""Request's user must be the selected user.""" | ||
|
||
def __init__( | ||
self, | ||
lookup_field: str = "pk", | ||
lookup_url_kwarg: t.Optional[str] = None, | ||
): | ||
"""Initialize permission. | ||
Args: | ||
lookup_field: The field used to uniquely identify a user. | ||
lookup_url_kwarg: The key for the url arg used to lookup the user. | ||
""" | ||
|
||
super().__init__() | ||
self.lookup_field = lookup_field | ||
self.lookup_url_kwarg = lookup_url_kwarg or lookup_field | ||
|
||
def __eq__(self, other): | ||
return ( | ||
isinstance(other, self.__class__) | ||
and self.lookup_field == other.lookup_field | ||
and self.lookup_url_kwarg == other.lookup_url_kwarg | ||
) | ||
|
||
def has_permission(self, request, view): | ||
return ( | ||
super().has_permission(request, view) | ||
and getattr(request.user, self.lookup_field) | ||
== view.kwargs[self.lookup_url_kwarg] | ||
) |