Browse Source

HUE-675. [useradmin] Disallow creating group names with spaces

Romain Rigaux 13 years ago
parent
commit
f432b42580
2 changed files with 8 additions and 1 deletions
  1. 5 0
      apps/useradmin/src/useradmin/tests.py
  2. 3 1
      apps/useradmin/src/useradmin/views.py

+ 5 - 0
apps/useradmin/src/useradmin/tests.py

@@ -228,6 +228,11 @@ def test_group_admin():
   response = c.post('/useradmin/groups/delete/testgroup')
   response = c.post('/useradmin/groups/delete/testgroup')
   assert_true(len(Group.objects.all()) == 1)
   assert_true(len(Group.objects.all()) == 1)
 
 
+  group_count = len(Group.objects.all())
+  response = c.post('/useradmin/groups/new', dict(name="with space"))
+  assert_true("Group name may only contain letters" in response.content)
+  assert_equal(len(Group.objects.all()), group_count)
+
 
 
 def test_user_admin():
 def test_user_admin():
   FUNNY_NAME = '~`!@#$%^&*()_-+={}[]|\;"<>?/,.'
   FUNNY_NAME = '~`!@#$%^&*()_-+={}[]|\;"<>?/,.'

+ 3 - 1
apps/useradmin/src/useradmin/views.py

@@ -463,6 +463,8 @@ class GroupEditForm(forms.ModelForm):
   """
   """
   Form to manipulate a group.  This manages the group name and its membership.
   Form to manipulate a group.  This manages the group name and its membership.
   """
   """
+  GROUPNAME = re.compile('^%s$' % get_groupname_re_rule())
+
   class Meta:
   class Meta:
     model = Group
     model = Group
     fields = ("name",)
     fields = ("name",)
@@ -470,7 +472,7 @@ class GroupEditForm(forms.ModelForm):
   def clean_name(self):
   def clean_name(self):
     # Note that the superclass doesn't have a clean_name method.
     # Note that the superclass doesn't have a clean_name method.
     data = self.cleaned_data["name"]
     data = self.cleaned_data["name"]
-    if not re.match(get_groupname_re_rule(), data):
+    if not self.GROUPNAME.match(data):
       raise forms.ValidationError("Group name may only contain letters, " +
       raise forms.ValidationError("Group name may only contain letters, " +
                                   "numbers, hypens or underscores.")
                                   "numbers, hypens or underscores.")
     return data
     return data