[Zope3-checkins] CVS: Products3/NewsSite - browser.py:1.6

Tres Seaver tseaver@zope.com
Tue, 1 Apr 2003 10:48:59 -0500


Update of /cvs-repository/Products3/NewsSite
In directory cvs.zope.org:/tmp/cvs-serv3552

Modified Files:
	browser.py 
Log Message:


  - Reorder imports.

  - Add new base class for views which want to force authentication
    (e.g., show logged-in user even for pages which are not denied to
    anonymous).


=== Products3/NewsSite/browser.py 1.5 => 1.6 ===
--- Products3/NewsSite/browser.py:1.5	Thu Mar 27 11:04:57 2003
+++ Products3/NewsSite/browser.py	Tue Apr  1 10:48:59 2003
@@ -18,27 +18,54 @@
 __metaclass__ = type
 
 from zope.schema import getFields
-from zope.component import getAdapter, queryAdapter
+from zope.schema.interfaces import ValidationError
+from zope.component import getAdapter
+from zope.component import queryAdapter
 from zope.component import getView
+from zope.component import getService
 from zope.component import queryUtility
-
 from zope.proxy.context import ContextWrapper
+from zope.security.management import getSecurityManager
 
-from zope.schema.interfaces import ValidationError
+from zope.app.interfaces.dublincore import IZopeDublinCore
 from zope.app.event import publish
 from zope.app.event.objectevent import ObjectCreatedEvent
-from zope.app.interfaces.dublincore import IZopeDublinCore
 from zope.app.browser.container.adding import Adding
-from zope.app.browser.workflow.stateful.interfaces \
-    import IContentFilterAdapter
+from zope.app.browser.workflow.stateful.interfaces import IContentFilterAdapter
+from zope.app.dublincore.annotatableadapter import ZDCAnnotatableAdapter
+from zope.app.services.servicenames import Authentication
 
 from interfaces import INewsSite, INewsSiteFormSchema
 from interfaces import ISyndicationPolicies
-from zope.app.dublincore.annotatableadapter \
-     import ZDCAnnotatableAdapter
 
 from sitesetup import setupSite
 
+class BaseNewsSiteView:
+    """Implement requirements which template expects of all "public" views.
+    """
+    def getUserInfo(self):
+
+        #import pdb; pdb.set_trace()
+        authsvc = getService(self.context, Authentication)
+        authsvc.authenticate(self.request)
+        principal = getSecurityManager().getPrincipal()
+        return {'id' : principal.getId(),
+                'title' : principal.getTitle(),
+                'roles' : principal.getRoles()
+               }
+
+    def unauth(self):
+        #import pdb; pdb.set_trace()
+        authsvc = getService(self.context, Authentication)
+        authsvc.authenticate(self.request)
+        principal = getSecurityManager().getPrincipal()
+        roles = principal.getRoles()
+        if roles:
+            url = getView(self.context, 'absolute_url', self.request)
+            # Append '/' to get browsers to sent 'Authorization:' header.
+            self.request.response.redirect( '%s/' % url )
+        else:
+            authsvc.unauthorized(None, self.request)
 
 class NewsSiteAttributesAdapter(ZDCAnnotatableAdapter):