Back to index

plone3  3.1.7
factory_adapter.py
Go to the documentation of this file.
00001 from zope.component import adapts
00002 from zope.interface import implements
00003 from borg.localrole.interfaces import IFactoryTempFolder
00004 from borg.localrole.interfaces import ILocalRoleProvider
00005 from Acquisition import aq_inner, aq_parent
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 class FactoryTempFolderProvider(object):
00009     """A simple local role provider which just gathers the roles from
00010     the desired context::
00011 
00012         >>> from zope.app.testing import placelesssetup
00013         >>> placelesssetup.setUp()
00014         >>> from zope.component import provideAdapter
00015         >>> from zope.interface import Interface, implements, directlyProvides
00016         >>> from borg.localrole.workspace import WorkspaceLocalRoleManager
00017         >>> rm = WorkspaceLocalRoleManager('rm', 'A Role Manager')
00018 
00019         >>> from Acquisition import Implicit
00020         >>> class DummyObject(Implicit):
00021         ...     implements(Interface)
00022         >>> root = DummyObject()
00023 
00024 
00025     Let's construct a hierarchy similar to the way portal factory is used::
00026 
00027         root --> folder -------|
00028           |------------> PortalFactory --> TempFolder --> NewObject 
00029 
00030         >>> fold = DummyObject().__of__(root)
00031         >>> factory = DummyObject().__of__(root)
00032         >>> wrapped_factory = factory.__of__(fold)
00033         >>> temp = DummyObject().__of__(wrapped_factory)
00034         >>> newob = DummyObject().__of__(temp)
00035 
00036         >>> from borg.localrole.tests import SimpleLocalRoleProvider
00037         >>> from borg.localrole.tests import DummyUser
00038         >>> user1 = DummyUser('bogus_user1')
00039 
00040 
00041     To test our adapter we need an acl_users, and our user needs a
00042     getRolesInContext method::
00043 
00044         >>> class FakeUF(object):
00045         ...     def getUserById(self, userid, default=None):
00046         ...         if userid == user1.getId():
00047         ...             return user1
00048         ...         return None
00049         >>> root.acl_users = FakeUF()
00050 
00051         >>> def getRolesInContext(user, context):
00052         ...     return rm.getRolesInContext(user, context)
00053         >>> from new import instancemethod
00054         >>> user1.getRolesInContext = instancemethod(getRolesInContext, user1,
00055         ...                                          DummyUser)
00056 
00057 
00058     We add special interface to our Folder which allows us to provide
00059     some local roles, these roles will be inherited by any contained
00060     objects but not by our 'newob' because though the folder is its
00061     acquisition chain it is not contained by it::
00062 
00063         >>> class ISpecialInterface(Interface):
00064         ...     pass
00065         >>> directlyProvides(fold, ISpecialInterface)
00066         >>> provideAdapter(SimpleLocalRoleProvider, adapts=(ISpecialInterface,))
00067         >>> rm.getRolesInContext(user1, fold)
00068         ['Foo']
00069         >>> contained = DummyObject().__of__(fold)
00070         >>> rm.getRolesInContext(user1, contained)
00071         ['Foo']
00072         >>> rm.getRolesInContext(user1, newob)
00073         []
00074 
00075     Now we mark our TempFolder, and check that roles are now inherited
00076     from the intended location ('fold')::
00077 
00078         >>> directlyProvides(temp, IFactoryTempFolder)
00079         >>> provideAdapter(FactoryTempFolderProvider)
00080         >>> rm.getRolesInContext(user1, newob)
00081         ['Foo']
00082 
00083     The getAllRoles method always returns an empty dict, becuas it is
00084     only used for thing which are irrelevant for temporary objects::
00085 
00086         >>> rm.getAllLocalRolesInContext(newob)
00087         {}
00088 
00089 
00090         >>> placelesssetup.tearDown()
00091 
00092     """
00093     adapts(IFactoryTempFolder)
00094     implements(ILocalRoleProvider)
00095 
00096     def __init__(self, obj):
00097         self.folder = obj
00098 
00099     def getRoles(self, principal_id):
00100         uf = aq_inner(getToolByName(self.folder, 'acl_users'))
00101         user = aq_inner(uf.getUserById(principal_id, default=None))
00102         # use the folder we are creating in as role generating context
00103         source = aq_parent(aq_parent(self.folder))
00104         if user is not None:
00105             return user.getRolesInContext(source)
00106         else:
00107             return []
00108 
00109     def getAllRoles(self):
00110         # This should not be used in any meaningful way, so we'll make it cheap
00111         return {}