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