Skip to content

Commit

Permalink
Adding the ability to configure password strength in the local_settin…
Browse files Browse the repository at this point in the history
…gs. Fixes bug 948317

Change-Id: I96e3838ab6675e7282172e56be3f0359065caccb
  • Loading branch information
John Postlethwait committed Mar 11, 2012
1 parent e165416 commit 24f6bc5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -23,6 +23,7 @@ Jay Pipes <jaypipes@gmail.com>
Jeffrey Wilcox <jeffjapan@gmail.com>
Jesse Andrews <anotherjesse@gmail.com>
Jim Yeh <lemonlatte@gmail.com>
John Postlethwait <john.postlethwait@nebula.com>
Joseph Heck <heckj@mac.com>
Joshua McKenty <joshua@pistoncloud.com>
Julien Danjou <julien.danjou@enovance.com>
Expand Down
9 changes: 7 additions & 2 deletions horizon/dashboards/syspanel/users/forms.py
Expand Up @@ -28,6 +28,7 @@
from horizon import api
from horizon import exceptions
from horizon import forms
from horizon.utils import validators


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,10 +62,14 @@ def clean(self):
class CreateUserForm(BaseUserForm):
name = forms.CharField(label=_("Name"))
email = forms.EmailField(label=_("Email"))
password = forms.CharField(label=_("Password"),
widget=forms.PasswordInput(render_value=False))
password = forms.RegexField(
label=_("Password"),
widget=forms.PasswordInput(render_value=False),
regex=validators.password_validator(),
error_messages={'invalid': validators.password_validator_msg()})
confirm_password = forms.CharField(
label=_("Confirm Password"),
required=False,
widget=forms.PasswordInput(render_value=False))
tenant_id = forms.ChoiceField(label=_("Primary Project"))

Expand Down
23 changes: 19 additions & 4 deletions horizon/dashboards/syspanel/users/tests.py
Expand Up @@ -82,10 +82,25 @@ def test_create_user_password_mismatch(self):
'confirm_password': "doesntmatch"}

res = self.client.post(USER_CREATE_URL, formData)
self.assertFormError(res,
"form",
None,
['Passwords do not match.'])
self.assertFormError(res, "form", None, ['Passwords do not match.'])

def test_user_password_validation(self):
user = self.users.get(id="1")
self.mox.StubOutWithMock(api, 'tenant_list')
api.tenant_list(IgnoreArg(), admin=True).AndReturn(self.tenants.list())
self.mox.ReplayAll()

formData = {'method': 'CreateUserForm',
'name': user.name,
'email': user.email,
'password': 'four',
'tenant_id': self.tenant.id,
'confirm_password': "four"}

res = self.client.post(USER_CREATE_URL, formData)
self.assertFormError(
res, "form", 'password',
['Your password must be at least 6 characters long.'])

def test_enable_user(self):
user = self.users.get(id="2")
Expand Down
4 changes: 4 additions & 0 deletions horizon/tests/testsettings.py
Expand Up @@ -89,6 +89,10 @@
HORIZON_CONFIG = {
'dashboards': ('nova', 'syspanel', 'settings',),
'default_dashboard': 'nova',
"password_validator": {
"regex": '.{6,}',
"help_text": "Your password must be at least 6 characters long."
},
}

AVAILABLE_REGIONS = [
Expand Down
14 changes: 14 additions & 0 deletions horizon/utils/validators.py
Expand Up @@ -16,6 +16,8 @@

import re

from django.conf import settings

from django.core import validators
from django.core.exceptions import ValidationError

Expand All @@ -31,3 +33,15 @@
def validate_port_range(port):
if port not in range(-1, 65535):
raise ValidationError("Not a valid port number")


def password_validator():
config = getattr(settings, "HORIZON_CONFIG", {})
password_config = config.get("password_validator", {})
return password_config.get("regex", ".*")


def password_validator_msg():
config = getattr(settings, "HORIZON_CONFIG", {})
password_config = config.get("password_validator", {})
return password_config.get("help_text", None)
10 changes: 10 additions & 0 deletions openstack_dashboard/local/local_settings.py.example
@@ -1,5 +1,7 @@
import os

from django.utils.translation import ugettext_lazy as _

DEBUG = True
TEMPLATE_DEBUG = DEBUG
PROD = False
Expand All @@ -8,6 +10,14 @@ USE_SSL = False
# Note: You should change this value
SECRET_KEY = 'elj1IWiLoWHgcyYxFVLj7cM5rGOOxWl0'

# Specify a regular expression to validate user passwords.
# HORIZON_CONFIG = {
# "password_validator": {
# "regex": '.*',
# "help_text": _("Your password does not meet the requirements.")
# }
# }

LOCAL_PATH = os.path.dirname(os.path.abspath(__file__))

# We recommend you use memcached for development; otherwise after every reload
Expand Down

0 comments on commit 24f6bc5

Please sign in to comment.