Ignore:
Timestamp:
15 Jun 2011, 23:19:58 (13 years ago)
Author:
uli
Message:

Add method to get principal without request (use only in case of
emergency!).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/helpers.py

    r6113 r6372  
    1212from zope.schema import getFieldNames
    1313from zope.schema.fieldproperty import FieldProperty
     14from zope.security.interfaces import NoInteraction
     15from zope.security.management import getInteraction
     16
    1417
    1518def removeFileOrDirectory(filepath):
     
    363366        setattr(cls, field_name, FieldProperty(iface[field_name]))
    364367    return cls
     368
     369def get_current_principal():
     370    """Get the 'current' principal.
     371
     372    This method works without a request. Examining a request is the
     373    regular (and recommended) way to get a principal involved
     374    'currently'.
     375
     376    Use this method only if you really have no access to the current
     377    request.
     378
     379    Returns ``None`` when no principal is involved (for instance
     380    during tests).
     381    """
     382    try:
     383        principal = getInteraction().participations[0].principal
     384    except NoInteraction:
     385        return None
     386    except IndexError: # No participations present
     387        return None
     388    return principal
Note: See TracChangeset for help on using the changeset viewer.