Filters not well formed account name given in register form
darcs-hash:20080901204203-86b55-80775d24770b57c736d5a8dc7d2e50bb279e958a.gz
This commit is contained in:
@@ -3,18 +3,18 @@
|
|||||||
## Login : David Rousselie <dax@happycoders.org>
|
## Login : David Rousselie <dax@happycoders.org>
|
||||||
## Started on Sun Nov 5 20:13:48 2006 David Rousselie
|
## Started on Sun Nov 5 20:13:48 2006 David Rousselie
|
||||||
## $Id$
|
## $Id$
|
||||||
##
|
##
|
||||||
## Copyright (C) 2006 David Rousselie
|
## Copyright (C) 2006 David Rousselie
|
||||||
## This program is free software; you can redistribute it and/or modify
|
## This program is free software; you can redistribute it and/or modify
|
||||||
## it under the terms of the GNU General Public License as published by
|
## it under the terms of the GNU General Public License as published by
|
||||||
## the Free Software Foundation; either version 2 of the License, or
|
## the Free Software Foundation; either version 2 of the License, or
|
||||||
## (at your option) any later version.
|
## (at your option) any later version.
|
||||||
##
|
##
|
||||||
## This program is distributed in the hope that it will be useful,
|
## This program is distributed in the hope that it will be useful,
|
||||||
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
## GNU General Public License for more details.
|
## GNU General Public License for more details.
|
||||||
##
|
##
|
||||||
## You should have received a copy of the GNU General Public License
|
## You should have received a copy of the GNU General Public License
|
||||||
## along with this program; if not, write to the Free Software
|
## along with this program; if not, write to the Free Software
|
||||||
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
@@ -24,13 +24,35 @@
|
|||||||
|
|
||||||
__revision__ = "$Id: error.py,v 1.1 2006/11/05 20:13:48 dax Exp $"
|
__revision__ = "$Id: error.py,v 1.1 2006/11/05 20:13:48 dax Exp $"
|
||||||
|
|
||||||
|
from jcl.lang import Lang
|
||||||
|
|
||||||
class FieldError(Exception):
|
class FieldError(Exception):
|
||||||
"""Error raised when error exists on Jabber Data Form fields"""
|
"""Error raised when error exists on Jabber Data Form fields"""
|
||||||
|
|
||||||
def __init__(self, field, error_msg):
|
def __init__(self, field, message="", message_property=None, lang_class=Lang.en):
|
||||||
Exception.__init__(self)
|
Exception.__init__(self)
|
||||||
self.field = field
|
self.field = field
|
||||||
self.error_msg = error_msg
|
self.lang_class = lang_class
|
||||||
|
self.message_property = message_property
|
||||||
|
self.message = message
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Error with " + str(self.field) + " field: " + str(self.error_msg)
|
if self.message_property is None \
|
||||||
|
or not hasattr(self.lang_class, self.message_property):
|
||||||
|
return self.lang_class.field_error % (str(self.field), self.message)
|
||||||
|
else:
|
||||||
|
return self.lang_class.field_error % \
|
||||||
|
(str(self.field),
|
||||||
|
str(getattr(self.lang_class, self.message_property)))
|
||||||
|
|
||||||
|
class MandatoryFieldError(FieldError):
|
||||||
|
"""Error raised when a mandatory field in a Form is not supplied"""
|
||||||
|
|
||||||
|
def __init__ (self, field):
|
||||||
|
FieldError.__init__(self, field, message_property="mandatory_field")
|
||||||
|
|
||||||
|
class NotWellFormedFieldError(FieldError):
|
||||||
|
"""Error raised when a supplied field in a Form is not well formed"""
|
||||||
|
|
||||||
|
def __init__ (self, field):
|
||||||
|
FieldError.__init__(self, field, message_property="not_well_formed_field")
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
##
|
##
|
||||||
## register.py
|
## register.py
|
||||||
## Login : David Rousselie <dax@happycoders.org>
|
## Login : David Rousselie <dax@happycoders.org>
|
||||||
## Started on Wed Jul 18 21:32:51 2007 David Rousselie
|
## Started on Wed Jul 18 21:32:51 2007 David Rousselie
|
||||||
## $Id$
|
## $Id$
|
||||||
##
|
##
|
||||||
## Copyright (C) 2007 David Rousselie
|
## Copyright (C) 2007 David Rousselie
|
||||||
## This program is free software; you can redistribute it and/or modify
|
## This program is free software; you can redistribute it and/or modify
|
||||||
## it under the terms of the GNU General Public License as published by
|
## it under the terms of the GNU General Public License as published by
|
||||||
## the Free Software Foundation; either version 2 of the License, or
|
## the Free Software Foundation; either version 2 of the License, or
|
||||||
## (at your option) any later version.
|
## (at your option) any later version.
|
||||||
##
|
##
|
||||||
## This program is distributed in the hope that it will be useful,
|
## This program is distributed in the hope that it will be useful,
|
||||||
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
## GNU General Public License for more details.
|
## GNU General Public License for more details.
|
||||||
##
|
##
|
||||||
## You should have received a copy of the GNU General Public License
|
## You should have received a copy of the GNU General Public License
|
||||||
## along with this program; if not, write to the Free Software
|
## along with this program; if not, write to the Free Software
|
||||||
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
@@ -23,10 +24,11 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
import re
|
||||||
|
|
||||||
import pyxmpp.error as error
|
import pyxmpp.error as error
|
||||||
|
|
||||||
from jcl.error import FieldError
|
from jcl.error import FieldError, MandatoryFieldError, NotWellFormedFieldError
|
||||||
import jcl.jabber as jabber
|
import jcl.jabber as jabber
|
||||||
|
|
||||||
class SetRegisterHandler(object):
|
class SetRegisterHandler(object):
|
||||||
@@ -36,6 +38,7 @@ class SetRegisterHandler(object):
|
|||||||
self.component = component
|
self.component = component
|
||||||
self.account_manager = component.account_manager
|
self.account_manager = component.account_manager
|
||||||
self.__logger = logging.getLogger("jcl.jabber.SetRegisterHandler")
|
self.__logger = logging.getLogger("jcl.jabber.SetRegisterHandler")
|
||||||
|
self.field_name_regexp = re.compile("^[^@]+$")
|
||||||
|
|
||||||
def filter(self, info_query, lang_class, x_data):
|
def filter(self, info_query, lang_class, x_data):
|
||||||
"""Filter requests to be handled"""
|
"""Filter requests to be handled"""
|
||||||
@@ -43,7 +46,7 @@ class SetRegisterHandler(object):
|
|||||||
|
|
||||||
def handle(self, info_query, lang_class, data, x_data):
|
def handle(self, info_query, lang_class, data, x_data):
|
||||||
"""Handle disco get items request"""
|
"""Handle disco get items request"""
|
||||||
return None
|
return self.validate_form(x_data, info_query, lang_class)
|
||||||
|
|
||||||
def handle_error(self, field_error, info_query, lang_class):
|
def handle_error(self, field_error, info_query, lang_class):
|
||||||
type, value, stack = sys.exc_info()
|
type, value, stack = sys.exc_info()
|
||||||
@@ -51,13 +54,26 @@ class SetRegisterHandler(object):
|
|||||||
(field_error, "".join(traceback.format_exception
|
(field_error, "".join(traceback.format_exception
|
||||||
(type, value, stack, 5))))
|
(type, value, stack, 5))))
|
||||||
iq_error = info_query.make_error_response("not-acceptable")
|
iq_error = info_query.make_error_response("not-acceptable")
|
||||||
|
field_error.lang_class = lang_class
|
||||||
text = iq_error.get_error().xmlnode.newTextChild(\
|
text = iq_error.get_error().xmlnode.newTextChild(\
|
||||||
None,
|
None,
|
||||||
"text",
|
"text",
|
||||||
lang_class.mandatory_field % (field_error.field))
|
str(field_error))
|
||||||
text.setNs(text.newNs(error.STANZA_ERROR_NS, None))
|
text.setNs(text.newNs(error.STANZA_ERROR_NS, None))
|
||||||
return [iq_error]
|
return [iq_error]
|
||||||
|
|
||||||
|
def validate_form(self, form, info_query, lang_class):
|
||||||
|
"""Test if given form is valid"""
|
||||||
|
if form is None or not "name" in form or form["name"].value == "":
|
||||||
|
return self.handle_error(\
|
||||||
|
MandatoryFieldError("name"),
|
||||||
|
info_query, lang_class)
|
||||||
|
if not self.field_name_regexp.match(form["name"].value):
|
||||||
|
return self.handle_error(\
|
||||||
|
NotWellFormedFieldError("name"),
|
||||||
|
info_query, lang_class)
|
||||||
|
return None
|
||||||
|
|
||||||
class RootSetRegisterHandler(SetRegisterHandler):
|
class RootSetRegisterHandler(SetRegisterHandler):
|
||||||
|
|
||||||
def __init__(self, component):
|
def __init__(self, component):
|
||||||
@@ -73,10 +89,11 @@ class RootSetRegisterHandler(SetRegisterHandler):
|
|||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
self.__logger.debug("root_set_register")
|
self.__logger.debug("root_set_register")
|
||||||
|
stanzas = SetRegisterHandler.handle(self, info_query, lang_class,
|
||||||
|
data, x_data)
|
||||||
|
if stanzas is not None:
|
||||||
|
return stanzas
|
||||||
_account = None
|
_account = None
|
||||||
if not "name" in x_data or x_data["name"].value == "":
|
|
||||||
return self.handle_error(FieldError("name", ""),
|
|
||||||
info_query, lang_class)
|
|
||||||
try:
|
try:
|
||||||
info_queries = self.account_manager.create_default_account(\
|
info_queries = self.account_manager.create_default_account(\
|
||||||
x_data["name"].value,
|
x_data["name"].value,
|
||||||
@@ -104,6 +121,10 @@ class AccountSetRegisterHandler(SetRegisterHandler):
|
|||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
self.__logger.debug("account_set_register")
|
self.__logger.debug("account_set_register")
|
||||||
|
stanzas = SetRegisterHandler.handle(self, info_query, lang_class,
|
||||||
|
data, x_data)
|
||||||
|
if stanzas is not None:
|
||||||
|
return stanzas
|
||||||
_account = None
|
_account = None
|
||||||
resource = info_query.get_to().resource
|
resource = info_query.get_to().resource
|
||||||
if resource is not None:
|
if resource is not None:
|
||||||
@@ -138,6 +159,10 @@ class AccountTypeSetRegisterHandler(SetRegisterHandler):
|
|||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
self.__logger.debug("account_type_set_register")
|
self.__logger.debug("account_type_set_register")
|
||||||
|
stanzas = SetRegisterHandler.handle(self, info_query, lang_class,
|
||||||
|
data, x_data)
|
||||||
|
if stanzas is not None:
|
||||||
|
return stanzas
|
||||||
account_type = data
|
account_type = data
|
||||||
_account = None
|
_account = None
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import unittest
|
|||||||
import jcl.jabber as jabber
|
import jcl.jabber as jabber
|
||||||
|
|
||||||
from jcl.jabber.tests import component, feeder, command, message, presence, \
|
from jcl.jabber.tests import component, feeder, command, message, presence, \
|
||||||
disco, vcard
|
disco, vcard, register
|
||||||
|
|
||||||
class HandlerType1:
|
class HandlerType1:
|
||||||
pass
|
pass
|
||||||
@@ -39,6 +39,7 @@ def suite():
|
|||||||
test_suite.addTest(presence.suite())
|
test_suite.addTest(presence.suite())
|
||||||
test_suite.addTest(disco.suite())
|
test_suite.addTest(disco.suite())
|
||||||
test_suite.addTest(vcard.suite())
|
test_suite.addTest(vcard.suite())
|
||||||
|
test_suite.addTest(register.suite())
|
||||||
return test_suite
|
return test_suite
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -1327,7 +1327,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
|
|||||||
self.assertEquals(stanza_error.get_condition().name,
|
self.assertEquals(stanza_error.get_condition().name,
|
||||||
"not-acceptable")
|
"not-acceptable")
|
||||||
self.assertEquals(stanza_error.get_text(),
|
self.assertEquals(stanza_error.get_text(),
|
||||||
Lang.en.mandatory_field % ("name"))
|
Lang.en.field_error % ("name", Lang.en.mandatory_field))
|
||||||
|
|
||||||
def test_handle_set_register_new_field_mandatory(self):
|
def test_handle_set_register_new_field_mandatory(self):
|
||||||
self.comp.stream = MockStream()
|
self.comp.stream = MockStream()
|
||||||
@@ -1358,7 +1358,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
|
|||||||
self.assertEquals(stanza_error.get_condition().name,
|
self.assertEquals(stanza_error.get_condition().name,
|
||||||
"not-acceptable")
|
"not-acceptable")
|
||||||
self.assertEquals(stanza_error.get_text(),
|
self.assertEquals(stanza_error.get_text(),
|
||||||
Lang.en.mandatory_field % ("login"))
|
Lang.en.field_error % ("login", Lang.en.mandatory_field))
|
||||||
|
|
||||||
def test_handle_set_register_update_not_existing(self):
|
def test_handle_set_register_update_not_existing(self):
|
||||||
self.comp.stream = MockStream()
|
self.comp.stream = MockStream()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
##
|
##
|
||||||
## disco.py
|
## disco.py
|
||||||
## Login : David Rousselie <dax@happycoders.org>
|
## Login : David Rousselie <dax@happycoders.org>
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ class Lang:
|
|||||||
update_account_message_subject = u"Updated account '%s'"
|
update_account_message_subject = u"Updated account '%s'"
|
||||||
update_account_message_body = u"Updated account"
|
update_account_message_body = u"Updated account"
|
||||||
|
|
||||||
mandatory_field = u"%s is required"
|
field_error = u"Error with '%s' field: %s"
|
||||||
|
mandatory_field = u"required field"
|
||||||
|
not_well_formed_field = u"not well formed field"
|
||||||
|
|
||||||
field_chat_action = u"Action when state is 'Free For Chat'"
|
field_chat_action = u"Action when state is 'Free For Chat'"
|
||||||
field_online_action = u"Action when state is 'Online'"
|
field_online_action = u"Action when state is 'Online'"
|
||||||
@@ -265,6 +267,7 @@ class Lang:
|
|||||||
update_account_message_body = u"Compte mis à jour"
|
update_account_message_body = u"Compte mis à jour"
|
||||||
|
|
||||||
mandatory_field = u"%s est requis"
|
mandatory_field = u"%s est requis"
|
||||||
|
not_well_formed_field = u"Le champs %s n'est pas acceptable"
|
||||||
|
|
||||||
field_chat_action = u"Action lorsque l'état est 'Free For Chat'"
|
field_chat_action = u"Action lorsque l'état est 'Free For Chat'"
|
||||||
field_online_action = u"Action lorsque l'état est 'Online'"
|
field_online_action = u"Action lorsque l'état est 'Online'"
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ from sqlobject.joins import MultipleJoin
|
|||||||
from sqlobject.sqlbuilder import AND
|
from sqlobject.sqlbuilder import AND
|
||||||
|
|
||||||
from jcl.lang import Lang
|
from jcl.lang import Lang
|
||||||
from jcl.error import FieldError
|
from jcl.error import MandatoryFieldError
|
||||||
import jcl.model as model
|
import jcl.model as model
|
||||||
|
|
||||||
OFFLINE = "offline"
|
OFFLINE = "offline"
|
||||||
@@ -58,7 +58,7 @@ def mandatory_field(field_name, field_value):
|
|||||||
"""Used as default function for field that must be specified
|
"""Used as default function for field that must be specified
|
||||||
and cannot have an empty value"""
|
and cannot have an empty value"""
|
||||||
if field_value is None or str(field_value) == "":
|
if field_value is None or str(field_value) == "":
|
||||||
raise FieldError(field_name, "Field required")
|
raise MandatoryFieldError(field_name)
|
||||||
return field_value
|
return field_value
|
||||||
|
|
||||||
class User(InheritableSQLObject):
|
class User(InheritableSQLObject):
|
||||||
|
|||||||
@@ -91,7 +91,9 @@ class Language_TestCase(unittest.TestCase):
|
|||||||
self.assertNotEquals(self.lang_class.update_account_message_subject % (""), None)
|
self.assertNotEquals(self.lang_class.update_account_message_subject % (""), None)
|
||||||
self.assertNotEquals(self.lang_class.update_account_message_body, None)
|
self.assertNotEquals(self.lang_class.update_account_message_body, None)
|
||||||
|
|
||||||
self.assertNotEquals(self.lang_class.mandatory_field % (""), None)
|
self.assertNotEquals(self.lang_class.field_error % ("", ""), None)
|
||||||
|
self.assertNotEquals(self.lang_class.mandatory_field, None)
|
||||||
|
self.assertNotEquals(self.lang_class.not_well_formed_field, None)
|
||||||
|
|
||||||
self.assertNotEquals(self.lang_class.field_chat_action, None)
|
self.assertNotEquals(self.lang_class.field_chat_action, None)
|
||||||
self.assertNotEquals(self.lang_class.field_online_action, None)
|
self.assertNotEquals(self.lang_class.field_online_action, None)
|
||||||
|
|||||||
Reference in New Issue
Block a user