Zope Security Framework

8.3 · active · verified Thu Apr 16

Zope security provides a generic mechanism to implement security policies on Python objects. It is part of the larger Zope ecosystem and defines roles, permissions, and security checks. As of April 2026, the current version is 8.3, and it follows the Zope release cadence with major releases typically every 2-6 months for actively supported versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define and check permissions using `zope.security` (via `AccessControl`). It sets up a basic secured object with a custom permission and then simulates two different user interactions: one with the required permission and one without. It uses `ClassSecurityInfo` for declarative security and `getSecurityManager().checkPermission()` for programmatic checks. Note that in a full Zope application, the interaction and permission checks are often handled implicitly by the Zope Publisher.

import os
from AccessControl.SecurityInfo import ClassSecurityInfo, ACCESS_PUBLIC
from AccessControl.SecurityManagement import setSecurityManager, getSecurityManager, noSecurityManager
from AccessControl.users import UnrestrictedUser, nobody

# Define a simple permission
VIEW_FOO_PERMISSION = 'View Foo'

# A dummy object to secure
class MySecuredObject:
    security = ClassSecurityInfo()
    security.declareObjectPublic() # Allow access to the object itself
    security.declareProtected(VIEW_FOO_PERMISSION, 'foo_data')

    def __init__(self, data):
        self._data = data

    def foo_data(self):
        return self._data

# --- Example Usage ---

# 1. Setup a security manager (usually done by Zope)
# For standalone testing, we can simulate an interaction

# Create a principal (user)
class TestUser(UnrestrictedUser):
    def __init__(self, id, roles=()):
        super().__init__(id, '', roles, '')
        self._roles = roles

    def getRoles(self):
        return self._roles

# User with permission
user_with_permission = TestUser('editor', roles=('Manager', VIEW_FOO_PERMISSION))

# User without permission
user_without_permission = TestUser('viewer', roles=())

# Create a secured object
obj = MySecuredObject('Secret Foo Content')

print(f"Object data: {obj.foo_data()}") # Access from unrestricted context is fine

try:
    # Simulate an interaction for user_with_permission
    setSecurityManager(user_with_permission)
    sm = getSecurityManager()
    print(f"User '{sm.getUser().getId()}' has permission '{VIEW_FOO_PERMISSION}': {sm.checkPermission(VIEW_FOO_PERMISSION, obj)}")
    # In a real Zope context, calling obj.foo_data() would be checked here
except Exception as e:
    print(f"Error with user_with_permission: {e}")
finally:
    noSecurityManager() # Clean up

print("---------------------")

try:
    # Simulate an interaction for user_without_permission
    setSecurityManager(user_without_permission)
    sm = getSecurityManager()
    print(f"User '{sm.getUser().getId()}' has permission '{VIEW_FOO_PERMISSION}': {sm.checkPermission(VIEW_FOO_PERMISSION, obj)}")
    # Attempting to access obj.foo_data() here would raise Unauthorized in a Zope context
except Exception as e:
    # In a real Zope context, this would likely be an IUnauthorized or similar
    print(f"Expected error for user_without_permission (no permission): {e}")
finally:
    noSecurityManager() # Clean up

view raw JSON →