File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 7
7
8
8
from .allow_none import AllowNone
9
9
from .is_cron_request_from_google import IsCronRequestFromGoogle
10
+ from .is_self import IsSelf
10
11
from .operators import AND , NOT , OR , Permission
Original file line number Diff line number Diff line change
1
+ """
2
+ © Ocado Group
3
+ Created on 08/02/2024 at 11:19:37(+00:00).
4
+ """
5
+
6
+ import typing as t
7
+
8
+ from rest_framework .permissions import IsAuthenticated
9
+
10
+
11
+ class IsSelf (IsAuthenticated ):
12
+ """Request's user must be the selected user."""
13
+
14
+ def __init__ (
15
+ self ,
16
+ lookup_field : str = "pk" ,
17
+ lookup_url_kwarg : t .Optional [str ] = None ,
18
+ ):
19
+ """Initialize permission.
20
+
21
+ Args:
22
+ lookup_field: The field used to uniquely identify a user.
23
+ lookup_url_kwarg: The key for the url arg used to lookup the user.
24
+ """
25
+
26
+ super ().__init__ ()
27
+ self .lookup_field = lookup_field
28
+ self .lookup_url_kwarg = lookup_url_kwarg or lookup_field
29
+
30
+ def __eq__ (self , other ):
31
+ return (
32
+ isinstance (other , self .__class__ )
33
+ and self .lookup_field == other .lookup_field
34
+ and self .lookup_url_kwarg == other .lookup_url_kwarg
35
+ )
36
+
37
+ def has_permission (self , request , view ):
38
+ return (
39
+ super ().has_permission (request , view )
40
+ and getattr (request .user , self .lookup_field )
41
+ == view .kwargs [self .lookup_url_kwarg ]
42
+ )
You can’t perform that action at this time.
0 commit comments