Package: zope.app.homefolder
The principal home folder subscriber lets you assign home folders to
principals as you would do in any OS. This particular implementation of such a
feature is intended as a demo of the power of the way to handle principals
and not as the holy grail. If you have concerns about the assumptions made in
this implementation (which are probably legitimate), just ignore this package
altogether.Detailed DcoumentationPrincipal Home FolderThe principal home folder subscriber lets you assign home folders to
principals as you would do in any OS. This particular implementation of such a
feature is intended as a demo of the power of the way to handle principals
and not as the holy grail. If you have concerns about the assumptions made in
this implementation (which are probably legitimate), just ignore this package
altogether.Managing the Home FoldersLet’s say we have a principal and we want to have a home folder for it. The
first task is to create the home folder manager, which keeps track of the
principal’s home folders:>>> from zope.app.homefolder.homefolder import HomeFolderManager
>>> manager = HomeFolderManager()Now the manager will not be able to do much, because it does not know where to
look for the principal home folders. Therefore we have to specify a folder
container:>>> from zope.container.btree import BTreeContainer
>>> baseFolder = BTreeContainer()
>>> manager.homeFolderBase = baseFolderNow we can assign a home folder to a principal:>>> manager.assignHomeFolder('stephan')Since we did not specify the name of the home folder, it is just the same
as the principal id:>>> manager.assignments['stephan']
'stephan'Since the home folder did not exist and thecreateHomeFolderoption was
turned on, the directory was created for you:>>> 'stephan' in baseFolder
TrueWhen creating the home folder, the principal also automatically receives thezope.Managerrole:>>> from zope.securitypolicy.interfaces import IPrincipalRoleManager
>>> roles = IPrincipalRoleManager(baseFolder['stephan'])
>>> [(role, str(setting))
...  for role, setting in roles.getRolesForPrincipal('stephan')]
[(u'zope.Manager', 'PermissionSetting: Allow')]If a folder already exists for the provided name, then the creation is simply
skipped silently:>>> from zope.app.folder import Folder
>>> baseFolder['sc3'] = Folder()
>>> manager.assignHomeFolder('sc3')
>>> manager.assignments['sc3']
'sc3'This has the advantage that you can choose your ownIContainerimplementation instead of relying on the vanilla folder.Now let’s look at some derivations of the same task.Sometimes you want to specify an alternative folder name:>>> manager.assignHomeFolder('jim', folderName='J1m')
>>> manager.assignments['jim']
'J1m'
>>> 'J1m' in baseFolder
TrueEven though folders are created by default, you can specifically turn
that behavior off for a particular assignment:>>> manager.assignHomeFolder('dreamcatcher', create=False)
>>> manager.assignments['dreamcatcher']
'dreamcatcher'
>>> 'dreamcatcher' in baseFolder
FalseYou wish not to create a folder by default:>>> manager.createHomeFolder = False
>>> manager.assignHomeFolder('philiKON')
>>> manager.assignments['philiKON']
'philiKON'
>>> 'philiKON' in baseFolder
FalseYou do not want to create a folder by default, you want to create the
folder for a specific user:>>> manager.assignHomeFolder('stevea', create=True)
>>> manager.assignments['stevea']
'stevea'
>>> 'stevea' in baseFolder
TrueLet’s now look at removing home folder assignments. By default, removing an
assignment willnotdelete the actual folder:>>> manager.unassignHomeFolder('stevea')
>>> 'stevea' not in manager.assignments
True
>>> 'stevea' in baseFolder
TrueBut if you specify thedeleteargument, then the folder will be deleted:>>> 'J1m' in baseFolder
True
>>> manager.unassignHomeFolder('jim', delete=True)
>>> 'jim' not in manager.assignments
True
>>> 'J1m' in baseFolder
FalseNext, let’s have a look at retrieving the home folder for a principal. This
can be done as follows:>>> homeFolder = manager.getHomeFolder('stephan')
>>> homeFolder is baseFolder['stephan']
TrueIf you try to get a folder and it does not yet exist,Nonewill be
returned if autoCreateAssignment is False. Remember ‘dreamcatcher’, which
has an assignment, but not a folder:>>> 'dreamcatcher' in baseFolder
False
>>> homeFolder = manager.getHomeFolder('dreamcatcher')
>>> homeFolder is None
TrueHowever, if autoCreateAssignment is True and you try to get a home folder
of a principal which has no assignment, the assignment and the folder
will be automatically created. The folder will always be created, regardless
of the value of createHomeFolder. The name of the folder will be identically
to the principalId:>>> manager.autoCreateAssignment = True
>>> homeFolder = manager.getHomeFolder('florian')
>>> 'florian' in manager.assignments
True
>>> 'florian' in baseFolder
TrueSometimes you want to create a homefolder which is not a zope.app.Folder.
You can change the object type that is being created by changing the
containerObject property. It defaults to ‘zope.app.folder.Folder’.
Let’s create a homefile.>>> manager.containerObject = 'zope.app.file.File'
>>> manager.assignHomeFolder('fileuser', create=True)
>>> homeFolder = manager.getHomeFolder('fileuser')
>>> print homeFolder #doctest: +ELLIPSIS
<zope.app.file.file.File object at ...>You see that now a File object has been created. We reset containerObject
to zope,folder.Folder to not confuse the follow tests.>>> manager.containerObject = 'zope.folder.Folder'Accessing the Home FolderBut how does the home folder get assigned to a principal? There are two ways
of accessing the homefolder. The first is via a simple adapter that provides ahomeFolderattribute. The second method provides the folder via a path
adapter calledhomefolder.Let’s start by creating a principal:>>> from zope.security.interfaces import IPrincipal
>>> from zope.interface import implements
>>> class Principal:
...     implements(IPrincipal)
...     def __init__(self, id):
...         self.id = id
>>> principal = Principal('stephan')We also need to register our manager as a utility:>>> from zope.app.testing import ztapi
>>> from zope.app.homefolder.interfaces import IHomeFolderManager
>>> ztapi.provideUtility(IHomeFolderManager, manager, 'manager')Now we can access the home folder via the adapter:>>> from zope.app.homefolder.interfaces import IHomeFolder
>>> adapter = IHomeFolder(principal)
>>> adapter.homeFolder is baseFolder['stephan']
TrueOr alternatively via the path adapter:>>> import zope.component
>>> from zope.traversing.interfaces import IPathAdapter
>>> zope.component.getAdapter(principal, IPathAdapter,
...                           "homefolder") is baseFolder['stephan']
TrueAs you can see, the path adapter just returns the homefolder. This way we can
guarantee that the folder’s full API is always available. Of course the real
way it will be used is via a TALES expression:Setup of the Engine>>> from zope.app.pagetemplate.engine import Engine
>>> from zope.tales.tales import Context
>>> context = Context(Engine, {'principal': principal})Executing the TALES expression>>> bytecode = Engine.compile('principal/homefolder:keys')
>>> list(bytecode(context))
[]
>>> baseFolder['stephan'][u'documents'] = Folder()
>>> list(bytecode(context))
[u'documents']CHANGES3.5.0 (2009-02-01)Usezope.containerinstead ofzope.app.container.Remove dependency onzope.app.zapi.3.4.0 (2007-11-03)Initial release independent of the main Zope tree.