src/pyams_security/principal.py
changeset 0 f04e1d0a0723
child 2 94e76f8e9828
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_security/principal.py	Thu Feb 19 10:53:29 2015 +0100
@@ -0,0 +1,72 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_security.interfaces import IPrincipalInfo
+
+# import packages
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_security import _
+
+
+@implementer(IPrincipalInfo)
+class PrincipalInfo(object):
+    """Generic principal info"""
+
+    id = FieldProperty(IPrincipalInfo['id'])
+    title = FieldProperty(IPrincipalInfo['title'])
+    groups = FieldProperty(IPrincipalInfo['groups'])
+
+    def __init__(self, **kwargs):
+        self.id = kwargs.get('id')
+        self.title = kwargs.get('title')
+
+    def __eq__(self, other):
+        return isinstance(other, PrincipalInfo) and (self.id == other.id)
+
+
+@implementer(IPrincipalInfo)
+class UnknownPrincipal(object):
+    """Unknown principal info"""
+
+    id = None
+    title = _("Not logged in")
+
+UnknownPrincipal = UnknownPrincipal()
+
+
+@implementer(IPrincipalInfo)
+class MissingPrincipal(object):
+    """Missing principal info
+
+    This class can be used when a stored principal ID
+    references a principal which can't be found anymore
+    """
+
+    id = FieldProperty(IPrincipalInfo['id'])
+
+    def __init__(self, **kwargs):
+        self.id = kwargs.get('id')
+
+    @property
+    def title(self):
+        return '<MissingPrincipal: {0}>'.format(self.id)
+
+    def __eq__(self, other):
+        return isinstance(other, PrincipalInfo) and (self.id == other.id)