# HG changeset patch # User Thierry Florac # Date 1413289973 -7200 # Node ID f1b437b926fdd28f5f63e0b3de053f259e36af21 # Parent a67eb3f6b789b975456838f83ee97f3ee9cdca7b Updated UnauthorizedExceptionView to correctly handle AJAX authentication errors diff -r a67eb3f6b789 -r f1b437b926fd src/ztfy/myams/page.py --- a/src/ztfy/myams/page.py Tue Oct 14 14:31:54 2014 +0200 +++ b/src/ztfy/myams/page.py Tue Oct 14 14:32:53 2014 +0200 @@ -15,6 +15,7 @@ from zope.pagetemplate.interfaces import IPageTemplate # import local interfaces +from zope.security.interfaces import IUnauthorized from ztfy.myams.interfaces import IInnerPage, IModalPage # import Zope3 packages @@ -123,8 +124,17 @@ principal = self.request.principal auth = getUtility(IAuthentication) auth.unauthorized(principal.id, self.request) + try: + context = self.context.args[0] + except: + context = self.context self.request.response.setStatus(200) - parent = getParent(self.context, IPersistent) - writer = getUtility(IJSONWriter) - return writer.write({'status': 'modal', - 'location': 'login-dialog.html?came_from=%s' % absoluteURL(parent, self.request)}) + if ('/@@ajax/' in self.request.getURL()) or (self.request.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'): + # Send JSON result when using AJAX + parent = getParent(context, IPersistent) + writer = getUtility(IJSONWriter) + return writer.write({'status': 'modal', + 'location': 'login-dialog.html?came_from=%s' % absoluteURL(parent, self.request)}) + else: + # else do a simple redirect... + self.request.response.redirect('login.html?came_from=%s' % absoluteURL(context, self.request))