[Checkins] SVN: Products.CMFCore/branches/cookiecrumbler_with_views/ modernizing CookieCrumbler
Yvo Schubbe
y.2010 at wcm-solutions.de
Fri Apr 23 12:30:47 EDT 2010
Log message for revision 111309:
modernizing CookieCrumbler
Changed:
A Products.CMFCore/branches/cookiecrumbler_with_views/
U Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/CookieCrumbler.py
U Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/interfaces/_cookieCrumbler.py
U Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/tests/test_CookieCrumbler.py
-=-
Modified: Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/CookieCrumbler.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/CookieCrumbler.py 2010-04-23 16:02:22 UTC (rev 111307)
+++ Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/CookieCrumbler.py 2010-04-23 16:30:47 UTC (rev 111309)
@@ -28,7 +28,6 @@
from OFS.SimpleItem import SimpleItem
from OFS.PropertyManager import PropertyManager
from OFS.interfaces import IObjectWillBeMovedEvent
-from zExceptions import Redirect
from zope.container.interfaces import IObjectMovedEvent
from zope.interface import implements
from ZPublisher import BeforeTraverse
@@ -261,12 +260,6 @@
if req.get('disable_cookie_login__', 0):
return
- if (self.unauth_page or
- attempt == ATTEMPT_LOGIN or attempt == ATTEMPT_NONE):
- # Modify the "unauthorized" response.
- req._hold(ResponseCleanup(resp))
- resp.unauthorized = self.unauthorized
- resp._unauthorized = self._unauthorized
if attempt != ATTEMPT_NONE:
# Trying to log in or resume a session
if self.cache_header_value:
@@ -295,87 +288,6 @@
resp = self.REQUEST['RESPONSE']
method( resp, self.auth_cookie, quote( ac ) )
- def _cleanupResponse(self):
- # XXX: this method violates the rules for tools/utilities:
- # it depends on self.REQUEST
- resp = self.REQUEST['RESPONSE']
- # No errors of any sort may propagate, and we don't care *what*
- # they are, even to log them.
- try: del resp.unauthorized
- except: pass
- try: del resp._unauthorized
- except: pass
- return resp
-
- security.declarePrivate('unauthorized')
- def unauthorized(self):
- resp = self._cleanupResponse()
- # If we set the auth cookie before, delete it now.
- if resp.cookies.has_key(self.auth_cookie):
- del resp.cookies[self.auth_cookie]
- # Redirect if desired.
- url = self.getUnauthorizedURL()
- if url is not None:
- raise Redirect, url
- # Fall through to the standard unauthorized() call.
- resp.unauthorized()
-
- def _unauthorized(self):
- resp = self._cleanupResponse()
- # If we set the auth cookie before, delete it now.
- if resp.cookies.has_key(self.auth_cookie):
- del resp.cookies[self.auth_cookie]
- # Redirect if desired.
- url = self.getUnauthorizedURL()
- if url is not None:
- resp.redirect(url, lock=1)
- # We don't need to raise an exception.
- return
- # Fall through to the standard _unauthorized() call.
- resp._unauthorized()
-
- security.declarePublic('getUnauthorizedURL')
- def getUnauthorizedURL(self):
- '''
- Redirects to the login page.
- '''
- # XXX: this method violates the rules for tools/utilities:
- # it depends on self.REQUEST
- req = self.REQUEST
- resp = req['RESPONSE']
- attempt = getattr(req, '_cookie_auth', ATTEMPT_NONE)
- if attempt == ATTEMPT_NONE:
- # An anonymous user was denied access to something.
- page_id = self.auto_login_page
- retry = ''
- elif attempt == ATTEMPT_LOGIN:
- # The login attempt failed. Try again.
- page_id = self.auto_login_page
- retry = '1'
- else:
- # An authenticated user was denied access to something.
- page_id = self.unauth_page
- retry = ''
- if page_id:
- page = self.restrictedTraverse(page_id, None)
- if page is not None:
- came_from = req.get('came_from', None)
- if came_from is None:
- came_from = req.get('ACTUAL_URL')
- query = req.get('QUERY_STRING')
- if query:
- # Include the query string in came_from
- if not query.startswith('?'):
- query = '?' + query
- came_from = came_from + query
- url = '%s?came_from=%s&retry=%s&disable_cookie_login__=1' % (
- page.absolute_url(), quote(came_from), retry)
- return url
- return None
-
- # backward compatible alias
- getLoginURL = getUnauthorizedURL
-
security.declarePublic('logout')
def logout(self):
'''
@@ -427,29 +339,7 @@
handle = ob.meta_type + '/' + ob.getId()
BeforeTraverse.unregisterBeforeTraverse(event.oldParent, handle)
-class ResponseCleanup:
- def __init__(self, resp):
- self.resp = resp
- def __del__(self):
- # Free the references.
- #
- # No errors of any sort may propagate, and we don't care *what*
- # they are, even to log them.
- try:
- del self.resp.unauthorized
- except:
- pass
- try:
- del self.resp._unauthorized
- except:
- pass
- try:
- del self.resp
- except:
- pass
-
-
manage_addCCForm = HTMLFile('dtml/addCC', globals())
manage_addCCForm.__name__ = 'addCC'
Modified: Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/interfaces/_cookieCrumbler.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/interfaces/_cookieCrumbler.py 2010-04-23 16:02:22 UTC (rev 111307)
+++ Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/interfaces/_cookieCrumbler.py 2010-04-23 16:30:47 UTC (rev 111309)
@@ -59,21 +59,6 @@
"""# XXX: this method violates the rules for tools/utilities:
# it depends on self.REQUEST """
- def _cleanupResponse():
- """# XXX: this method violates the rules for tools/utilities:
- # it depends on self.REQUEST"""
-
- def unauthorized():
- """Remove authentication cookies and redirect to standard unauthorized"""
-
- def _unauthorized():
- """Remove authentication cookies and redirect to standard _unauthorized"""
-
- def getUnauthorizedURL():
- """
- Redirects to the login page.
- """
-
def logout():
"""
Logs out the user and redirects to the logout page.
Modified: Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/tests/test_CookieCrumbler.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/tests/test_CookieCrumbler.py 2010-04-23 16:02:22 UTC (rev 111307)
+++ Products.CMFCore/branches/cookiecrumbler_with_views/Products/CMFCore/tests/test_CookieCrumbler.py 2010-04-23 16:30:47 UTC (rev 111309)
@@ -172,13 +172,13 @@
req.traverse('/')
self.failIf( req.has_key('__ac'))
- def testAutoLoginRedirection(self):
- # Redirect unauthorized anonymous users to the login page
- from Products.CMFCore.CookieCrumbler import Redirect
+# def testAutoLoginRedirection(self):
+# # Redirect unauthorized anonymous users to the login page
+# from zExceptions import Redirect
+#
+# root, cc, req, credentials = self._makeSite()
+# self.assertRaises(Redirect, req.traverse, '/protected')
- root, cc, req, credentials = self._makeSite()
- self.assertRaises(Redirect, req.traverse, '/protected')
-
def testDisabledAutoLoginRedirection(self):
# When disable_cookie_login__ is set, don't redirect.
from zExceptions.unauthorized import Unauthorized
@@ -197,46 +197,46 @@
req.cookies['__ac'] = credentials
self.assertRaises(Unauthorized, req.traverse, '/protected')
- def testRetryLogin(self):
- # After a failed login, CookieCrumbler should give the user an
- # opportunity to try to log in again.
- from Products.CMFCore.CookieCrumbler import Redirect
+# def testRetryLogin(self):
+# # After a failed login, CookieCrumbler should give the user an
+# # opportunity to try to log in again.
+# from zExceptions import Redirect
+#
+# root, cc, req, credentials = self._makeSite()
+# req.cookies['__ac_name'] = 'israel'
+# req.cookies['__ac_password'] = 'pass-w'
+# try:
+# req.traverse('/protected')
+# except Redirect, s:
+# # Test passed
+# if hasattr(s, 'args'):
+# s = s.args[0]
+# self.failUnless(s.find('came_from=') >= 0)
+# self.failUnless(s.find('retry=1') >= 0)
+# self.failUnless(s.find('disable_cookie_login__=1') >= 0)
+# else:
+# self.fail('Did not redirect')
- root, cc, req, credentials = self._makeSite()
- req.cookies['__ac_name'] = 'israel'
- req.cookies['__ac_password'] = 'pass-w'
- try:
- req.traverse('/protected')
- except Redirect, s:
- # Test passed
- if hasattr(s, 'args'):
- s = s.args[0]
- self.failUnless(s.find('came_from=') >= 0)
- self.failUnless(s.find('retry=1') >= 0)
- self.failUnless(s.find('disable_cookie_login__=1') >= 0)
- else:
- self.fail('Did not redirect')
+# def testLoginRestoresQueryString(self):
+# # When redirecting for login, the came_from form field should
+# # include the submitted URL as well as the query string.
+# import urllib
+# from zExceptions import Redirect
+#
+# root, cc, req, credentials = self._makeSite()
+# req['PATH_INFO'] = '/protected'
+# req['QUERY_STRING'] = 'a:int=1&x:string=y'
+# try:
+# req.traverse('/protected')
+# except Redirect, s:
+# if hasattr(s, 'args'):
+# s = s.args[0]
+# to_find = urllib.quote('/protected?' + req['QUERY_STRING'])
+# self.failUnless(s.find(to_find) >= 0, s)
+# else:
+# self.fail('Did not redirect')
- def testLoginRestoresQueryString(self):
- # When redirecting for login, the came_from form field should
- # include the submitted URL as well as the query string.
- import urllib
- from Products.CMFCore.CookieCrumbler import Redirect
-
- root, cc, req, credentials = self._makeSite()
- req['PATH_INFO'] = '/protected'
- req['QUERY_STRING'] = 'a:int=1&x:string=y'
- try:
- req.traverse('/protected')
- except Redirect, s:
- if hasattr(s, 'args'):
- s = s.args[0]
- to_find = urllib.quote('/protected?' + req['QUERY_STRING'])
- self.failUnless(s.find(to_find) >= 0, s)
- else:
- self.fail('Did not redirect')
-
def testCacheHeaderAnonymous(self):
# Should not set cache-control
root, cc, req, credentials = self._makeSite()
@@ -295,19 +295,19 @@
'abraham')
self.failIf( req.has_key('__ac'))
- def testMidApplicationAutoLoginRedirection(self):
- # Redirect anonymous users to login page if Unauthorized
- # occurs in the middle of the app
- from zExceptions.unauthorized import Unauthorized
+# def testMidApplicationAutoLoginRedirection(self):
+# # Redirect anonymous users to login page if Unauthorized
+# # occurs in the middle of the app
+# from zExceptions.unauthorized import Unauthorized
+#
+# root, cc, req, credentials = self._makeSite()
+# req.traverse('/')
+# try:
+# raise Unauthorized
+# except:
+# req.response.exception()
+# self.assertEqual(req.response.status, 302)
- root, cc, req, credentials = self._makeSite()
- req.traverse('/')
- try:
- raise Unauthorized
- except:
- req.response.exception()
- self.assertEqual(req.response.status, 302)
-
def testMidApplicationAuthenticationButUnauthorized(self):
# Don't redirect already-authenticated users to the login page,
# even when Unauthorized happens in the middle of the app.
@@ -322,16 +322,16 @@
req.response.exception()
self.assertEqual(req.response.status, 401)
- def testRedirectOnUnauthorized(self):
- # Redirect already-authenticated users to the unauthorized
- # handler page if that's what the sysadmin really wants.
- from Products.CMFCore.CookieCrumbler import Redirect
+# def testRedirectOnUnauthorized(self):
+# # Redirect already-authenticated users to the unauthorized
+# # handler page if that's what the sysadmin really wants.
+# from zExceptions import Redirect
+#
+# root, cc, req, credentials = self._makeSite()
+# cc.unauth_page = 'login_form'
+# req.cookies['__ac'] = credentials
+# self.assertRaises(Redirect, req.traverse, '/protected')
- root, cc, req, credentials = self._makeSite()
- cc.unauth_page = 'login_form'
- req.cookies['__ac'] = credentials
- self.assertRaises(Redirect, req.traverse, '/protected')
-
def testLoginRatherThanResume(self):
# When the user presents both a session resume and new
# credentials, choose the new credentials (so that it's
More information about the checkins
mailing list