Updated UnauthorizedExceptionView to correctly handle AJAX authentication errors
authorThierry Florac <thierry.florac@onf.fr>
Tue, 14 Oct 2014 14:32:53 +0200
changeset 82 f1b437b926fd
parent 81 a67eb3f6b789
child 83 782a12d62702
Updated UnauthorizedExceptionView to correctly handle AJAX authentication errors
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))