quick search:

Find out about the current path in an Access Rule

Submitted by: slinkp
Last Edited: 2003-12-16

Category: CMF

Average rating is: 0.0 out of 5 (0 ratings)

You want to find out something about the current path and
do something with it, using an Access Rule.

Source (Text):
# Assume we're in a CMF site and want to find out the
# id of the topmost Portal Folder in the current path.
# (might be an empty string if we're in the portal root)

utool = container.portal_url
rel_path = request.get('PATH_INFO', None)
if rel_path:
    base_portal_path = utool.getPortalPath()
    # this should work with VHM                                                 
    split_path = rel_path.split(base_portal_path, 1)
    # First element of that tuple is a prefix - just stuff we
    # don't want.
    if len(split_path) == 1:
        # failed to get a prefix. This happens sometimes, 
        # not sure when.
        rel_object_path = split_path[0]
        prefix, rel_object_path =  split_path
    rel_object_path_elements = rel_object_path.split('/')
    # We're only interested in the first 2 elements.                                
    # First is always blank, 2nd (if there is one) is the 
    # subfolder id.
    result = rel_object_path_elements[:2][-1]
    # For some reason we didn't get PATH_INFO, 
    # so fall back to the context's path.
    # I don't think this would actually work... maybe this
    # never happens ;-)
    result = ''.join(utool.getRelativeContentPath(context)[0:1])

# ... now you have your result, do something with it!

The trouble is that Access Rules are called too early
for some of the usual techniques to behave as expected.
For example, "context" does not seem to be set up yet when
the Access Rule is triggered; it gives you the script's container
instead of the object you're traversing.
So I can't find out where I am by doing context.getPhysicalPath()
or the like. So, we have to extract the path information from the
request. PATH_INFO works for that.

You can apply this technique to many situations. In my case I wanted
to toggle the cmf skin automatically based on a combination of the
virtual hostname and the current topmost Portal Folder.
I didn't want to have a separate access rule for each folder so I came
up with this one script that would work in all subfolders.
I use the above recipe to determine the folder, then use the "Bind
skin on folder traversal" recipe
(http://www.zopelabs.com/cookbook/1016693454 - read all the comments,
the original recipe is buggy). I leave it to you to fill in
the blanks.


missing assignment to request by gerrykirk - 2003-12-16
add the line
request = container.REQUEST

after the first line.