changeset 32:b25c89d693cf

version 0.2.3 - first version with access control! * defaults to open when no access info * uses any logged in user for local access
author casties
date Tue, 11 Apr 2006 19:27:57 +0200
parents c6451e8d5d23
children b3fe3a50f605
files documentViewer.py version.txt zpt/thumbs_main.zpt
diffstat 3 files changed, 80 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/documentViewer.py	Mon Apr 10 21:51:50 2006 +0200
+++ b/documentViewer.py	Tue Apr 11 19:27:57 2006 +0200
@@ -1,10 +1,8 @@
-
-genericDigilib="http://nausikaa2.rz-berlin.mpg.de/digitallibrary/"
-
 from OFS.Folder import Folder
 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
 from AccessControl import ClassSecurityInfo
+from AccessControl import getSecurityManager
 from Globals import package_home
 
 from Ft.Xml.Domlette import NonvalidatingReader
@@ -28,6 +26,8 @@
     
 
 def getTextFromNode(nodename):
+    if nodename is None:
+        return ""
     nodelist=nodename.childNodes
     rc = ""
     for node in nodelist:
@@ -37,9 +37,9 @@
 
 import socket
 
-def urlopen(url):
+def urlopen(url,timeout=2):
         """urlopen mit timeout"""
-        socket.setdefaulttimeout(2)
+        socket.setdefaulttimeout(timeout)
         ret=urllib.urlopen(url)
         socket.setdefaulttimeout(5)
         return ret
@@ -69,7 +69,7 @@
     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
 
     
-    def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10):
+    def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"):
         """init document viewer"""
         self.id=id
         self.title=title
@@ -80,6 +80,8 @@
             self.digilibBaseUrl = digilibBaseUrl
         self.thumbcols = thumbcols
         self.thumbrows = thumbrows
+        # authgroups is list of authorized groups (delimited by ,)
+        self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
         # add template folder so we can always use template.something
         self.manage_addFolder('template')
 
@@ -131,6 +133,25 @@
         else:
             return style    
         
+    def accessOK(self, docinfo):
+        """returns if access to the resource is granted"""
+        access = docinfo.get('accessType', None)
+        if access is None:
+            # no information - no access (not yet)
+            return True
+        elif access == 'free':
+            return True
+        
+        print "access: ", access, " authgroups: ", self.authgroups
+        if access in self.authgroups:
+            # local access OK
+            user = getSecurityManager().getUser().getUserName()
+            print "user: ", user
+            return (user != "Anonymous User")
+        
+        zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "unknown access group %s"%access)
+        return False
+                
         
     def getDirinfoFromDigilib(self,path,docinfo=None):
         """gibt param von dlInfo aus"""
@@ -151,13 +172,41 @@
         zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo:size"%params)
         
         if params:
-            docinfo['numPages'] = getTextFromNode(params[0])
+            docinfo['numPages'] = int(getTextFromNode(params[0]))
         else:
             docinfo['numPages'] = 0
                         
         return docinfo
+    
             
+    def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None):
+        """gets authorization info from the index.meta file at url or given by dom"""
+        zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))
+        
+        access = None
+        
+        if docinfo is None:
+            docinfo = {}
+            
+        if dom is None:
+            server=self.digilibBaseUrl+"/servlet/Texter?fn="
+            path="/".join(path.split("/")[0:-1])
+            metaUrl=server+path+"/index.meta"
+            try:
+                dom = NonvalidatingReader.parseUri(metaUrl)
+            except:
+                return docinfo
+            
+        acctype = dom.xpath("//access-conditions/access/@type")
+        if acctype and (len(acctype)>0):
+            access=acctype[0].value
+            if access == 'group':
+                access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower()
+            
+        docinfo['accessType'] = access
+        return docinfo
     
+        
     def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):
         """gets bibliographical info from the index.meta file at url or given by dom"""
         zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))
@@ -183,6 +232,7 @@
         bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
         bibmap=metaData.generateMappingForType(bibtype)
         print "bibmap: ", bibmap, " for: ", bibtype
+        # if there is no mapping bibmap is empty (mapping sometimes has empty fields)
         if len(bibmap) > 0 and len(bibmap['author'][0]) > 0:
             docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
             docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
@@ -191,21 +241,31 @@
         return docinfo
 
         
-    def getDocinfoFromTextTool(self,url,docinfo=None):
+    def getDocinfoFromTextTool(self,url,dom=None,docinfo=None):
        """parse texttool tag in index meta"""
        zLOG.LOG("documentViewer (getdocinfofromtexttool)", zLOG.INFO,"url: %s"%(url))
        if docinfo is None:
            docinfo = {}
            
-       try:
-           dom = NonvalidatingReader.parseUri(url)
-       except:
-           zLOG.LOG("documentViewer (parseUrlTexttool)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])
-           raise IOError("Unable to get texttool info from %s"%(url))
+       if dom is None:
+           try:
+               dom = NonvalidatingReader.parseUri(url)
+           except:
+               zLOG.LOG("documentViewer (parseUrlTexttool)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])
+               raise IOError("Unable to get texttool info from %s"%(url))
+       
+       archiveNames=dom.xpath("//resource/name")
+       if archiveNames and (len(archiveNames)>0):
+           archiveName=getTextFromNode(archiveNames[0])
        
        archivePaths=dom.xpath("//resource/archive-path")
        if archivePaths and (len(archivePaths)>0):
            archivePath=getTextFromNode(archivePaths[0])
+           # clean up archive path
+           if archivePath[0] != '/':
+               archivePath = '/' + archivePath
+           if not archivePath.endswith(archiveName):
+               archivePath += "/" + archiveName
        else:
            archivePath=None
        
@@ -216,6 +276,7 @@
            image=None
            
        if image and archivePath:
+           print "image: ", image, " archivepath: ", archivePath
            image=os.path.join(archivePath,image)
            image=image.replace("/mpiwg/online",'')
            docinfo=self.getDirinfoFromDigilib(image,docinfo=docinfo)
@@ -233,6 +294,7 @@
            docinfo['textURL'] = textUrl
                      
        docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)
+       docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)
        return docinfo
    
 
@@ -248,6 +310,7 @@
         docinfo['imageURL'] = imageUrl
         
         docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo)
+        docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo)
         return docinfo
     
     
@@ -360,14 +423,14 @@
         except:
             return None
     
-    def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,RESPONSE=None):
+    def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None):
         """init document viewer"""
         self.title=title
         self.imageViewerUrl=imageViewerUrl
         self.digilibBaseUrl = digilibBaseUrl
         self.thumbrows = thumbrows
         self.thumbcols = thumbcols
-        
+        self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
         if RESPONSE is not None:
             RESPONSE.redirect('manage_main')
     
--- a/version.txt	Mon Apr 10 21:51:50 2006 +0200
+++ b/version.txt	Tue Apr 11 19:27:57 2006 +0200
@@ -1,1 +1,1 @@
-DocumentViewer 0.2.2
\ No newline at end of file
+DocumentViewer 0.2.3
\ No newline at end of file
--- a/zpt/thumbs_main.zpt	Mon Apr 10 21:51:50 2006 +0200
+++ b/zpt/thumbs_main.zpt	Tue Apr 11 19:27:57 2006 +0200
@@ -10,7 +10,7 @@
       tal:attributes="selected python:(start==grp*grpsize+1); value python:(grp*grpsize+1)"
       tal:content="python:(grp*grpsize+1)"/>
   </select>
-  <span tal:condition="python:(start+grpsize<docinfo['numPages'])">
+  <span tal:condition="python:(start+grpsize<int(docinfo['numPages']))">
     <a tal:attributes="href python:here.getLink(param='start',val=start+grpsize)">&gt;</a>
   </span>
 </div>