[Checkins] SVN: Sandbox/nadako/z3c.ownership/trunk/ Initial import.

Dan Korostelev nadako at gmail.com
Tue Mar 10 13:09:41 EDT 2009


Log message for revision 97812:
  Initial import.

Changed:
  _U  Sandbox/nadako/z3c.ownership/trunk/
  A   Sandbox/nadako/z3c.ownership/trunk/CHANGES.txt
  A   Sandbox/nadako/z3c.ownership/trunk/README.txt
  A   Sandbox/nadako/z3c.ownership/trunk/bootstrap.py
  A   Sandbox/nadako/z3c.ownership/trunk/buildout.cfg
  A   Sandbox/nadako/z3c.ownership/trunk/setup.py
  A   Sandbox/nadako/z3c.ownership/trunk/src/
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/__init__.py
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/README.txt
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/__init__.py
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/configure.zcml
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/interfaces.py
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/ownership.py
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/subscriber.py
  A   Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/tests.py

-=-

Property changes on: Sandbox/nadako/z3c.ownership/trunk
___________________________________________________________________
Added: svn:ignore
   + .installed.cfg
bin
parts
coverage
develop-eggs


Added: Sandbox/nadako/z3c.ownership/trunk/CHANGES.txt
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/CHANGES.txt	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/CHANGES.txt	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,8 @@
+=======
+CHANGES
+=======
+
+0.1.0 (unreleased)
+------------------
+
+Initial release.


Property changes on: Sandbox/nadako/z3c.ownership/trunk/CHANGES.txt
___________________________________________________________________
Added: svn:eol-style
   + native

Added: Sandbox/nadako/z3c.ownership/trunk/README.txt
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/README.txt	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/README.txt	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,3 @@
+This package provides very simple object ownership functionality for Zope3. It's
+based on zope.securitypolicy and works by setting special role for given principal
+on the object in question.


Property changes on: Sandbox/nadako/z3c.ownership/trunk/README.txt
___________________________________________________________________
Added: svn:eol-style
   + native

Added: Sandbox/nadako/z3c.ownership/trunk/bootstrap.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/bootstrap.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/bootstrap.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,52 @@
+##############################################################################
+#
+# Copyright (c) 2006 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.
+#
+##############################################################################
+"""Bootstrap a buildout-based project
+
+Simply run this script in a directory containing a buildout.cfg.
+The script accepts buildout command-line options, so you can
+use the -c option to specify an alternate configuration file.
+
+$Id: bootstrap.py 73774 2007-03-27 15:11:34Z dobe $
+"""
+
+import os, shutil, sys, tempfile, urllib2
+
+tmpeggs = tempfile.mkdtemp()
+
+ez = {}
+exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
+                     ).read() in ez
+ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
+
+import pkg_resources
+
+cmd = 'from setuptools.command.easy_install import main; main()'
+if sys.platform == 'win32':
+    cmd = '"%s"' % cmd # work around spawn lamosity on windows
+
+ws = pkg_resources.working_set
+assert os.spawnle(
+    os.P_WAIT, sys.executable, sys.executable,
+    '-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
+    dict(os.environ,
+         PYTHONPATH=
+         ws.find(pkg_resources.Requirement.parse('setuptools')).location
+         ),
+    ) == 0
+
+ws.add_entry(tmpeggs)
+ws.require('zc.buildout')
+import zc.buildout.buildout
+zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
+shutil.rmtree(tmpeggs)


Property changes on: Sandbox/nadako/z3c.ownership/trunk/bootstrap.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/buildout.cfg
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/buildout.cfg	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/buildout.cfg	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,18 @@
+[buildout]
+develop = .
+parts = test coverage-test coverage-report
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = z3c.ownership
+
+[coverage-test]
+recipe = zc.recipe.testrunner
+eggs = z3c.ownership
+defaults = ['--coverage', '../../coverage']
+
+[coverage-report]
+recipe = zc.recipe.egg
+eggs = z3c.coverage
+scripts = coverage=coverage-report
+arguments = ('coverage', 'coverage/report')

Added: Sandbox/nadako/z3c.ownership/trunk/setup.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/setup.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/setup.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,69 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""Setup for z3c.ownership package
+
+$Id$
+"""
+import os
+from setuptools import find_packages, setup
+
+def read(*rnames):
+    return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
+
+setup(
+    name='z3c.ownership',
+    version='0.1.0dev',
+    author='Dan Korostelev and Zope Community',
+    author_email='zope-dev at zope.org',
+    description='Object ownership functionality based on zope.securitypolicy',
+    long_description=(
+        read('README.txt')
+        + '\n.. contents::\n\n' +
+        read('src', 'z3c', 'ownership', 'README.txt')
+        + '\n\n' +
+        read('CHANGES.txt')
+        ),
+    keywords="zope security ownership",
+    classifiers=[
+        'Development Status :: 3 - Alpha',
+        'Environment :: Web Environment',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: Zope Public License',
+        'Programming Language :: Python',
+        'Natural Language :: English',
+        'Operating System :: OS Independent',
+        'Topic :: Internet :: WWW/HTTP',
+        'Framework :: Zope3'
+        ],
+    url='http://pypi.python.org/pypi/z3c.ownership',
+    license='ZPL 2.1',
+    packages=find_packages('src'),
+    package_dir={'': 'src'},
+    namespace_packages=['z3c'],
+    install_requires=[
+        'setuptools',
+        'zope.app.security',
+        'zope.component',
+        'zope.event',
+        'zope.i18nmessageid',
+        'zope.interface',
+        'zope.lifecycleevent',
+        'zope.proxy',
+        'zope.schema',
+        'zope.security',
+        'zope.securitypolicy',
+        ],
+    include_package_data=True,
+    zip_safe=False,
+    )


Property changes on: Sandbox/nadako/z3c.ownership/trunk/setup.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/__init__.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/__init__.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/__init__.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1 @@
+__import__('pkg_resources').declare_namespace(__name__)
\ No newline at end of file


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/__init__.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/README.txt
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/README.txt	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/README.txt	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,212 @@
+=========
+Ownership
+=========
+
+This package provides simple ownership support for objects that has an
+IPrincipalRoleManager adapter, and that means almost any persistent object
+when using zope.securitypolicy.
+
+
+IOwnership interface
+--------------------
+
+First, let's create example content class. Note, that it must implement
+IOwnerAware class, and for zope.securitypolicy adapters, it also must implement
+IAnnotatable.
+
+  >>> from zope.annotation.interfaces import IAttributeAnnotatable
+  >>> from zope.interface import implements, Interface
+  >>> from z3c.ownership.interfaces import IOwnerAware
+  
+  >>> class IContent(Interface):
+  ...     pass
+  
+  >>> class Content(object):
+  ...
+  ...     implements(IContent, IAttributeAnnotatable, IOwnerAware)
+  
+  >>> content = Content()
+
+The object can now be adapted to IOwnership interface that provides the "owner"
+attribute.
+
+  >>> from z3c.ownership.interfaces import IOwnership
+  >>> ownership = IOwnership(content)
+
+Default owner is unset, so it's None
+
+  >>> ownership.owner is None
+  True
+
+It uses the IAuthentication utility for looking up principals. For this test,
+we have simple principalRegistry registered as an utility. Let's use it to
+define two principals
+
+  >>> dan = authentication.definePrincipal('dan', 'Dan', login='dan')
+  >>> bob = authentication.definePrincipal('bob', 'Bob', login='bob')
+
+Now, let's make dan an owner of our content object
+
+  >>> ownership.owner = dan
+
+Let's check it out
+  
+  >>> ownership = IOwnership(content)
+  >>> ownership.owner is dan
+  True
+
+When principal owns an object, it has the ``z3c.ownership.Owner`` role on that
+object. The name of the role is also available as `OWNER_ROLE` constant from
+``z3c.ownership.interfaces``.
+
+  >>> from z3c.ownership.interfaces import OWNER_ROLE
+  >>> OWNER_ROLE == 'z3c.ownership.Owner'
+  True
+
+Let's check if dan has a "z3c.ownership.Owner" role for this object
+
+  >>> from zope.securitypolicy.interfaces import IPrincipalRoleMap
+  >>> rolemap = IPrincipalRoleMap(content)
+
+  >>> rolemap.getPrincipalsAndRoles()
+  [('z3c.ownership.Owner', 'dan', PermissionSetting: Allow)]
+
+Now, let's change the owner. Note, that when changing an owner, the
+OwnerChangedEvent is fired, let's check it out.
+
+  >>> def printEvent(object, event):
+  ...     old = (event.oldOwner is not None) and event.oldOwner.id or None
+  ...     new = (event.newOwner is not None) and event.newOwner.id or None
+  ...     print old, '->', new
+
+  >>> from z3c.ownership.interfaces import IOwnerChangedEvent
+  >>> from zope.component import provideHandler
+  >>> provideHandler(printEvent, adapts=(IContent, IOwnerChangedEvent))
+
+First, let's check changing owners to None (to remove ownership at all)
+
+  >>> ownership.owner = None
+  dan -> None
+
+  >>> rolemap.getPrincipalsAndRoles()
+  []
+
+Let's set owner to bob
+
+  >>> ownership.owner = bob
+  None -> bob
+
+  >>> rolemap.getPrincipalsAndRoles()
+  [('z3c.ownership.Owner', 'bob', PermissionSetting: Allow)]
+
+Now, let's change owner back to dan.
+
+  >>> ownership.owner = dan
+  bob -> dan
+
+  >>> rolemap.getPrincipalsAndRoles()
+  [('z3c.ownership.Owner', 'dan', PermissionSetting: Allow)]
+
+Note, that we can't use non IPrincipal object as an owner.
+
+  >>> ownership.owner = object
+  Traceback (most recent call last):
+  ...
+  ValueError: IPrincipal object or None required
+
+If we'll try to set owner to same principal as it were, no event will be fired.
+
+  >>> ownership.owner = dan
+
+(note, that "dan -> dan" wasn't printed on this assignment)
+
+
+Breaking ownership
+------------------
+
+There is possibility to break the system, if we assign z3c.ownership.Owner role
+by hand, because object can only have one owner, so don't do it at all :)
+
+  >>> rolemap.assignRoleToPrincipal('z3c.ownership.Owner', bob.id)
+
+It will fail on getting the owner
+
+  >>> ownership.owner
+  Traceback (most recent call last):
+  ...
+  RuntimeError: Object has multiple owners. This should not happen
+  
+And on setting
+
+  >>> ownership.owner = dan
+  Traceback (most recent call last):
+  ...
+  RuntimeError: Object has multiple owners. This should not happen
+
+If we remove all z3c.ownership.Owner roles by hand, the owner will be None
+
+  >>> rolemap.unsetRoleForPrincipal('z3c.ownership.Owner', dan.id)
+  >>> rolemap.unsetRoleForPrincipal('z3c.ownership.Owner', bob.id)
+  >>> ownership.owner is None
+  True
+
+
+Ownership subscriber
+--------------------
+
+This package also provides a subscriber for IObjectAddedEvent that sets
+current interaction principal as object owner
+
+Let's create new interaction and participation
+
+  >>> from zope.security.management import endInteraction, newInteraction
+
+  >>> class Participation(object):
+  ...     interaction = None
+
+  >>> participation = Participation()
+  >>> participation.principal = bob
+
+  >>> endInteraction()
+  >>> newInteraction(participation)
+
+Now, let's create object and notify system with ObjectCreatedEvent
+
+  >>> from zope.event import notify
+  >>> from zope.lifecycleevent import ObjectCreatedEvent
+  
+  >>> content2 = Content()
+  >>> notify(ObjectCreatedEvent(content2))
+  None -> bob
+
+Note that we also catched the OwnerChangedEvent with the handler we
+registered above.
+
+Let's check if our object has an owner
+
+  >>> IOwnership(content2).owner is bob
+  True
+
+If there's no participation active or principal can't be get for some reason,
+owner won't be set.
+
+  >>> endInteraction()
+  >>> content3 = Content()
+  >>> notify(ObjectCreatedEvent(content3))
+  >>> IOwnership(content3).owner is None
+  True
+
+
+Unexistant principals
+---------------------
+
+If principal that owns the object doesn't exist anymore, the ``owner`` attribute
+will be None.
+
+  >>> content4 = Content()
+  >>> IOwnership(content4).owner = dan
+  None -> dan
+
+  >>> authentication._clear()
+  >>> IOwnership(content4).owner is None
+  True


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/README.txt
___________________________________________________________________
Added: svn:eol-style
   + native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/__init__.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/__init__.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/__init__.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1 @@
+# i am a package
\ No newline at end of file


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/__init__.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/configure.zcml
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/configure.zcml	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/configure.zcml	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,29 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    i18n_domain="z3c.ownership"
+    >
+
+  <role
+      id="z3c.ownership.Owner"
+      title="Owner"
+      description="Principal that owns the context object"
+      />
+
+  <permission
+      id="z3c.ownership.ChangeOwner"
+      title="Change object owner"
+      />
+
+  <class class=".ownership.Ownership">
+    <allow attributes="owner" />
+    <require
+        permission="z3c.ownership.ChangeOwner"
+        set_attributes="owner"
+        />
+  </class>
+
+  <adapter factory=".ownership.Ownership" />
+
+  <subscriber handler=".subscriber.setOwner" />
+
+</configure>


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/configure.zcml
___________________________________________________________________
Added: svn:eol-style
   + native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/interfaces.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/interfaces.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/interfaces.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,64 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""Ownership-related interfaces
+
+$Id$
+"""
+from zope.component.interfaces import IObjectEvent, ObjectEvent
+from zope.i18nmessageid import MessageFactory
+from zope.interface import Interface, implements
+from zope.schema import Object
+from zope.security.interfaces import IPrincipal
+
+_ = MessageFactory('z3c.ownership')
+
+OWNER_ROLE = 'z3c.ownership.Owner'
+
+class IOwnerAware(Interface):
+    """Marker interface for objects that supports ownership"""
+
+class IOwnership(Interface):
+    """Objects that support ownership provide this interface"""
+
+    owner = Object(
+        title=_(u'Owner'),
+        description=_(u'Principal that owns this object'),
+        schema=IPrincipal,
+        required=False,
+        )    
+
+class IOwnerChangedEvent(IObjectEvent):
+    """Event that is fired when owner changes"""
+    
+    oldOwner = Object(
+        title=_(u'Old owner'),
+        schema=IPrincipal,
+        required=False
+        )
+
+    newOwner = Object(
+        title=_(u'New owner'),
+        schema=IPrincipal,
+        required=False
+        )
+
+class OwnerChangedEvent(ObjectEvent):
+    """Event that is fired when owner changes"""
+    
+    implements(IOwnerChangedEvent)
+    
+    def __init__(self, object, newOwner, oldOwner):
+        self.object = object
+        self.newOwner = newOwner
+        self.oldOwner = oldOwner


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/interfaces.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/ownership.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/ownership.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/ownership.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,84 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""Ownership implementation
+
+$Id$
+"""
+from zope.app.security.interfaces import IAuthentication, PrincipalLookupError
+from zope.component import adapts, getUtility
+from zope.event import notify
+from zope.interface import implements
+from zope.security.interfaces import IPrincipal
+from zope.securitypolicy.interfaces import IPrincipalRoleMap, IPrincipalRoleManager, Allow
+from zope.proxy import sameProxiedObjects
+
+from z3c.ownership.interfaces import IOwnerAware, IOwnership, OwnerChangedEvent
+from z3c.ownership.interfaces import OWNER_ROLE
+
+class Ownership(object):
+    
+    adapts(IOwnerAware)
+    implements(IOwnership)
+    
+    def __init__(self, context):
+        self._rolemanager = IPrincipalRoleManager(context)
+        self.context = context
+
+    def _getCurrentOwnerId(self):
+        settings = self._rolemanager.getPrincipalsForRole(OWNER_ROLE)
+        principals = [principal_id for (principal_id, setting) in settings if sameProxiedObjects(setting, Allow)]
+        if not principals:
+            return None
+        if len(principals) > 1:
+            raise RuntimeError('Object has multiple owners. This should not happen')
+        return principals[0]
+        
+    @apply
+    def owner():
+
+        def fget(self):
+            principal_id = self._getCurrentOwnerId()
+            if principal_id is None:
+                return None
+            try:
+                return getUtility(IAuthentication).getPrincipal(principal_id)
+            except PrincipalLookupError:
+                return None
+        
+        def fset(self, new_owner):
+            if not (new_owner is None or IPrincipal.providedBy(new_owner)):
+                raise ValueError('IPrincipal object or None required')
+
+            if new_owner is None:
+                new_owner_id = None
+            else:
+                new_owner_id = new_owner.id
+
+            current_owner_id = self._getCurrentOwnerId()
+
+            if new_owner_id == current_owner_id:
+                return
+
+            if current_owner_id is not None:
+                self._rolemanager.unsetRoleForPrincipal(OWNER_ROLE, current_owner_id)
+                current_owner = getUtility(IAuthentication).getPrincipal(current_owner_id)
+            else:
+                current_owner = None
+
+            if new_owner_id:
+                self._rolemanager.assignRoleToPrincipal(OWNER_ROLE, new_owner_id)
+
+            notify(OwnerChangedEvent(self.context, new_owner, current_owner))
+            
+        return property(fget, fset)


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/ownership.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/subscriber.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/subscriber.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/subscriber.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,36 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""Subscriber that sets current principal as object's owner on object creation
+
+$Id$
+"""
+from zope.component import adapter
+from zope.lifecycleevent.interfaces import IObjectCreatedEvent
+from zope.security.management import queryInteraction
+
+from z3c.ownership.interfaces import IOwnership, IOwnerAware
+
+def getCurrentPrincipal():
+    interaction = queryInteraction()
+    if interaction is not None:
+        for participation in interaction.participations:
+            if participation.principal is not None:
+                return participation.principal
+    return None
+
+ at adapter(IOwnerAware, IObjectCreatedEvent)
+def setOwner(object, event):
+    principal = getCurrentPrincipal()
+    if principal is not None:
+        IOwnership(object).owner = principal


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/subscriber.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native

Added: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/tests.py
===================================================================
--- Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/tests.py	                        (rev 0)
+++ Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/tests.py	2009-03-10 17:09:40 UTC (rev 97812)
@@ -0,0 +1,50 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""Tests z3c.ownership package
+
+$Id$
+"""
+import unittest
+from zope.testing import doctest, cleanup
+from zope.component import eventtesting
+
+from zope.annotation.interfaces import IAnnotatable
+from zope.annotation.attribute import AttributeAnnotations
+from zope.app.security.interfaces import IAuthentication
+from zope.app.security.principalregistry import principalRegistry
+from zope.component import provideAdapter, provideUtility, provideHandler
+from zope.securitypolicy.principalrole import AnnotationPrincipalRoleManager
+
+from z3c.ownership.ownership import Ownership
+from z3c.ownership.subscriber import setOwner
+
+def setUp(test):
+    cleanup.setUp()
+    eventtesting.setUp()
+    provideAdapter(AttributeAnnotations)
+    provideAdapter(AnnotationPrincipalRoleManager, adapts=(IAnnotatable, ))
+    provideAdapter(Ownership)
+    provideHandler(setOwner)
+    provideUtility(principalRegistry, IAuthentication)
+    test.globs = {'authentication': principalRegistry}
+
+def tearDown(test):
+    cleanup.tearDown()
+
+def test_suite():
+    return unittest.TestSuite(
+        doctest.DocFileSuite(
+            'README.txt', setUp=setUp, tearDown=tearDown,
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS)
+        )


Property changes on: Sandbox/nadako/z3c.ownership/trunk/src/z3c/ownership/tests.py
___________________________________________________________________
Added: svn:keywords
   + Id,svn:eol-style=native



More information about the Checkins mailing list