Skip to content

Commit d5e8926

Browse files
author
Matthew Fisher
committed
ref(controller): move permission classes
1 parent 42996b1 commit d5e8926

2 files changed

Lines changed: 100 additions & 96 deletions

File tree

controller/api/permissions.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from rest_framework import permissions
2+
from django.conf import settings
3+
from django.contrib.auth.models import AnonymousUser, User
4+
5+
from api import models
6+
7+
8+
class IsAnonymous(permissions.BasePermission):
9+
"""
10+
View permission to allow anonymous users.
11+
"""
12+
13+
def has_permission(self, request, view):
14+
"""
15+
Return `True` if permission is granted, `False` otherwise.
16+
"""
17+
return type(request.user) is AnonymousUser
18+
19+
20+
class IsOwner(permissions.BasePermission):
21+
"""
22+
Object-level permission to allow only owners of an object to access it.
23+
Assumes the model instance has an `owner` attribute.
24+
"""
25+
26+
def has_object_permission(self, request, view, obj):
27+
if hasattr(obj, 'owner'):
28+
return obj.owner == request.user
29+
else:
30+
return False
31+
32+
33+
class IsAppUser(permissions.BasePermission):
34+
"""
35+
Object-level permission to allow owners or collaborators to access
36+
an app-related model.
37+
"""
38+
def has_object_permission(self, request, view, obj):
39+
if isinstance(obj, models.App) and obj.owner == request.user:
40+
return True
41+
elif hasattr(obj, 'app') and obj.app.owner == request.user:
42+
return True
43+
elif request.user.has_perm('use_app', obj):
44+
return request.method != 'DELETE'
45+
elif hasattr(obj, 'app') and request.user.has_perm('use_app', obj.app):
46+
return request.method != 'DELETE'
47+
else:
48+
return False
49+
50+
51+
class IsAdmin(permissions.BasePermission):
52+
"""
53+
View permission to allow only admins.
54+
"""
55+
56+
def has_permission(self, request, view):
57+
"""
58+
Return `True` if permission is granted, `False` otherwise.
59+
"""
60+
return request.user.is_superuser
61+
62+
63+
class IsAdminOrSafeMethod(permissions.BasePermission):
64+
"""
65+
View permission to allow only admins to use unsafe methods
66+
including POST, PUT, DELETE.
67+
68+
This allows
69+
"""
70+
71+
def has_permission(self, request, view):
72+
"""
73+
Return `True` if permission is granted, `False` otherwise.
74+
"""
75+
return request.method in permissions.SAFE_METHODS or request.user.is_superuser
76+
77+
78+
class HasRegistrationAuth(permissions.BasePermission):
79+
"""
80+
Checks to see if registration is enabled
81+
"""
82+
def has_permission(self, request, view):
83+
return settings.REGISTRATION_ENABLED
84+
85+
86+
class HasBuilderAuth(permissions.BasePermission):
87+
"""
88+
View permission to allow builder to perform actions
89+
with a special HTTP header
90+
"""
91+
92+
def has_permission(self, request, view):
93+
"""
94+
Return `True` if permission is granted, `False` otherwise.
95+
"""
96+
auth_header = request.environ.get('HTTP_X_DEIS_BUILDER_AUTH')
97+
if not auth_header:
98+
return False
99+
return auth_header == settings.BUILDER_KEY

controller/api/views.py

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
from rest_framework.response import Response
2525

2626
from api import models, serializers
27-
28-
from django.conf import settings
27+
from api.permissions import *
2928

3029

3130
class 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-
13540
class UserRegistrationView(viewsets.GenericViewSet,
13641
viewsets.mixins.CreateModelMixin):
13742
model = User

0 commit comments

Comments
 (0)