Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

User Home Folders for Zope 3 Applications

Project Description

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 Dcoumentation

Principal Home Folder

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.

Managing the Home Folders

Let’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 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 = baseFolder

Now 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']

Since the home folder did not exist and the createHomeFolder option was turned on, the directory was created for you:

>>> 'stephan' in baseFolder

When creating the home folder, the principal also automatically receives the zope.Manager role:

>>> 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 import Folder
>>> baseFolder['sc3'] = Folder()
>>> manager.assignHomeFolder('sc3')
>>> manager.assignments['sc3']

This has the advantage that you can choose your own IContainer implementation instead of relying on the vanilla folder.

Now let’s look at some derivations of the same task.

  1. Sometimes you want to specify an alternative folder name:
>>> manager.assignHomeFolder('jim', folderName='J1m')
>>> manager.assignments['jim']
>>> 'J1m' in baseFolder
  1. Even 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' in baseFolder
  1. You wish not to create a folder by default:
>>> manager.createHomeFolder = False
>>> manager.assignHomeFolder('philiKON')
>>> manager.assignments['philiKON']
>>> 'philiKON' in baseFolder
  1. You 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' in baseFolder

Let’s now look at removing home folder assignments. By default, removing an assignment will not delete the actual folder:

>>> manager.unassignHomeFolder('stevea')
>>> 'stevea' not in manager.assignments
>>> 'stevea' in baseFolder

But if you specify the delete argument, then the folder will be deleted:

>>> 'J1m' in baseFolder
>>> manager.unassignHomeFolder('jim', delete=True)
>>> 'jim' not in manager.assignments
>>> 'J1m' in baseFolder

Next, 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']

If you try to get a folder and it does not yet exist, None will be returned if autoCreateAssignment is False. Remember ‘dreamcatcher’, which has an assignment, but not a folder:

>>> 'dreamcatcher' in baseFolder
>>> homeFolder = manager.getHomeFolder('dreamcatcher')
>>> homeFolder is None

However, 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
>>> 'florian' in baseFolder

Sometimes you want to create a homefolder which is not a You can change the object type that is being created by changing the containerObject property. It defaults to ‘’. Let’s create a homefile.

>>> manager.containerObject = ''
>>> manager.assignHomeFolder('fileuser', create=True)
>>> homeFolder = manager.getHomeFolder('fileuser')
>>> print homeFolder #doctest: +ELLIPSIS
< 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 Folder

But 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 a homeFolder attribute. The second method provides the folder via a path adapter called homefolder.

Let’s start by creating a principal:

>>> from import IPrincipal
>>> from zope.interface import implements
>>> class Principal:
...     implements(IPrincipal)
...     def __init__(self, id):
... = id
>>> principal = Principal('stephan')

We also need to register our manager as a utility:

>>> from import ztapi
>>> from import IHomeFolderManager
>>> ztapi.provideUtility(IHomeFolderManager, manager, 'manager')
  1. Now we can access the home folder via the adapter:
>>> from import IHomeFolder
>>> adapter = IHomeFolder(principal)
>>> adapter.homeFolder is baseFolder['stephan']
  1. Or alternatively via the path adapter:
>>> import zope.component
>>> from zope.traversing.interfaces import IPathAdapter
>>> zope.component.getAdapter(principal, IPathAdapter,
...                           "homefolder") is baseFolder['stephan']

As 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 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))


3.5.0 (2009-02-01)

  • Use zope.container instead of
  • Remove dependency on

3.4.0 (2007-11-03)

  • Initial release independent of the main Zope tree.
Release History

Release History

This version
History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date (12.3 kB) Copy SHA256 Checksum SHA256 Source Feb 1, 2009

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting