Changeset - c9fa3f53143b
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 15 years ago 2010-11-23 14:58:29
marcin@python-works.com
added validation of username alphanumeric+dash only
2 files changed with 12 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rhodecode/model/forms.py
Show inline comments
 
@@ -14,85 +14,95 @@ ignore_key_missing      False     If Tru
 
  
 
  
 
<name> = formencode.validators.<name of validator>
 
<name> must equal form name
 
list=[1,2,3,4,5]
 
for SELECT use formencode.All(OneOf(list), Int())
 
    
 
"""
 
from formencode import All
 
from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \
 
    Email, Bool, StringBoolean
 
from pylons import session
 
from pylons.i18n.translation import _
 
from rhodecode.lib.auth import authfunc, get_crypt_password
 
from rhodecode.lib.exceptions import LdapImportError
 
from rhodecode.model import meta
 
from rhodecode.model.user import UserModel
 
from rhodecode.model.repo import RepoModel
 
from rhodecode.model.db import User
 
from webhelpers.pylonslib.secure_form import authentication_token
 
from rhodecode import BACKENDS
 
import formencode
 
import logging
 
import os
 
import re
 
import rhodecode.lib.helpers as h
 

	
 
log = logging.getLogger(__name__)
 

	
 
#this is needed to translate the messages using _() in validators
 
class State_obj(object):
 
    _ = staticmethod(_)
 

	
 
#===============================================================================
 
# VALIDATORS
 
#===============================================================================
 
class ValidAuthToken(formencode.validators.FancyValidator):
 
    messages = {'invalid_token':_('Token mismatch')}
 

	
 
    def validate_python(self, value, state):
 

	
 
        if value != authentication_token():
 
            raise formencode.Invalid(self.message('invalid_token', state,
 
                                            search_number=value), value, state)
 

	
 
def ValidUsername(edit, old_data):
 
    class _ValidUsername(formencode.validators.FancyValidator):
 

	
 
        def validate_python(self, value, state):
 
            if value in ['default', 'new_user']:
 
                raise formencode.Invalid(_('Invalid username'), value, state)
 
            #check if user is unique
 
            old_un = None
 
            if edit:
 
                old_un = UserModel().get(old_data.get('user_id')).username
 

	
 
            if old_un != value or not edit:
 
                if UserModel().get_by_username(value, cache=False,
 
                                               case_insensitive=True):
 
                    raise formencode.Invalid(_('This username already exists') ,
 
                                             value, state)
 

	
 
            
 
            
 
            if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-]+$', value) is None:
 
                raise formencode.Invalid(_('Username may only contain '
 
                                      'alphanumeric characters '
 
                                      'or dashes and cannot begin with a dash'),
 
                                      value, state)
 
            
 
            
 
            
 
    return _ValidUsername
 

	
 
class ValidPassword(formencode.validators.FancyValidator):
 

	
 
    def to_python(self, value, state):
 

	
 
        if value:
 

	
 
            if value.get('password'):
 
                try:
 
                    value['password'] = get_crypt_password(value['password'])
 
                except UnicodeEncodeError:
 
                    e_dict = {'password':_('Invalid characters in password')}
 
                    raise formencode.Invalid('', value, state, error_dict=e_dict)
 

	
 
            if value.get('password_confirmation'):
 
                try:
 
                    value['password_confirmation'] = \
 
                        get_crypt_password(value['password_confirmation'])
 
                except UnicodeEncodeError:
 
                    e_dict = {'password_confirmation':_('Invalid characters in password')}
 
                    raise formencode.Invalid('', value, state, error_dict=e_dict)
 

	
 
            if value.get('new_password'):
rhodecode/public/css/style.css
Show inline comments
 
@@ -2282,48 +2282,49 @@ display:block;
 
float:left;
 
margin:0 0 0 3px;
 
padding:0;
 
}
 
 
#login div.title h5,#register div.title h5 {
 
color:#fff;
 
margin:10px;
 
padding:0;
 
}
 
 
#login div.form div.fields div.field,#register div.form div.fields div.field {
 
clear:both;
 
overflow:hidden;
 
margin:0;
 
padding:0 0 10px;
 
}
 
 
#login div.form div.fields div.field span.error-message,#register div.form div.fields div.field span.error-message {
 
height:1%;
 
display:block;
 
color:red;
 
margin:8px 0 0;
 
padding:0;
 
width: 320px;
 
}
 
 
#login div.form div.fields div.field div.label label,#register div.form div.fields div.field div.label label {
 
color:#000;
 
font-weight:700;
 
}
 
 
#login div.form div.fields div.field div.input,#register div.form div.fields div.field div.input {
 
float:left;
 
margin:0;
 
padding:0;
 
}
 
 
#login div.form div.fields div.field div.checkbox,#register div.form div.fields div.field div.checkbox {
 
margin:0 0 0 184px;
 
padding:0;
 
}
 
 
#login div.form div.fields div.field div.checkbox label,#register div.form div.fields div.field div.checkbox label {
 
color:#565656;
 
font-weight:700;
 
}
 
 
#login div.form div.fields div.buttons input,#register div.form div.fields div.buttons input {
0 comments (0 inline, 0 general)