[Zope3-checkins] SVN: Zope3/trunk/src/zope/security/ Forgot to
check in last checkin
Amos Brocco
amos.brocco at mriyasoftware.com
Wed Oct 13 10:37:35 EDT 2004
Log message for revision 28084:
Forgot to check in last checkin
Changed:
A Zope3/trunk/src/zope/security/configure.zcml
A Zope3/trunk/src/zope/security/group.py
A Zope3/trunk/src/zope/security/groupprincipals.txt
A Zope3/trunk/src/zope/security/tests/groups_principals.txt
A Zope3/trunk/src/zope/security/tests/grouptest.txt
A Zope3/trunk/src/zope/security/tests/test_group.py
A Zope3/trunk/src/zope/security/tests/test_groupprincipals.py
-=-
Added: Zope3/trunk/src/zope/security/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/security/configure.zcml 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/configure.zcml 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,49 @@
+<configure
+ xmlns='http://namespaces.zope.org/zope'
+ xmlns:browser='http://namespaces.zope.org/browser'
+ i18n_domain="security"
+ xmlns:i18n="http://namespaces.zope.org/i18n"
+ >
+
+ <browser:addMenuItem
+ title="Group"
+ description="A principals group"
+ class=".group.Group"
+ permission="zope.ManageServices"
+ />
+
+
+<content class=".group.Group">
+ <require
+ permission="zope.ManageContent"
+ interface=".interfaces.IGroup"
+ set_schema=".interfaces.IGroup"
+ />
+ <require
+ permission="zope.ManageContent"
+ interface="zope.app.groupscontainer.interfaces.IGroupContained"
+ />
+ <implements
+ interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
+ />
+ </content>
+
+<browser:editform
+ schema=".interfaces.IGroup"
+ label="Change group information"
+ name="edit.html"
+ menu="zmi_views" title="Edit"
+ permission="zope.ManageContent"
+ />
+
+ <subscriber
+ factory="zope.security.group.updateGroupsForPrincipal"
+ for="zope.security.interfaces.IPrincipalCreatedEvent"
+ />
+
+ <subscriber
+ factory="zope.security.group.updatePrincipalsFromGroups"
+ for="zope.security.interfaces.IGroupChangedEvent"
+ />
+
+</configure>
Added: Zope3/trunk/src/zope/security/group.py
===================================================================
--- Zope3/trunk/src/zope/security/group.py 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/group.py 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,105 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Zope Group implementation
+
+$Id: group.py 27237 2004-10-12 09:33:00 mriya3 $
+
+"""
+
+from zope.security.interfaces import IGroup, IGroupChangedEvent, IPrincipalCreatedEvent
+from persistent import Persistent
+from zope.interface import implements
+from zope.app.groupscontainer.interfaces import IGroupContained, IGroupsFolder
+from types import StringTypes
+from zope.event import notify
+
+
+class Group(Persistent):
+
+ implements(IGroup, IGroupContained)
+
+ __parent__ = __name__ = None
+
+ def __init__(self, id='', title='', description='', principalsids=[]):
+ self.id = id
+ self.title = title
+ self.description = description
+ self.__principals = principalsids
+
+ def addPrincipals(self, *principalIds):
+ tmpNewList = self.__principals
+ for principalId in principalIds:
+ if not principalId in self.__principals:
+ tmpNewList.append(principalId)
+ self.setPrincipals(tmpNewList)
+
+ def removePrincipals(self, *principalIds):
+ tmpNewList = self.__principals
+ for principalId in principalIds:
+ if principalId in tmpNewList:
+ tmpNewList.remove(principalId)
+ self.setPrincipals(tmpNewList)
+
+ def containsPrincipal(self, principalId):
+ return principalId in self.__principals
+
+ def getPrincipals(self):
+ return self.__principals
+
+ def setPrincipals(self, prinlist):
+ origprincipals = self.__principals
+ self.__principals = prinlist
+ notify(GroupChangedEvent(self, origprincipals))
+
+ principals = property(getPrincipals, setPrincipals)
+
+
+class PrincipalCreatedEvent:
+ """Event on principal creation"""
+ implements(IPrincipalCreatedEvent)
+
+ def __init__(self, principal):
+ self.principal = principal
+
+class GroupChangedEvent:
+ """Event on group change"""
+ implements(IGroupChangedEvent)
+
+ def __init__(self, group, originalids):
+ self.group = group
+ self.originalids = originalids
+
+
+def updateGroupsForPrincipal(event):
+ """Updates group informations when a principal is created"""
+ principal = event.principal
+ groupfolder = zope.app.zapi.getUtility(IGroupsFolder)
+ groups = groupfolder.getGroupsForPrincipal(principal.id)
+ principal.groups = groups
+
+def updatePrincipalsFromGroups(event):
+ """Updates principals when a group changes"""
+ group = event.group
+ originalids = event.originalids
+ groupfolder = zope.app.zapi.getUtility(IGroupsFolder)
+ for principalid in originalids:
+ authservice = zope.app.zapi.getService(IAuthenticationService)
+ principal = authservice.getPrincipal(principalid)
+ groups = groupfolder.getGroupsForPrincipal(principal.id)
+ principal.groups = groups
+
+
+
+
+
Added: Zope3/trunk/src/zope/security/groupprincipals.txt
===================================================================
--- Zope3/trunk/src/zope/security/groupprincipals.txt 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/groupprincipals.txt 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,32 @@
+Groups & Principals
+================
+
+We import the needed modules
+ >>> from zope.security.group import Group
+ >>> from zope.security.principalregistry import Principal
+ >>> from zope.app.groupcontainer.groupfolder import GroupFolder
+ >>> from zope.app.pas.principalsplugins import PrincipalFactory
+ >>> from zope.
+
+
+We first create a group folder and add some groups:
+
+ >>> groups = GroupFolder()
+ >>> geeks = Group(id='geeks', principals=['srichter','jim'])
+ >>> newbies = Group(id='newbies', principals=['amos','claudia'])
+ >>> sprinters = Group(id='sprinters', principals=['srichter', 'jim', 'amos','claudia'])
+ >>> for g in [geeks, sprinters, newbies]: groups[g.id] = g
+
+Now we create some principals:
+1) Instantiate a Principal Factory
+ >>> pf = PrincipalFactory()
+ >>> srichter = pf.createAuthenticatedPrincipal(id='srichter', {} , TestRequest())
+ >>> groups.getGroupsForPrincipal(srichter.id)
+ ['geeks', 'sprinters']
+ >>> thomas = pf.createAuthenticatedPrincipal(id='thomas', {} , TestRequest())
+ >>> groups.getGroupsForPrincipal(thomas.id)
+ []
+ >>> amos = pf.createAuthenticatedPrincipal(id='amos', {} , TestRequest())
+ >>> groups.getGroupsForPrincipal(amos.id)
+ ['newbies','sprinters']
+
Added: Zope3/trunk/src/zope/security/tests/groups_principals.txt
===================================================================
--- Zope3/trunk/src/zope/security/tests/groups_principals.txt 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/tests/groups_principals.txt 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,32 @@
+Groups & Principals
+================
+
+We import the needed modules
+ >>> from zope.security.group import Group
+ >>> from zope.app.groupscontainer.groupsfolder import GroupsFolder
+ >>> from zope.app.pas.principalplugins import PrincipalFactory
+ >>> from zope.publisher.tests.httprequest import TestRequest
+
+We first create a group folder and add some groups:
+
+ >>> groups = GroupsFolder()
+ >>> geeks = Group(id='geeks', principalsids=['srichter','jim'])
+ >>> newbies = Group(id='newbies', principalsids=['amos','claudia'])
+ >>> sprinters = Group(id='sprinters', principalsids=['srichter', 'jim', 'amos','claudia'])
+ >>> for g in [geeks, sprinters, newbies]: groups[g.id] = g
+
+Now we create some principals and check if they operate correctly:
+1) Instantiate a Principal Factory
+ >>> pf = PrincipalFactory()
+
+2) Get some principals
+ >>> srichter = pf.createAuthenticatedPrincipal('srichter', {} , TestRequest())
+ >>> groups.getGroupsForPrincipal(srichter.id)
+ ['geeks', 'sprinters']
+ >>> thomas = pf.createAuthenticatedPrincipal('thomas', {} , TestRequest())
+ >>> groups.getGroupsForPrincipal(thomas.id)
+ []
+ >>> amos = pf.createAuthenticatedPrincipal('amos', {} , TestRequest())
+ >>> groups.getGroupsForPrincipal(amos.id)
+ ['sprinters', 'newbies']
+
Added: Zope3/trunk/src/zope/security/tests/grouptest.txt
===================================================================
--- Zope3/trunk/src/zope/security/tests/grouptest.txt 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/tests/grouptest.txt 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,32 @@
+Group Implementation
+==================
+
+A group is used to store a list of principals.
+First we create a new group
+ >>> from zope.security.group import Group
+ >>> foogroup = Group(id='foogroup', title='A typical foo group', description='Foo group', principalsids=['1', '2', '3', '4', '5'])
+ >>> foogroup.getPrincipals()
+ ['1', '2', '3', '4', '5']
+
+We can add additional principals with the addPrincipal method:
+ >>> foogroup.addPrincipals('8', '12')
+ >>> foogroup.getPrincipals()
+ ['1', '2', '3', '4', '5', '8', '12']
+
+It is not possible to have two times the same id:
+ >>> foogroup.addPrincipals('8')
+ >>> foogroup.getPrincipals()
+ ['1', '2', '3', '4', '5', '8', '12']
+
+Or delete a particular principal:
+ >>> foogroup.removePrincipals('3', '12')
+ >>> foogroup.getPrincipals()
+ ['1', '2', '4', '5', '8']
+
+To check if a principal belongs to the group we use the containsPrincipal method:
+ >>> foogroup.containsPrincipal('5')
+ True
+ >>> foogroup.containsPrincipal('13')
+ False
+
+
Added: Zope3/trunk/src/zope/security/tests/test_group.py
===================================================================
--- Zope3/trunk/src/zope/security/tests/test_group.py 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/tests/test_group.py 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Groups folder tests.
+
+$Id: tests_group.py 27237 2004-10-12 10:49:00 mriya3 $
+"""
+
+
+import unittest
+from zope.testing.doctest import DocFileSuite
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(DocFileSuite('grouptest.txt'))
+ return suite
+
+
Added: Zope3/trunk/src/zope/security/tests/test_groupprincipals.py
===================================================================
--- Zope3/trunk/src/zope/security/tests/test_groupprincipals.py 2004-10-13 14:33:39 UTC (rev 28083)
+++ Zope3/trunk/src/zope/security/tests/test_groupprincipals.py 2004-10-13 14:37:34 UTC (rev 28084)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Groups folder tests.
+
+$Id: tests_groupprincipals.py 27237 2004-10-12 10:49:00 mriya3 $
+"""
+
+
+import unittest
+from zope.testing.doctest import DocFileSuite
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(DocFileSuite('groups_principals.txt'))
+ return suite
+
+
More information about the Zope3-Checkins
mailing list