[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security - RoleRegistry.py:1.1.2.1

Barry Warsaw barry@wooz.org
Wed, 12 Dec 2001 17:30:16 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security
In directory cvs.zope.org:/tmp/cvs-serv14781/lib/python/Zope/App/Security

Added Files:
      Tag: Zope-3x-branch
	RoleRegistry.py 
Log Message:
The global role registry


=== Added File Zope3/lib/python/Zope/App/Security/RoleRegistry.py ===
# RoleRegistry.py
#
# Copyright (c) 2001 Zope Coporation and Contributors.  All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
# FOR A PARTICULAR PURPOSE.

""" Global role registry."""

from IRole import IRole

class Role:
    __implements__ = IRole

    def __init__(self, title, description):
        self._title = title
        self._description = description

    def getTitle(self):
        return self._title

    def getDescription(self):
        return self._description


# Key is string naming a role, value is a Role object which implements a IRole
# interface.
_roles={}


def defineRole(name, title=None, description=None):
    """Define a new role object, register, and return it.

    name is the role name, must be globally unique

    title (optional) is the role title, human readable.  If omitted then
    the name is used as the title

    description (optional) is human readable
    """
    _roles[name] = role = Role(title or name, description or '')
    return role
 
def definedRole(name):
    """Return true if named role is registered, otherwise return false
    """
    return _roles.has_key(name)

_missing = []
def getRole(name, default=_missing):
    """Return role object registered as name.

    If no named role is registered, return optional default.  If default
    is not given, then KeyError is raised.
    """
    ret = _roles.get(name, default)
    if ret is _missing:
        raise KeyError('No such role: %s' % name)
    return ret

def _clear(): # Reset, e.g., for unit testing antisepsis
    _roles.clear()