Commit ccb885b0 authored by Ivan Tyagov's avatar Ivan Tyagov

Use proper Cache Plugin API. Use sessions (which can be distributed with

proper cache plugin) rather than local dict.
parent 89986851
...@@ -144,27 +144,20 @@ class CaptchaWidget(Widget.TextWidget): ...@@ -144,27 +144,20 @@ class CaptchaWidget(Widget.TextWidget):
A widget that displays a Captcha. A widget that displays a Captcha.
""" """
def __init__(self): def add_captcha(self, portal_sessions, key, value):
# Associate a captcha key and (the right answer, the generation date) session = portal_sessions[key]
self.__captcha_cache = {} if session.has_key(key):
def add_captcha(self, key, value):
# First, cleanup the cache
cleanup_time = time.time() - 3600
for item in self.__captcha_cache.items():
if item[1][1] < cleanup_time:
del(self.__captcha_cache[item[0]])
# Then add the value if needed
if self.__captcha_cache.has_key(key):
return False return False
self.__captcha_cache[key] = (str(value), time.time()) session[key] = value
return True return True
def validate_answer(self, key, value): def validate_answer(self, portal_sessions, key, value):
if not(self.__captcha_cache.has_key(key)): session = portal_sessions[key]
if not(session.has_key(key)):
return False return False
result = (self.__captcha_cache[key][0] == value) result = (session[key] == value)
del(self.__captcha_cache[key]) # Forbid several use of the same captcha. # Forbid several use of the same captcha.
del(session[key])
return result return result
property_names = Widget.Widget.property_names + ['captcha_type'] property_names = Widget.Widget.property_names + ['captcha_type']
...@@ -185,11 +178,11 @@ class CaptchaWidget(Widget.TextWidget): ...@@ -185,11 +178,11 @@ class CaptchaWidget(Widget.TextWidget):
""" """
captcha_key = None captcha_key = None
captcha_field = None captcha_field = None
captcha_type = field.get_value("captcha_type") captcha_type = field.get_value("captcha_type")
provider = CaptchaProviderFactory.getProvider(captcha_type) provider = CaptchaProviderFactory.getProvider(captcha_type)
(captcha_key, captcha_answer) = provider.generate(field) (captcha_key, captcha_answer) = provider.generate(field)
while not(self.add_captcha(md5.new(captcha_key).hexdigest(), captcha_answer)): portal_sessions = field.getPortalObject().portal_sessions
while not(self.add_captcha(portal_sessions, md5.new(captcha_key).hexdigest(), captcha_answer)):
(captcha_key, captcha_answer) = provider.generate(field) (captcha_key, captcha_answer) = provider.generate(field)
captcha_field = provider.getHTML(field, captcha_key) captcha_field = provider.getHTML(field, captcha_key)
...@@ -222,8 +215,8 @@ class CaptchaValidator(Validator.Validator): ...@@ -222,8 +215,8 @@ class CaptchaValidator(Validator.Validator):
def validate(self, field, key, REQUEST): def validate(self, field, key, REQUEST):
value = REQUEST.get(key, None) value = REQUEST.get(key, None)
cache_key = REQUEST.get("__captcha_" + key + "__") cache_key = REQUEST.get("__captcha_" + key + "__")
portal_sessions = field.getPortalObject().portal_sessions
if not(CaptchaWidgetInstance.validate_answer(cache_key, value)): if not(CaptchaWidgetInstance.validate_answer(portal_sessions, cache_key, value)):
self.raise_error('wrong_captcha', field) self.raise_error('wrong_captcha', field)
return value return value
......
...@@ -215,7 +215,8 @@ class SessionTool(BaseTool): ...@@ -215,7 +215,8 @@ class SessionTool(BaseTool):
session._updateSessionId(session_id) session._updateSessionId(session_id)
if session_duration is None: if session_duration is None:
# set session duration (this is used from backend storage machinery for expire purposes) # set session duration (this is used from backend storage machinery for expire purposes)
session_duration = self.portal_caches[SESSION_CACHE_FACTORY].objectValues()[0].cache_duration cache_plugin = self.portal_caches[SESSION_CACHE_FACTORY].objectValues()[0]
session_duration = cache_plugin.getCacheDuration()
session._updateSessionDuration(session_duration) session._updateSessionDuration(session_duration)
storage_plugin.set(session_id, SESSION_SCOPE, session, session_duration) storage_plugin.set(session_id, SESSION_SCOPE, session, session_duration)
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment