From 2567483281c6390164fd2d18f6e3409d3bfd0fc0 Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Sat, 15 Aug 2009 10:21:26 +0000 Subject: [PATCH] CaptchaValidator for captcha/recaptcha/no captcha added. This is necessary because the zcml registration of the CaptchaValidator has to work with all three options. svn path=/plone.app.discussion/trunk/; revision=28894 --- plone/app/discussion/browser/captcha.py | 8 ++-- plone/app/discussion/browser/comments.py | 2 +- plone/app/discussion/browser/validator.py | 49 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 plone/app/discussion/browser/validator.py diff --git a/plone/app/discussion/browser/captcha.py b/plone/app/discussion/browser/captcha.py index 50a0eb7..666dd2e 100644 --- a/plone/app/discussion/browser/captcha.py +++ b/plone/app/discussion/browser/captcha.py @@ -21,10 +21,11 @@ from plone.app.discussion.browser.comments import CommentForm from plone.app.discussion.comment import Comment from plone.app.discussion.interfaces import IDiscussionSettings, ICaptcha +from plone.app.discussion.browser.validator import CaptchaValidator + HAS_CAPTCHA = False try: from plone.formwidget.captcha import CaptchaFieldWidget - from plone.formwidget.captcha.validator import CaptchaValidator HAS_CAPTCHA = True except ImportError: pass @@ -44,8 +45,7 @@ class Captcha(Persistent): Captcha = factory(Captcha) if HAS_CAPTCHA or HAS_RECAPTCHA: - - # Extend the comment form with captcha, only if a captcha solution is installed. + # Extend the comment form with captcha, if a captcha solution is installed. class CaptchaExtender(extensible.FormExtender): adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form @@ -73,7 +73,7 @@ if HAS_CAPTCHA or HAS_RECAPTCHA: # use the ReCaptchaFieldWidget self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget - # Register Captcha validator for the captcha field in the ICaptchaForm + # Register Captcha validator for the Captcha field in the ICaptcha Form validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptcha['captcha']) else: diff --git a/plone/app/discussion/browser/comments.py b/plone/app/discussion/browser/comments.py index 6a47725..ea2de18 100644 --- a/plone/app/discussion/browser/comments.py +++ b/plone/app/discussion/browser/comments.py @@ -34,7 +34,7 @@ from plone.app.layout.viewlets.common import ViewletBase from plone.app.discussion.comment import Comment, CommentFactory from plone.app.discussion.interfaces import IConversation, IComment, IReplies, IDiscussionSettings, ICaptcha -from plone.formwidget.captcha.validator import CaptchaValidator +from plone.app.discussion.browser.validator import CaptchaValidator from plone.z3cform import layout, z2 from plone.z3cform.fieldsets import extensible diff --git a/plone/app/discussion/browser/validator.py b/plone/app/discussion/browser/validator.py new file mode 100644 index 0000000..60448e7 --- /dev/null +++ b/plone/app/discussion/browser/validator.py @@ -0,0 +1,49 @@ +# Captcha/ReCaptcha validators. We override the standard validators from +# plone.formwidget.captcha and plone.formwidget.recaptcha, in order to +# switch between the two. This is necessary, because the zcml registration +# of the CaptchaValidator has to be there, no matter which captcha solution +# is installed, or even when no captcha solution is installed. + +from Acquisition import aq_inner + +from z3c.form import validator + +from z3c.form.interfaces import IValidator + +from zope.component import getMultiAdapter, provideAdapter, queryUtility + +from zope.schema import ValidationError + +from plone.registry.interfaces import IRegistry + +from plone.app.discussion.interfaces import IDiscussionSettings + +from plone.formwidget.captcha import CaptchaMessageFactory as _ + +class WrongCaptchaCode(ValidationError): + __doc__ = _("""The code you entered was wrong, please enter the new one.""") + +class CaptchaValidator(validator.SimpleFieldValidator): + + def validate(self, value): + super(CaptchaValidator, self).validate(value) + + registry = queryUtility(IRegistry) + settings = registry.forInterface(IDiscussionSettings) + + if settings.captcha == 'captcha': + # Fetch captcha view + captcha = getMultiAdapter((aq_inner(self.context), self.request), name='captcha') + if value: + if not captcha.verify(value): + raise WrongCaptchaCode + else: + return True + raise WrongCaptchaCode + elif settings.captcha == 'recaptcha': + # Fetch recatpcha view + captcha = getMultiAdapter((aq_inner(self.context), self.request), name='recaptcha') + if not captcha.verify(): + raise WrongCaptchaCode + else: + return True \ No newline at end of file