2010-06-01 18:38:29 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-08-31 19:36:28 +02:00
|
|
|
"""Captcha validator, see captcha.txt for design notes.
|
|
|
|
"""
|
2009-08-15 12:21:26 +02:00
|
|
|
from Acquisition import aq_inner
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.discussion.interfaces import ICaptcha
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionLayer
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
|
|
|
from plone.registry.interfaces import IRegistry
|
2009-08-15 12:21:26 +02:00
|
|
|
from z3c.form import validator
|
|
|
|
from z3c.form.interfaces import IValidator
|
2017-07-28 17:58:35 +02:00
|
|
|
from zope.component import adapter
|
2015-05-03 08:16:39 +02:00
|
|
|
from zope.component import getMultiAdapter
|
|
|
|
from zope.component import queryUtility
|
2016-07-05 23:12:08 +02:00
|
|
|
from zope.interface import implementer
|
2015-05-03 08:16:39 +02:00
|
|
|
from zope.interface import Interface
|
|
|
|
from zope.schema.interfaces import IField
|
2009-08-28 10:28:58 +02:00
|
|
|
|
2010-06-01 18:38:29 +02:00
|
|
|
|
2010-10-09 13:13:28 +02:00
|
|
|
try:
|
|
|
|
from collective.z3cform.norobots.validator import WrongNorobotsAnswer
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2009-08-28 10:28:58 +02:00
|
|
|
try:
|
2010-06-01 18:38:29 +02:00
|
|
|
from plone.formwidget.captcha.validator import WrongCaptchaCode
|
2010-08-31 19:36:28 +02:00
|
|
|
except ImportError:
|
2009-08-28 10:28:58 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2013-04-18 15:51:57 +02:00
|
|
|
from plone.formwidget.recaptcha.validator import WrongCaptchaCode # noqa
|
2010-08-31 19:36:28 +02:00
|
|
|
except ImportError:
|
2009-08-28 10:28:58 +02:00
|
|
|
pass
|
2009-08-21 23:15:13 +02:00
|
|
|
|
2012-01-14 07:30:43 +01:00
|
|
|
|
2017-07-28 17:58:35 +02:00
|
|
|
@adapter(Interface, IDiscussionLayer, Interface, IField, Interface)
|
2016-07-05 23:12:08 +02:00
|
|
|
@implementer(IValidator)
|
2009-08-15 12:21:26 +02:00
|
|
|
class CaptchaValidator(validator.SimpleFieldValidator):
|
2009-08-21 23:15:13 +02:00
|
|
|
# Object, Request, Form, Field, Widget,
|
2010-06-10 12:51:41 +02:00
|
|
|
# We adapt the CaptchaValidator class to all form fields (IField)
|
2009-08-15 12:21:26 +02:00
|
|
|
|
|
|
|
def validate(self, value):
|
|
|
|
super(CaptchaValidator, self).validate(value)
|
|
|
|
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-10-29 12:43:46 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
2009-08-15 12:21:26 +02:00
|
|
|
|
2010-09-10 15:02:56 +02:00
|
|
|
if settings.captcha in ('captcha', 'recaptcha', 'norobots'):
|
2010-12-16 00:52:56 +01:00
|
|
|
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
2010-06-10 12:51:41 +02:00
|
|
|
name=settings.captcha)
|
|
|
|
if not captcha.verify(input=value):
|
2010-10-09 13:13:28 +02:00
|
|
|
if settings.captcha == 'norobots':
|
|
|
|
raise WrongNorobotsAnswer
|
|
|
|
else:
|
|
|
|
raise WrongCaptchaCode
|
2009-08-15 12:21:26 +02:00
|
|
|
else:
|
2010-06-01 18:38:29 +02:00
|
|
|
return True
|
2010-06-21 11:11:45 +02:00
|
|
|
|
|
|
|
|
2010-06-01 18:38:29 +02:00
|
|
|
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
2010-12-16 00:52:56 +01:00
|
|
|
validator.WidgetValidatorDiscriminators(CaptchaValidator,
|
2010-06-10 12:51:41 +02:00
|
|
|
field=ICaptcha['captcha'])
|