plone.app.discussion/plone/app/discussion/browser/validator.py

61 lines
2.0 KiB
Python
Raw Normal View History

"""Captcha validator, see captcha.txt for design notes.
"""
from Acquisition import aq_inner
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
from z3c.form import validator
from z3c.form.interfaces import IValidator
2017-07-28 17:58:35 +02:00
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.component import queryUtility
from zope.interface import implementer
from zope.interface import Interface
from zope.schema.interfaces import IField
try:
from collective.z3cform.norobots.validator import WrongNorobotsAnswer
except ImportError:
pass
try:
from plone.formwidget.captcha.validator import WrongCaptchaCode
except ImportError:
pass
try:
2013-04-18 15:51:57 +02:00
from plone.formwidget.recaptcha.validator import WrongCaptchaCode # noqa
except ImportError:
pass
2012-01-14 07:30:43 +01:00
@implementer(IValidator)
@adapter(Interface, IDiscussionLayer, Interface, IField, Interface)
class CaptchaValidator(validator.SimpleFieldValidator):
# Object, Request, Form, Field, Widget,
# We adapt the CaptchaValidator class to all form fields (IField)
def validate(self, value):
super().validate(value)
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
2022-05-01 23:14:09 +02:00
if settings.captcha in ("captcha", "recaptcha", "norobots"):
captcha = getMultiAdapter(
(aq_inner(self.context), self.request), name=settings.captcha
)
if not captcha.verify(input=value):
2022-05-01 23:14:09 +02:00
if settings.captcha == "norobots":
raise WrongNorobotsAnswer
else:
raise WrongCaptchaCode
else:
return True
# Register Captcha validator for the Captcha field in the ICaptcha Form
2022-05-01 23:14:09 +02:00
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptcha["captcha"])