Added getPrincipal function
authorThierry Florac <tflorac@ulthar.net>
Tue, 02 Mar 2010 23:35:23 +0100
changeset 34 9b882e41b6d2
parent 33 11b11c5a4f9e
child 35 0303172ca2db
Added getPrincipal function
ztfy/utils/security.py
--- a/ztfy/utils/security.py	Tue Mar 02 23:34:55 2010 +0100
+++ b/ztfy/utils/security.py	Tue Mar 02 23:35:23 2010 +0100
@@ -18,11 +18,14 @@
 from persistent.dict import PersistentDict
 
 # import Zope3 interfaces
+from zope.app.authentication.interfaces import IPrincipalInfo
 
 # import local interfaces
 
 # import Zope3 packages
 from zc.set import Set
+from zope.app import zapi
+from zope.interface import implements
 from zope.security.proxy import removeSecurityProxy
 
 # import local packages
@@ -45,3 +48,27 @@
     else:
         result = removeSecurityProxy(value)
     return result
+
+
+class MissingPrincipal(object):
+
+    implements(IPrincipalInfo)
+
+    def __init__(self, id):
+        self.id = id
+
+    @property
+    def title(self):
+        return _("< missing principal %s>") % self.id
+
+    @property
+    def description(self):
+        return _("This principal can't be found in any authentication utility...")
+
+
+def getPrincipal(uid):
+    principals = zapi.principals()
+    try:
+        return principals.getPrincipal(uid)
+    except:
+        return MissingPrincipal(uid)