Skip to content

Commit

Permalink
Restrict user registration
Browse files Browse the repository at this point in the history
For certain deployments, users should not be able to register on their own,
but should be added by the appropriate gym managers. This can now be configured
in the global settings file

Fixes wger-project#220
  • Loading branch information
rolandgeider committed Nov 21, 2015
1 parent ffa5ac1 commit 1fa2888
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
4 changes: 4 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
44 changes: 30 additions & 14 deletions wger/core/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def test_registration_captcha(self):
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)

Expand Down Expand Up @@ -84,16 +88,28 @@ def test_register(self):
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)
5 changes: 5 additions & 0 deletions wger/core/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
3 changes: 2 additions & 1 deletion wger/settings_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,6 @@
#
WGER_SETTINGS = {
'USE_RECAPTCHA': False,
'REMOVE_WHITESPACE': False
'REMOVE_WHITESPACE': False,
'ALLOW_REGISTRATION': True
}
4 changes: 4 additions & 0 deletions wger/software/templates/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ <h4>2015-XX-XX</h4>
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
Expand Down
2 changes: 2 additions & 0 deletions wger/software/templates/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ <h2>{% trans "Your progress." %}</h2>
{% 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>
Expand Down
6 changes: 4 additions & 2 deletions wger/software/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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$',
Expand All @@ -27,7 +29,7 @@
name='tos'),

url(r'^features$',
TemplateView.as_view(template_name="functions.html"),
views.features,
name='features'),

url(r'^changelog$',
Expand Down
32 changes: 32 additions & 0 deletions wger/software/views.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1fa2888

Please sign in to comment.