Skip to content

Commit deb6a14

Browse files
author
Matthew Fisher
committed
dynamically disable registration
fixes #547
1 parent ae097a0 commit deb6a14

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

controller/api/tests/test_auth.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from django.conf import settings
1313
from django.test import TestCase
14+
from django.test.utils import override_settings
1415

1516

1617
class AuthTest(TestCase):
@@ -85,6 +86,22 @@ def test_auth(self):
8586
response = self.client.get(url)
8687
self.assertEqual(response.status_code, 200)
8788

89+
@override_settings(REGISTRATION_ENABLED=False)
90+
def test_auth_registration_disabled(self):
91+
"""test that a new user cannot register when registration is disabled."""
92+
url = '/api/auth/register'
93+
submit = {
94+
'username': 'testuser',
95+
'password': 'password',
96+
'first_name': 'test',
97+
'last_name': 'user',
98+
'email': 'test@user.com',
99+
'is_superuser': False,
100+
'is_staff': False,
101+
}
102+
response = self.client.post(url, json.dumps(submit), content_type='application/json')
103+
self.assertEqual(response.status_code, 403)
104+
88105
def test_cancel(self):
89106
"""Test that a registered user can cancel her account."""
90107
# test registration workflow

controller/api/views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def has_permission(self, request, view):
111111
return request.method in permissions.SAFE_METHODS or request.user.is_superuser
112112

113113

114+
class HasRegistrationAuth(permissions.BasePermission):
115+
"""
116+
Checks to see if registration is enabled
117+
"""
118+
def has_permission(self, request, view):
119+
return settings.REGISTRATION_ENABLED
120+
121+
114122
class HasBuilderAuth(permissions.BasePermission):
115123
"""
116124
View permission to allow builder to perform actions
@@ -132,7 +140,7 @@ class UserRegistrationView(viewsets.GenericViewSet,
132140
model = User
133141

134142
authentication_classes = (AnonymousAuthentication,)
135-
permission_classes = (IsAnonymous,)
143+
permission_classes = (IsAnonymous, HasRegistrationAuth)
136144
serializer_class = serializers.UserSerializer
137145

138146
def post_save(self, user, created=False):

controller/deis/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@
296296
# default providers, typically overriden in local_settings to include ec2, etc.
297297
PROVIDER_MODULES = ('mock',)
298298

299+
# check if we can register users with `deis register`
300+
REGISTRATION_ENABLED = True
301+
299302
# default to sqlite3, but allow postgresql config through envvars
300303
DATABASES = {
301304
'default': {

0 commit comments

Comments
 (0)