src/pyams_security/permission.py
changeset 0 f04e1d0a0723
child 2 94e76f8e9828
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_security/permission.py	Thu Feb 19 10:53:29 2015 +0100
@@ -0,0 +1,50 @@
+#
+# 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 IPermission
+
+# import packages
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IPermission)
+class Permission(object):
+    """Permission utility class"""
+
+    id = FieldProperty(IPermission['id'])
+    title = FieldProperty(IPermission['title'])
+    description = FieldProperty(IPermission['description'])
+
+    def __init__(self, values=None, **args):
+        if not isinstance(values, dict):
+            values = args
+        self.id = values.get('id')
+        self.title = values.get('title')
+        self.description = values.get('description')
+
+
+def register_permission(config, permission):
+    """Register a new permission
+
+    Permissions registry is not required.
+    But only registered permissions can be applied via default
+    ZMI features
+    """
+    if not IPermission.providedBy(permission):
+        permission = Permission(permission)
+    config.registry.registerUtility(permission, IPermission, name=permission.id)