2424from rest_framework .response import Response
2525
2626from api import models , serializers
27-
28- from django .conf import settings
27+ from api .permissions import *
2928
3029
3130class AnonymousAuthentication (BaseAuthentication ):
@@ -38,100 +37,6 @@ def authenticate(self, request):
3837 return user , None
3938
4039
41- class IsAnonymous (permissions .BasePermission ):
42- """
43- View permission to allow anonymous users.
44- """
45-
46- def has_permission (self , request , view ):
47- """
48- Return `True` if permission is granted, `False` otherwise.
49- """
50- return type (request .user ) is AnonymousUser
51-
52-
53- class IsOwner (permissions .BasePermission ):
54- """
55- Object-level permission to allow only owners of an object to access it.
56- Assumes the model instance has an `owner` attribute.
57- """
58-
59- def has_object_permission (self , request , view , obj ):
60- if hasattr (obj , 'owner' ):
61- return obj .owner == request .user
62- else :
63- return False
64-
65-
66- class IsAppUser (permissions .BasePermission ):
67- """
68- Object-level permission to allow owners or collaborators to access
69- an app-related model.
70- """
71- def has_object_permission (self , request , view , obj ):
72- if isinstance (obj , models .App ) and obj .owner == request .user :
73- return True
74- elif hasattr (obj , 'app' ) and obj .app .owner == request .user :
75- return True
76- elif request .user .has_perm ('use_app' , obj ):
77- return request .method != 'DELETE'
78- elif hasattr (obj , 'app' ) and request .user .has_perm ('use_app' , obj .app ):
79- return request .method != 'DELETE'
80- else :
81- return False
82-
83-
84- class IsAdmin (permissions .BasePermission ):
85- """
86- View permission to allow only admins.
87- """
88-
89- def has_permission (self , request , view ):
90- """
91- Return `True` if permission is granted, `False` otherwise.
92- """
93- return request .user .is_superuser
94-
95-
96- class IsAdminOrSafeMethod (permissions .BasePermission ):
97- """
98- View permission to allow only admins to use unsafe methods
99- including POST, PUT, DELETE.
100-
101- This allows
102- """
103-
104- def has_permission (self , request , view ):
105- """
106- Return `True` if permission is granted, `False` otherwise.
107- """
108- return request .method in permissions .SAFE_METHODS or request .user .is_superuser
109-
110-
111- class HasRegistrationAuth (permissions .BasePermission ):
112- """
113- Checks to see if registration is enabled
114- """
115- def has_permission (self , request , view ):
116- return settings .REGISTRATION_ENABLED
117-
118-
119- class HasBuilderAuth (permissions .BasePermission ):
120- """
121- View permission to allow builder to perform actions
122- with a special HTTP header
123- """
124-
125- def has_permission (self , request , view ):
126- """
127- Return `True` if permission is granted, `False` otherwise.
128- """
129- auth_header = request .environ .get ('HTTP_X_DEIS_BUILDER_AUTH' )
130- if not auth_header :
131- return False
132- return auth_header == settings .BUILDER_KEY
133-
134-
13540class UserRegistrationView (viewsets .GenericViewSet ,
13641 viewsets .mixins .CreateModelMixin ):
13742 model = User
0 commit comments