Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Restrict user registration

Roland Geider1fa288876ccf
diff --git a/docs/settings.rst b/docs/settings.rst
index d2494477b..90dd78bfa 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -6,6 +6,10 @@ Settings
You can configure some of the application behaviour with the ``WGER_SETTINGS``
dictionary in your settings file. Currently the following options are supported:
+**ALLOW_REGISTRATION**: Default ``True``.
+ Controls whether users can register on their own or if a gym adminstrator has
+ to create the user accounts.
+
**USE_RECAPTCHA**: Default ``False``.
Controls whether a captcha challenge will be presented when new users register.
diff --git a/wger/core/tests/test_registration.py b/wger/core/tests/test_registration.py
index 97164badc..62437561a 100644
--- a/wger/core/tests/test_registration.py
+++ b/wger/core/tests/test_registration.py
@@ -34,11 +34,15 @@ class RegistrationTestCase(WorkoutManagerTestCase):
Tests that the correct form is used depending on global
configuration settings
'''
- with self.settings(WGER_SETTINGS={'USE_RECAPTCHA': True, 'REMOVE_WHITESPACE': False}):
+ with self.settings(WGER_SETTINGS={'USE_RECAPTCHA': True,
+ 'REMOVE_WHITESPACE': False,
+ 'ALLOW_REGISTRATION': True}):
response = self.client.get(reverse('core:user:registration'))
self.assertIsInstance(response.context['form'], RegistrationForm)
- with self.settings(WGER_SETTINGS={'USE_RECAPTCHA': False, 'REMOVE_WHITESPACE': False}):
+ with self.settings(WGER_SETTINGS={'USE_RECAPTCHA': False,
+ 'REMOVE_WHITESPACE': False,
+ 'ALLOW_REGISTRATION': True}):
response = self.client.get(reverse('core:user:registration'))
self.assertIsInstance(response.context['form'], RegistrationFormNoCaptcha)
@@ -84,16 +88,28 @@ class RegistrationTestCase(WorkoutManagerTestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(count_before + 1, count_after)
- # No email
- registration_data['email'] = ''
- response = self.client.post(reverse('core:user:registration'), registration_data)
- count_after = User.objects.count()
- self.assertEqual(response.status_code, 302)
- self.assertEqual(count_before + 2, count_after)
+ def test_registration_deactivated(self):
+ '''
+ Test that with deactivated registration no users can register
+ '''
- # Already logged in
- response = self.client.post(reverse('core:user:registration'), registration_data)
- count_after = User.objects.count()
- self.assertEqual(response.status_code, 302)
- self.assertEqual(count_before + 2, count_after)
- self.assertIn('dashboard', response['Location'])
+ with self.settings(WGER_SETTINGS={'USE_RECAPTCHA': False,
+ 'REMOVE_WHITESPACE': False,
+ 'ALLOW_REGISTRATION': False}):
+
+ # Fetch the registration page
+ response = self.client.get(reverse('core:user:registration'))
+ self.assertEqual(response.status_code, 302)
+
+ # Fill in the registration form
+ registration_data = {'username': 'myusername',
+ 'password1': 'secret',
+ 'password2': 'secret',
+ 'email': '[email protected]',
+ 'g-recaptcha-response': 'PASSED', }
+ count_before = User.objects.count()
+
+ response = self.client.post(reverse('core:user:registration'), registration_data)
+ count_after = User.objects.count()
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(count_before, count_after)
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index 610736d0e..e26d9b6ea 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -196,6 +196,11 @@ def registration(request):
'''
A form to allow for registration of new users
'''
+
+ # If global user registration is deactivated, redirect
+ if not settings.WGER_SETTINGS['ALLOW_REGISTRATION']:
+ return HttpResponseRedirect(reverse('software:features'))
+
template_data = {}
template_data.update(csrf(request))
diff --git a/wger/settings_global.py b/wger/settings_global.py
index 640cdadb2..4b1f6ecce 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -347,5 +347,6 @@ IGNORABLE_404_URLS = (
#
WGER_SETTINGS = {
'USE_RECAPTCHA': False,
- 'REMOVE_WHITESPACE': False
+ 'REMOVE_WHITESPACE': False,
+ 'ALLOW_REGISTRATION': True
}
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index 81029ae0e..fc173e01c 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -35,6 +35,10 @@
Global list of users for installation
<a href="https://github.com/rolandgeider/wger/issues/212">#212</a>
</li>
+ <li>
+ Allow administrators to restrict user registration
+ <a href="https://github.com/rolandgeider/wger/issues/220">#220</a>
+ </li>
<li>Visual consistency for lists and actions</li>
<li>
Refactored and improved code, among others
diff --git a/wger/software/templates/functions.html b/wger/software/templates/functions.html
index a86cdca99..548a5f8a0 100644
--- a/wger/software/templates/functions.html
+++ b/wger/software/templates/functions.html
@@ -42,9 +42,11 @@
{% endif %}
{% if not user.is_authenticated or user.userprofile.is_temporary %}
+ {% if allow_registration %}
<p>
<a href="{% url 'core:user:registration' %}" class="btn btn-default btn-lg btn-block">{% trans "Register" %}</a>
</p>
+ {% endif %}
<p>
<a href="{% url 'core:user:login' %}" class="btn btn-default btn-lg btn-block">{% trans "Login" %}</a>
diff --git a/wger/software/urls.py b/wger/software/urls.py
index 7c6cc7034..2e7d36ece 100644
--- a/wger/software/urls.py
+++ b/wger/software/urls.py
@@ -14,9 +14,11 @@
#
# You should have received a copy of the GNU Affero General Public License
-from django.conf.urls import patterns, url
+from django.conf.urls import url
from django.views.generic import TemplateView
+from wger.software import views
+
urlpatterns = [
url(r'^issues$',
@@ -27,7 +29,7 @@ urlpatterns = [
name='tos'),
url(r'^features$',
- TemplateView.as_view(template_name="functions.html"),
+ views.features,
name='features'),
url(r'^changelog$',
diff --git a/wger/software/views.py b/wger/software/views.py
new file mode 100644
index 000000000..bf28fb060
--- /dev/null
+++ b/wger/software/views.py
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import logging
+
+from django.conf import settings
+
+from django.shortcuts import render
+
+logger = logging.getLogger(__name__)
+
+
+def features(request):
+ '''
+ Render the features page
+ '''
+
+ context = {'allow_registration': settings.WGER_SETTINGS['ALLOW_REGISTRATION']}
+ return render(request, 'functions.html', context)