summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2009-09-06 03:58:46 +0000
committerChris McDonough <chrism@agendaless.com>2009-09-06 03:58:46 +0000
commit573184904e48fa5eb9c04e7d3d321183b3e96f6d (patch)
tree912d394af04622e9667da31b76ea9276878d700f
parentd66bfb5d1f1aef5cce4941b49740dbd136c95605 (diff)
downloadpyramid-573184904e48fa5eb9c04e7d3d321183b3e96f6d.tar.gz
pyramid-573184904e48fa5eb9c04e7d3d321183b3e96f6d.tar.bz2
pyramid-573184904e48fa5eb9c04e7d3d321183b3e96f6d.zip
Remove 0.9 deprecations.
-rw-r--r--CHANGES.txt53
-rw-r--r--repoze/bfg/interfaces.py40
-rw-r--r--repoze/bfg/secpols.py468
-rw-r--r--repoze/bfg/security.py25
-rw-r--r--repoze/bfg/testing.py4
-rw-r--r--repoze/bfg/tests/test_secpols.py738
-rw-r--r--repoze/bfg/tests/test_zcml.py55
-rw-r--r--repoze/bfg/zcml.py71
8 files changed, 56 insertions, 1398 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 5dd17f4a4..232ba1c27 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,6 +1,59 @@
Next release
============
+- The ``IViewPermissionFactory`` interface has been removed. This was
+ never an API.
+
+- The ``INotFoundAppFactory`` interface has been removed; it has
+ been deprecated since repoze.bfg 0.9. If you have something like
+ the following in your ``configure.zcml``::
+
+ <utility provides="repoze.bfg.interfaces.INotFoundAppFactory"
+ component="helloworld.factories.notfound_app_factory"/>
+
+ Replace it with something like::
+
+ <notfound
+ view="helloworld.views.notfound_view"/>
+
+ See "Changing the Not Found View" in the "Hooks" chapter of the
+ documentation for more information.
+
+- The ``IUnauthorizedAppFactory`` interface has been removed; it has
+ been deprecated since repoze.bfg 0.9. If you have something like
+ the following in your ``configure.zcml``::
+
+ <utility provides="repoze.bfg.interfaces.IUnauthorizedAppFactory"
+ component="helloworld.factories.unauthorized_app_factory"/>
+
+ Replace it with something like::
+
+ <forbidden
+ view="helloworld.views.forbidden_view"/>
+
+ See "Changing the Forbidden View" in the "Hooks" chapter of the
+ documentation for more information.
+
+- ``ISecurityPolicy``-based security policies, deprecated since
+ repoze.bfg 0.9, have been removed. If you have something like this
+ in your ``configure.zcml``, it will no longer work::
+
+ <utility
+ provides="repoze.bfg.interfaces.ISecurityPolicy"
+ factory="repoze.bfg.security.RemoteUserInheritingACLSecurityPolicy"
+ />
+
+ If ZCML like the above exists in your application, you will receive
+ an error at startup time. Instead of the above, you'll need
+ something like::
+
+ <remoteuserauthenticationpolicy/>
+ <aclauthorizationpolicy/>
+
+ This is just an example. See the "Security" chapter of the
+ repoze.bfg documentation for more information about configuring
+ security policies.
+
- The ``route`` ZCML directive now honors ``view_request_method``,
``view_request_param`` and ``view_containment`` attributes, which
pass along these values to the associated view if any is provided.
diff --git a/repoze/bfg/interfaces.py b/repoze/bfg/interfaces.py
index 6a999a98c..827654b1d 100644
--- a/repoze/bfg/interfaces.py
+++ b/repoze/bfg/interfaces.py
@@ -30,7 +30,7 @@ class IView(Interface):
authorization failure is detected during view execution."""
class ISecuredView(IView):
- """ Internal interface. Not an API. """
+ """ *Internal only* interface. Not an API. """
def __call_permissive__(context, request):
""" Guaranteed-permissive version of __call__ """
@@ -90,40 +90,10 @@ class ITemplateRendererFactory(Interface):
def __call__(path, auto_reload=False):
""" Return an object that implements ``ITemplateRenderer`` """
-class ISecurityPolicy(Interface):
- """ A utility that provides a mechanism to check authorization
- using authentication data. This interface was deprecated in
- BFG 0.9; use the combination of IAuthenticationPolicy and
- IAuthorization Policy instead"""
- def permits(context, request, permission):
- """ Returns True if the combination of the authorization
- information in the context and the authentication data in the
- request allow the action implied by the permission"""
-
- def authenticated_userid(request):
- """ Return the userid of the currently authenticated user or
- None if there is no currently authenticated user """
-
- def effective_principals(request):
- """ Return the list of 'effective' principals for the request.
- This must include the userid of the currently authenticated
- user if a user is currently authenticated."""
-
- def principals_allowed_by_permission(context, permission):
- """ Return a sequence of principal identifiers allowed by the
- ``permission`` in the model implied by ``context``. This
- method may not be supported by a given security policy
- implementation, in which case, it should raise a
- ``NotImplementedError`` exception."""
-
class IViewPermission(Interface):
def __call__(context, request):
""" Return True if the permission allows, return False if it denies. """
-class IViewPermissionFactory(Interface):
- def __call__(permission_name):
- """ Returns an IViewPermission """
-
class IRouter(Interface):
"""WSGI application which routes requests to 'view' code based on
a view registry."""
@@ -185,14 +155,6 @@ class INotFoundView(Interface):
a``message`` key in the WSGI environ provides information
pertaining to the reason for the notfound error."""
-class INotFoundAppFactory(Interface):
- """ A utility which returns a NotFound WSGI application factory.
- Deprecated in repoze.bfg 0.9 in favor of INotFoundView"""
-
-class IUnauthorizedAppFactory(Interface):
- """ A utility which returns an Unauthorized WSGI application
- factory. Deprecated in repoze.bfg 0.9 in favor of IForbiddenView"""
-
class IContextURL(Interface):
""" An adapter which deals with URLs related to a context.
"""
diff --git a/repoze/bfg/secpols.py b/repoze/bfg/secpols.py
deleted file mode 100644
index 407988a02..000000000
--- a/repoze/bfg/secpols.py
+++ /dev/null
@@ -1,468 +0,0 @@
-from zope.deprecation import deprecated
-from zope.interface import implements
-
-from repoze.bfg.interfaces import ISecurityPolicy
-from repoze.bfg.interfaces import IAuthorizationPolicy
-from repoze.bfg.interfaces import IAuthenticationPolicy
-
-from repoze.bfg.location import lineage
-from repoze.bfg.threadlocal import get_current_request
-
-from repoze.bfg.security import Allow
-from repoze.bfg.security import Deny
-from repoze.bfg.security import ACLAllowed
-from repoze.bfg.security import ACLDenied
-from repoze.bfg.security import Everyone
-from repoze.bfg.security import Authenticated
-
-class ACLSecurityPolicy(object):
- implements(ISecurityPolicy)
-
- def __init__(self, get_principals):
- self.get_principals = get_principals
-
- def permits(self, context, request, permission):
- """ Return ``ACLAllowed`` if the policy permits access,
- ``ACLDenied`` if not. """
- principals = set(self.effective_principals(request))
-
- for location in lineage(context):
- try:
- acl = location.__acl__
- except AttributeError:
- continue
-
- for ace in acl:
- ace_action, ace_principal, ace_permissions = ace
- if ace_principal in principals:
- if not hasattr(ace_permissions, '__iter__'):
- ace_permissions = [ace_permissions]
- if permission in ace_permissions:
- if ace_action == Allow:
- return ACLAllowed(ace, acl, permission,
- principals, location)
- else:
- return ACLDenied(ace, acl, permission,
- principals, location)
-
- # default deny if no ACE matches in the ACL found
- result = ACLDenied(None, acl, permission, principals, location)
- return result
-
- # default deny if no ACL in lineage at all
- return ACLDenied(None, None, permission, principals, context)
-
- def authenticated_userid(self, request):
- principals = self.get_principals(request)
- if principals:
- return principals[0]
-
- def effective_principals(self, request):
- effective_principals = [Everyone]
- principal_ids = self.get_principals(request)
-
- if principal_ids:
- effective_principals.append(Authenticated)
- effective_principals.extend(principal_ids)
-
- return effective_principals
-
- def principals_allowed_by_permission(self, context, permission):
- for location in lineage(context):
- try:
- acl = location.__acl__
- except AttributeError:
- continue
-
- allowed = {}
-
- for ace_action, ace_principal, ace_permissions in acl:
- if ace_action == Allow:
- if not hasattr(ace_permissions, '__iter__'):
- ace_permissions = [ace_permissions]
- if permission in ace_permissions:
- allowed[ace_principal] = True
- return sorted(allowed.keys())
-
- return []
-
-class InheritingACLSecurityPolicy(object):
- """ A security policy which uses ACLs in the following ways:
-
- - When checking whether a user is permitted (via the ``permits``
- method), the security policy consults the ``context`` for an ACL
- first. If no ACL exists on the context, or one does exist but
- the ACL does not explicitly allow or deny access for any of the
- effective principals, consult the context's parent ACL, and so
- on, until the lineage is exhausted or we determine that the
- policy permits or denies.
-
- During this processing, if any ``Deny`` ACE is found matching
- any effective principal, stop processing by returning an
- ``ACLDenied`` (equals False) immediately. If any ``Allow`` ACE
- is found matching any effective principal, stop processing by
- returning an ``ACLAllowed`` (equals True) immediately. If we
- exhaust the context's lneage, and no ACE has explicitly
- permitted or denied access, return an ``ACLDenied``. This
- differs from the non-inheriting security policy (the
- ``ACLSecurityPolicy``) by virtue of the fact that it does not
- stop looking for ACLs in the object lineage after it finds the
- first one.
-
- - When computing principals allowed by a permission via the
- ``principals_allowed_by_permission`` method, we compute the set
- of principals that are explicitly granted the ``permission``.
- We do this by walking 'up' the object graph *from the root* to
- the context. During this walking process, if we find an
- explicit ``Allow`` ACE for a principal that matches the
- ``permission``, the principal is included in the allow list.
- However, if later in the walking process that user is mentioned
- in any ``Deny`` ACE for the permission, the user is removed from
- the allow list. If a ``Deny`` to the principal ``Everyone`` is
- encountered during the walking process that matches the
- ``permission``, the allow list is cleared for all principals
- encountered in previous ACLs. The walking process ends after
- we've processed the any ACL directly attached to ``context``; a
- list of principals is returned.
-
- - Other aspects of this policy are the same as those in the
- ACLSecurityPolicy (e.g. ``effective_principals``,
- ``authenticated_userid``).
- """
- implements(ISecurityPolicy)
-
- def __init__(self, get_principals):
- self.get_principals = get_principals
-
- def permits(self, context, request, permission):
- """ Return ``ACLAllowed`` if the policy permits access,
- ``ACLDenied`` if not. """
- principals = set(self.effective_principals(request))
-
- for location in lineage(context):
- try:
- acl = location.__acl__
- except AttributeError:
- continue
-
- for ace in acl:
- ace_action, ace_principal, ace_permissions = ace
- if ace_principal in principals:
- if not hasattr(ace_permissions, '__iter__'):
- ace_permissions = [ace_permissions]
- if permission in ace_permissions:
- if ace_action == Allow:
- return ACLAllowed(ace, acl, permission,
- principals, location)
- else:
- return ACLDenied(ace, acl, permission,
- principals, location)
-
- # default deny if no ACL in lineage at all
- return ACLDenied(None, None, permission, principals, context)
-
- def authenticated_userid(self, request):
- principals = self.get_principals(request)
- if principals:
- return principals[0]
-
- def effective_principals(self, request):
- effective_principals = [Everyone]
- principal_ids = self.get_principals(request)
-
- if principal_ids:
- effective_principals.append(Authenticated)
- effective_principals.extend(principal_ids)
-
- return effective_principals
-
- def principals_allowed_by_permission(self, context, permission):
- allowed = set()
-
- for location in reversed(list(lineage(context))):
- # NB: we're walking *up* the object graph from the root
- try:
- acl = location.__acl__
- except AttributeError:
- continue
-
- allowed_here = set()
- denied_here = set()
-
- for ace_action, ace_principal, ace_permissions in acl:
- if not hasattr(ace_permissions, '__iter__'):
- ace_permissions = [ace_permissions]
- if ace_action == Allow and permission in ace_permissions:
- if not ace_principal in denied_here:
- allowed_here.add(ace_principal)
- if ace_action == Deny and permission in ace_permissions:
- denied_here.add(ace_principal)
- if ace_principal == Everyone:
- # clear the entire allowed set, as we've hit a
- # deny of Everyone ala (Deny, Everyone, ALL)
- allowed = set()
- break
- elif ace_principal in allowed:
- allowed.remove(ace_principal)
-
- allowed.update(allowed_here)
-
- return allowed
-
-def get_remoteuser(request):
- user_id = request.environ.get('REMOTE_USER')
- if user_id:
- return [user_id]
- return []
-
-def RemoteUserACLSecurityPolicy():
- """ A security policy which:
-
- - examines the request.environ for the REMOTE_USER variable and
- uses any non-false value as a principal id for this request.
-
- - uses an ACL-based authorization model which attempts to find the
- *first* ACL in the context' lineage. It returns ``Allowed`` from
- its 'permits' method if the single ACL found grants access to the
- current principal. It returns ``Denied`` if permission was not
- granted (either explicitly via a deny or implicitly by not finding
- a matching ACE action). The *first* ACL found in the context's
- lineage is considered canonical; no searching is done for other
- security attributes after the first ACL is found in the context'
- lineage. Use the 'inheriting' variant of this policy to consider
- more than one ACL in the lineage.
-
- An ACL is an ordered sequence of ACE tuples, e.g. ``[(Allow,
- Everyone, 'read'), (Deny, 'george', 'write')]``. ACLs stored on
- model instance objects as their ``__acl__`` attribute will be used
- by the security machinery to grant or deny access.
-
- Enable this security policy by adding the following to your
- application's ``configure.zcml``:
-
- .. code-block:: xml
-
- <utility
- provides="repoze.bfg.interfaces.ISecurityPolicy"
- factory="repoze.bfg.security.RemoteUserACLSecurityPolicy"
- />
-
- """
- return ACLSecurityPolicy(get_remoteuser)
-
-def RemoteUserInheritingACLSecurityPolicy():
- """ A security policy which:
-
- - examines the request.environ for the REMOTE_USER variable and
- uses any non-false value as a principal id for this request.
-
- - Differs from the non-inheriting security policy variants
- (e.g. ``ACLSecurityPolicy``) by virtue of the fact that it does
- not stop looking for ACLs in the object lineage after it finds
- the first one.
-
- - When checking whether a user is permitted (via the ``permits``
- method), the security policy consults the ``context`` for an ACL
- first. If no ACL exists on the context, or one does exist but
- the ACL does not explicitly allow or deny access for any of the
- effective principals, consult the context's parent ACL, and so
- on, until the lineage is exhausted or we determine that the
- policy permits or denies.
-
- During this processing, if any ``Deny`` ACE is found matching
- any effective principal, stop processing by returning an
- ``ACLDenied`` (equals False) immediately. If any ``Allow`` ACE
- is found matching any effective principal, stop processing by
- returning an ``ACLAllowed`` (equals True) immediately. If we
- exhaust the context's lneage, and no ACE has explicitly
- permitted or denied access, return an ``ACLDenied``.
-
- - When computing principals allowed by a permission via the
- ``principals_allowed_by_permission`` method, we compute the set
- of principals that are explicitly granted the ``permission``.
- We do this by walking 'up' the object graph *from the root* to
- the context. During this walking process, if we find an
- explicit ``Allow`` ACE for a principal that matches the
- ``permission``, the principal is included in the allow list.
- However, if later in the walking process that user is mentioned
- in any ``Deny`` ACE for the permission, the user is removed from
- the allow list. If a ``Deny`` to the principal ``Everyone`` is
- encountered during the walking process that matches the
- ``permission``, the allow list is cleared for all principals
- encountered in previous ACLs. The walking process ends after
- we've processed the any ACL directly attached to ``context``; a
- list of principals is returned.
-
- - Other aspects of this policy are the same as those in the
- ACLSecurityPolicy (e.g. ``effective_principals``,
- ``authenticated_userid``).
-
- Enable this security policy by adding the following to your
- application's ``configure.zcml``:
-
- .. code-block:: xml
-
- <utility
- provides="repoze.bfg.interfaces.ISecurityPolicy"
- factory="repoze.bfg.security.RemoteUserInheritingACLSecurityPolicy"
- />
-
- """
- return InheritingACLSecurityPolicy(get_remoteuser)
-
-def get_who_principals(request):
- identity = request.environ.get('repoze.who.identity')
- if not identity:
- return []
- principals = [identity['repoze.who.userid']]
- principals.extend(identity.get('groups', []))
- return principals
-
-def WhoACLSecurityPolicy():
- """
- A security policy which:
-
- - examines the request.environ for the ``repoze.who.identity``
- dictionary. If one is found, the principal ids for the request
- are composed of ``repoze.who.identity['repoze.who.userid']``
- plus ``repoze.who.identity.get('groups', [])``.
-
- - uses an ACL-based authorization model which attempts to find the
- *first* ACL in the context' lineage. It returns ``Allowed`` from
- its 'permits' method if the single ACL found grants access to the
- current principal. It returns ``Denied`` if permission was not
- granted (either explicitly via a deny or implicitly by not finding
- a matching ACE action). The *first* ACL found in the context's
- lineage is considered canonical; no searching is done for other
- security attributes after the first ACL is found in the context'
- lineage. Use the 'inheriting' variant of this policy to consider
- more than one ACL in the lineage.
-
- An ACL is an ordered sequence of ACE tuples, e.g. ``[(Allow,
- Everyone, 'read'), (Deny, 'george', 'write')]``. ACLs stored on
- model instance objects as their ``__acl__`` attribute will be used
- by the security machinery to grant or deny access.
-
- Enable this security policy by adding the following to your
- application's ``configure.zcml``:
-
- .. code-block:: xml
-
- <utility
- provides="repoze.bfg.interfaces.ISecurityPolicy"
- factory="repoze.bfg.security.WhoACLSecurityPolicy"
- />
- """
- return ACLSecurityPolicy(get_who_principals)
-
-RepozeWhoIdentityACLSecurityPolicy = WhoACLSecurityPolicy
-
-deprecated('RepozeWhoIdentityACLSecurityPolicy',
- '(repoze.bfg.security.RepozeWhoIdentityACLSecurityPolicy '
- 'should now be imported as '
- 'repoze.bfg.security.WhoACLSecurityPolicy)',
- )
-
-def WhoInheritingACLSecurityPolicy():
- """ A security policy which:
-
- - examines the request.environ for the ``repoze.who.identity``
- dictionary. If one is found, the principal ids for the request
- are composed of ``repoze.who.identity['repoze.who.userid']``
- plus ``repoze.who.identity.get('groups', [])``.
-
- - Differs from the non-inheriting security policy variants
- (e.g. ``ACLSecurityPolicy``) by virtue of the fact that it does
- not stop looking for ACLs in the object lineage after it finds
- the first one.
-
- - When checking whether a user is permitted (via the ``permits``
- method), the security policy consults the ``context`` for an ACL
- first. If no ACL exists on the context, or one does exist but
- the ACL does not explicitly allow or deny access for any of the
- effective principals, consult the context's parent ACL, and so
- on, until the lineage is exhausted or we determine that the
- policy permits or denies.
-
- During this processing, if any ``Deny`` ACE is found matching
- any effective principal, stop processing by returning an
- ``ACLDenied`` (equals False) immediately. If any ``Allow`` ACE
- is found matching any effective principal, stop processing by
- returning an ``ACLAllowed`` (equals True) immediately. If we
- exhaust the context's lneage, and no ACE has explicitly
- permitted or denied access, return an ``ACLDenied``.
-
- - When computing principals allowed by a permission via the
- ``principals_allowed_by_permission`` method, we compute the set
- of principals that are explicitly granted the ``permission``.
- We do this by walking 'up' the object graph *from the root* to
- the context. During this walking process, if we find an
- explicit ``Allow`` ACE for a principal that matches the
- ``permission``, the principal is included in the allow list.
- However, if later in the walking process that user is mentioned
- in any ``Deny`` ACE for the permission, the user is removed from
- the allow list. If a ``Deny`` to the principal ``Everyone`` is
- encountered during the walking process that matches the
- ``permission``, the allow list is cleared for all principals
- encountered in previous ACLs. The walking process ends after
- we've processed the any ACL directly attached to ``context``; a
- list of principals is returned.
-
- - Other aspects of this policy are the same as those in the
- ACLSecurityPolicy (e.g. ``effective_principals``,
- ``authenticated_userid``).
-
- Enable this security policy by adding the following to your
- application's ``configure.zcml``:
-
- .. code-block:: xml
-
- <utility
- provides="repoze.bfg.interfaces.ISecurityPolicy"
- factory="repoze.bfg.security.WhoInheritingACLSecurityPolicy"
- />
- """
- return InheritingACLSecurityPolicy(get_who_principals)
-
-
-class SecurityPolicyToAuthorizationPolicyAdapter(object):
- """ An adapter registered when an old-style ISecurityPolicy
- utility is configured in ZCML instead of an IAuthorizationPolicy
- utility """
- implements(IAuthorizationPolicy)
- def __init__(self, secpol):
- self.secpol = secpol
-
- def permits(self, context, principals, permission):
- request = get_current_request()
- return self.secpol.permits(context, request, permission)
-
- def principals_allowed_by_permission(self, context, permission):
- return self.secpol.principals_allowed_by_permission(context, permission)
-
-class SecurityPolicyToAuthenticationPolicyAdapter(object):
- implements(IAuthenticationPolicy)
- def __init__(self, secpol):
- self.secpol = secpol
-
- def authenticated_userid(self, request):
- return self.secpol.authenticated_userid(request)
-
- def effective_principals(self, request):
- return self.secpol.effective_principals(request)
-
- def remember(self, request, principal, **kw):
- return []
-
- def forget(self, request):
- return []
-
-def registerBBBAuthn(secpol, registry):
- # Used when an explicit authentication policy is not defined, and
- # an an old-style ISecurityPolicy is registered (via ZCML), turn
- # it into separate authorization and authentication utilities
- # using adapters
- authn = SecurityPolicyToAuthenticationPolicyAdapter(secpol)
- authz = SecurityPolicyToAuthorizationPolicyAdapter(secpol)
- registry.registerUtility(authn, IAuthenticationPolicy)
- registry.registerUtility(authz, IAuthorizationPolicy)
diff --git a/repoze/bfg/security.py b/repoze/bfg/security.py
index bb33435a0..09f146ded 100644
--- a/repoze/bfg/security.py
+++ b/repoze/bfg/security.py
@@ -1,7 +1,6 @@
from zope.component import getSiteManager
from zope.component import queryUtility
from zope.component import providedBy
-from zope.deprecation import deprecated
from repoze.bfg.interfaces import IAuthenticationPolicy
from repoze.bfg.interfaces import IAuthorizationPolicy
@@ -233,27 +232,3 @@ class ACLAllowed(ACLPermitsResult):
class Unauthorized(Exception):
pass
-# BBB imports: these must come at the end of the file, as there's a
-# circular dependency between secpols and security
-from repoze.bfg.secpols import ACLSecurityPolicy
-from repoze.bfg.secpols import InheritingACLSecurityPolicy
-from repoze.bfg.secpols import RemoteUserACLSecurityPolicy
-from repoze.bfg.secpols import RemoteUserInheritingACLSecurityPolicy
-from repoze.bfg.secpols import WhoACLSecurityPolicy
-from repoze.bfg.secpols import WhoInheritingACLSecurityPolicy
-# /BBB imports
-
-for name in ('ACLSecurityPolicy',
- 'InheritingACLSecurityPolicy',
- 'RemoteUserACLSecurityPolicy',
- 'RemoteUserInheritingACLSecurityPolicy',
- 'WhoACLSecurityPolicy',
- 'WhoInheritingACLSecurityPolicy'):
- deprecated(name,
- ('repoze.bfg.security.%s should be imported from '
- 'repoze.bfg.secpols.%s as of BFG 0.9. Please consider '
- 'disusing any security policy in favor of separate '
- 'authorization and authentication policies; security '
- 'policies themselves are deprecated as of BFG 0.9; see the '
- 'Security chapter of the BFG docs for the new spellings.'%
- (name, name)))
diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py
index 99d3c5e8d..1c9dad9e0 100644
--- a/repoze/bfg/testing.py
+++ b/repoze/bfg/testing.py
@@ -131,7 +131,7 @@ def registerViewPermission(name, result=True, viewpermission=None,
Note that view permissions are not checked unless a security
policy is in effect (see ``registerSecurityPolicy``).
- **This function was deprecated in repoze.bfg 1.2.**
+ **This function was deprecated in repoze.bfg 1.1.**
"""
from repoze.bfg.security import Allowed
from repoze.bfg.security import Denied
@@ -147,7 +147,7 @@ def registerViewPermission(name, result=True, viewpermission=None,
deprecated('registerViewPermission',
'registerViewPermission has been deprecated. As of repoze.bfg '
- 'version 1.2, view functions are now responsible for protecting '
+ 'version 1.1, view functions are now responsible for protecting '
'their own execution. A call to this function wont prevent a '
'view from being executed by the repoze.bfg router, nor '
'will the ``repoze.bfg.security.view_execution_permitted`` function '
diff --git a/repoze/bfg/tests/test_secpols.py b/repoze/bfg/tests/test_secpols.py
deleted file mode 100644
index 8f60a0d94..000000000
--- a/repoze/bfg/tests/test_secpols.py
+++ /dev/null
@@ -1,738 +0,0 @@
-import unittest
-
-from repoze.bfg.testing import cleanUp
-
-class TestAPIFunctionsSecpolBBB(unittest.TestCase):
- def setUp(self):
- cleanUp()
-
- def tearDown(self):
- cleanUp()
- try:
- del globals()['__warningregistry__']
- except KeyError:
- pass
-
- def _registerSecurityPolicy(self, secpol):
- import zope.component
- from repoze.bfg.secpols import registerBBBAuthn
- gsm = zope.component.getGlobalSiteManager()
- registerBBBAuthn(secpol, gsm)
-
- def test_has_permission_registered(self):
- secpol = DummySecurityPolicy(False)
- self._registerSecurityPolicy(secpol)
- from repoze.bfg.security import has_permission
- self.assertEqual(has_permission('view', None, None), False)
-
- def test_has_permission_not_registered(self):
- from repoze.bfg.security import has_permission
- result = has_permission('view', None, None)
- self.assertEqual(result, True)
- self.assertEqual(result.msg, 'No authentication policy in use.')
-
- def test_authenticated_userid_registered(self):
- secpol = DummySecurityPolicy(False)
- self._registerSecurityPolicy(secpol)
- from repoze.bfg.security import authenticated_userid
- request = DummyRequest({})
- result = authenticated_userid(request)
- self.assertEqual(result, 'fred')
-
- def test_authenticated_userid_not_registered(self):
- from repoze.bfg.security import authenticated_userid
- request = DummyRequest({})
- result = authenticated_userid(request)
- self.assertEqual(result, None)
-
- def test_effective_principals_registered(self):
- secpol = DummySecurityPolicy(False)
- self._registerSecurityPolicy(secpol)
- from repoze.bfg.security import effective_principals
- request = DummyRequest({})
- result = effective_principals(request)
- self.assertEqual(result, ['fred', 'bob'])
-
- def test_effective_principals_not_registered(self):
- from repoze.bfg.security import effective_principals
- request = DummyRequest({})
- result = effective_principals(request)
- self.assertEqual(result, [])
-
- def test_principals_allowed_by_permission_not_registered(self):
- from repoze.bfg.security import principals_allowed_by_permission
- from repoze.bfg.security import Everyone
- self.assertEqual(principals_allowed_by_permission(None, None),
- [Everyone])
-
- def test_principals_allowed_by_permission_registered(self):
- secpol = DummySecurityPolicy(False)
- self._registerSecurityPolicy(secpol)
- from repoze.bfg.security import principals_allowed_by_permission
- self.assertEqual(principals_allowed_by_permission(None, None),
- ['fred', 'bob'])
-
-
-class TestACLSecurityPolicy(unittest.TestCase):
- def setUp(self):
- cleanUp()
-
- def tearDown(self):
- cleanUp()
-
- def _getTargetClass(self):
- from repoze.bfg.secpols import ACLSecurityPolicy
- return ACLSecurityPolicy
-
- def _makeOne(self, *arg, **kw):
- klass = self._getTargetClass()
- return klass(*arg, **kw)
-
- def test_class_implements_ISecurityPolicy(self):
- from zope.interface.verify import verifyClass
- from repoze.bfg.interfaces import ISecurityPolicy
- verifyClass(ISecurityPolicy, self._getTargetClass())
-
- def test_instance_implements_ISecurityPolicy(self):
- from zope.interface.verify import verifyObject
- from repoze.bfg.interfaces import ISecurityPolicy
- verifyObject(ISecurityPolicy, self._makeOne(lambda *arg: None))
-
- def test_permits_no_principals_no_acl_info_on_context(self):
- context = DummyContext()
- request = DummyRequest({})
- policy = self._makeOne(lambda *arg: [])
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, False)
- from repoze.bfg.security import Everyone
- self.assertEqual(result.principals, set([Everyone]))
- self.assertEqual(result.permission, 'view')
- self.assertEqual(result.context, context)
-
- def test_permits_no_principals_empty_acl_info_on_context(self):
- context = DummyContext()
- context.__acl__ = []
- request = DummyRequest({})
- policy = self._makeOne(lambda *arg: [])
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, False)
- from repoze.bfg.security import Everyone
- self.assertEqual(result.principals, set([Everyone]))
- self.assertEqual(result.permission, 'view')
- self.assertEqual(result.context, context)
-
- def test_permits_no_principals_root_has_empty_acl_info(self):
- context = DummyContext()
- context.__name__ = None
- context.__parent__ = None
- context.__acl__ = []
- context2 = DummyContext()
- context2.__name__ = 'context2'
- context2.__parent__ = context
- request = DummyRequest({})
- policy = self._makeOne(lambda *arg: [])
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, False)
- from repoze.bfg.security import Everyone
- self.assertEqual(result.principals, set([Everyone]))
- self.assertEqual(result.permission, 'view')
- self.assertEqual(result.context, context)
-
- def test_permits_no_principals_root_allows_everyone(self):
- context = DummyContext()
- context.__name__ = None
- context.__parent__ = None
- from repoze.bfg.security import Allow, Everyone
- context.__acl__ = [ (Allow, Everyone, 'view') ]
- context2 = DummyContext()
- context2.__name__ = 'context2'
- context2.__parent__ = context
- request = DummyRequest({})
- policy = self._makeOne(lambda *arg: [])
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.principals, set([Everyone]))
- self.assertEqual(result.permission, 'view')
- self.assertEqual(result.context, context)
-
- def test_permits_deny_implicit(self):
- from repoze.bfg.security import Allow, Authenticated, Everyone
- context = DummyContext()
- context.__acl__ = [ (Allow, 'somebodyelse', 'read') ]
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'read')
- self.assertEqual(result, False)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'read')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, None)
-
- def test_permits_deny_explicit(self):
- from repoze.bfg.security import Deny, Authenticated, Everyone
- context = DummyContext()
- context.__acl__ = [ (Deny, 'fred', 'read') ]
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'read')
- self.assertEqual(result, False)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'read')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, (Deny, 'fred', 'read'))
-
- def test_permits_deny_twoacl_implicit(self):
- from repoze.bfg.security import Allow, Authenticated, Everyone
- context = DummyContext()
- acl = [(Allow, 'somebody', 'view'), (Allow, 'somebody', 'write')]
- context.__acl__ = acl
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'read')
- self.assertEqual(result, False)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'read')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, None)
-
- def test_permits_allow_twoacl_multiperm(self):
- from repoze.bfg.security import Allow, Deny, Authenticated, Everyone
- context = DummyContext()
- acl = [ (Allow, 'fred', ('write', 'view') ), (Deny, 'fred', 'view') ]
- context.__acl__ = acl
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'view')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, (Allow, 'fred', ('write', 'view') ))
-
- def test_permits_deny_twoacl_multiperm(self):
- from repoze.bfg.security import Allow, Deny, Authenticated, Everyone
- context = DummyContext()
- acl = []
- deny = (Deny, 'fred', ('view', 'read'))
- allow = (Allow, 'fred', 'view')
- context.__acl__ = [deny, allow]
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'read')
- self.assertEqual(result, False)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'read')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, deny)
-
- def test_permits_allow_via_location_parent(self):
- from repoze.bfg.security import Allow, Authenticated, Everyone
- context = DummyContext()
- context.__parent__ = None
- context.__name__ = None
- context.__acl__ = [ (Allow, 'fred', 'read') ]
- context2 = DummyContext()
- context2.__parent__ = context
- context2.__name__ = 'myname'
-
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context2, request, 'read')
- self.assertEqual(result, True)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'read')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, ('Allow', 'fred', 'read'))
-
- def test_permits_deny_byorder(self):
- from repoze.bfg.security import Allow, Deny, Authenticated, Everyone
- context = DummyContext()
- acl = []
- deny = (Deny, 'fred', 'read')
- allow = (Allow, 'fred', 'view')
- context.__acl__ = [deny, allow]
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'read')
- self.assertEqual(result, False)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'read')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, deny)
-
- def test_permits_allow_byorder(self):
- from repoze.bfg.security import Allow, Deny, Authenticated, Everyone
- context = DummyContext()
- acl = []
- deny = (Deny, 'fred', ('view', 'read'))
- allow = (Allow, 'fred', 'view')
- context.__acl__ = [allow, deny]
- policy = self._makeOne(lambda *arg: ['fred'])
- request = DummyRequest({})
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.principals,
- set(['fred', Authenticated, Everyone]))
- self.assertEqual(result.permission, 'view')
- self.assertEqual(result.context, context)
- self.assertEqual(result.ace, allow)
-
- def test_principals_allowed_by_permission_direct(self):
- from repoze.bfg.security import Allow
- context = DummyContext()
- acl = [ (Allow, 'chrism', ('read', 'write')),
- (Allow, 'other', 'read') ]
- context.__acl__ = acl
- policy = self._makeOne(lambda *arg: None)
- result = policy.principals_allowed_by_permission(context, 'read')
- self.assertEqual(result, ['chrism', 'other'])
-
- def test_principals_allowed_by_permission_acquired(self):
- from repoze.bfg.security import Allow
- context = DummyContext()
- acl = [ (Allow, 'chrism', ('read', 'write')),
- (Allow, 'other', ('read',)) ]
- context.__acl__ = acl
- context.__parent__ = None
- context.__name__ = 'context'
- inter = DummyContext()
- inter.__name__ = None
- inter.__parent__ = context
- policy = self._makeOne(lambda *arg: None)
- result = policy.principals_allowed_by_permission(inter, 'read')
- self.assertEqual(result, ['chrism', 'other'])
-
- def test_principals_allowed_by_permission_no_acls(self):
- policy = self._makeOne(lambda *arg: None)
- result = policy.principals_allowed_by_permission(None, 'read')
- self.assertEqual(result, [])
-
-class TestInheritingACLSecurityPolicy(unittest.TestCase):
- def setUp(self):
- cleanUp()
-
- def tearDown(self):
- cleanUp()
-
- def _getTargetClass(self):
- from repoze.bfg.secpols import InheritingACLSecurityPolicy
- return InheritingACLSecurityPolicy
-
- def _makeOne(self, *arg, **kw):
- klass = self._getTargetClass()
- return klass(*arg, **kw)
-
- def test_class_implements_ISecurityPolicy(self):
- from zope.interface.verify import verifyClass
- from repoze.bfg.interfaces import ISecurityPolicy
- verifyClass(ISecurityPolicy, self._getTargetClass())
-
- def test_instance_implements_ISecurityPolicy(self):
- from zope.interface.verify import verifyObject
- from repoze.bfg.interfaces import ISecurityPolicy
- verifyObject(ISecurityPolicy, self._makeOne(lambda *arg: None))
-
- def test_permits(self):
- from repoze.bfg.security import Deny
- from repoze.bfg.security import Allow
- from repoze.bfg.security import Everyone
- from repoze.bfg.security import Authenticated
- from repoze.bfg.security import ALL_PERMISSIONS
- from repoze.bfg.security import DENY_ALL
- policy = self._makeOne(lambda *arg: [])
- root = DummyContext()
- community = DummyContext(__name__='community', __parent__=root)
- blog = DummyContext(__name__='blog', __parent__=community)
- root.__acl__ = [
- (Allow, Authenticated, VIEW),
- ]
- community.__acl__ = [
- (Allow, 'fred', ALL_PERMISSIONS),
- (Allow, 'wilma', VIEW),
- DENY_ALL,
- ]
- blog.__acl__ = [
- (Allow, 'barney', MEMBER_PERMS),
- (Allow, 'wilma', VIEW),
- ]
- policy = self._makeOne(lambda request: request.principals)
- request = DummyRequest({})
-
- request.principals = ['wilma']
- result = policy.permits(blog, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.context, blog)
- self.assertEqual(result.ace, (Allow, 'wilma', VIEW))
- result = policy.permits(blog, request, 'delete')
- self.assertEqual(result, False)
- self.assertEqual(result.context, community)
- self.assertEqual(result.ace, (Deny, Everyone, ALL_PERMISSIONS))
-
- request.principals = ['fred']
- result = policy.permits(blog, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.context, community)
- self.assertEqual(result.ace, (Allow, 'fred', ALL_PERMISSIONS))
- result = policy.permits(blog, request, 'doesntevenexistyet')
- self.assertEqual(result, True)
- self.assertEqual(result.context, community)
- self.assertEqual(result.ace, (Allow, 'fred', ALL_PERMISSIONS))
-
- request.principals = ['barney']
- result = policy.permits(blog, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.context, blog)
- self.assertEqual(result.ace, (Allow, 'barney', MEMBER_PERMS))
- result = policy.permits(blog, request, 'administer')
- self.assertEqual(result, False)
- self.assertEqual(result.context, community)
- self.assertEqual(result.ace, (Deny, Everyone, ALL_PERMISSIONS))
-
- request.principals = ['someguy']
- result = policy.permits(root, request, 'view')
- self.assertEqual(result, True)
- self.assertEqual(result.context, root)
- self.assertEqual(result.ace, (Allow, Authenticated, VIEW))
- result = policy.permits(blog, request, 'view')
- self.assertEqual(result, False)
- self.assertEqual(result.context, community)
- self.assertEqual(result.ace, (Deny, Everyone, ALL_PERMISSIONS))
-
- request.principals = []
- result = policy.permits(root, request, 'view')
- self.assertEqual(result, False)
- self.assertEqual(result.context, root)
- self.assertEqual(result.ace, None)
-
- request.principals = []
- context = DummyContext()
- result = policy.permits(context, request, 'view')
- self.assertEqual(result, False)
-
- def test_principals_allowed_by_permission_direct(self):
- from repoze.bfg.security import Allow
- from repoze.bfg.security import DENY_ALL
- context = DummyContext()
- acl = [ (Allow, 'chrism', ('read', 'write')),
- DENY_ALL,
- (Allow, 'other', 'read') ]
- context.__acl__ = acl
- policy = self._makeOne(lambda *arg: None)
- result = sorted(
- policy.principals_allowed_by_permission(context, 'read'))
- self.assertEqual(result, ['chrism'])
-
- def test_principals_allowed_by_permission(self):
- from repoze.bfg.security import Allow
- from repoze.bfg.security import Deny
- from repoze.bfg.security import DENY_ALL
- from repoze.bfg.security import ALL_PERMISSIONS
- root = DummyContext(__name__='', __parent__=None)
- community = DummyContext(__name__='community', __parent__=root)
- blog = DummyContext(__name__='blog', __parent__=community)
- root.__acl__ = [ (Allow, 'chrism', ('read', 'write')),
- (Allow, 'other', ('read',)),
- (Allow, 'jim', ALL_PERMISSIONS)]
- community.__acl__ = [ (Deny, 'flooz', 'read'),
- (Allow, 'flooz', 'read'),
- (Allow, 'mork', 'read'),
- (Deny, 'jim', 'read'),
- (Allow, 'someguy', 'manage')]
- blog.__acl__ = [ (Allow, 'fred', 'read'),
- DENY_ALL]
-
- policy = self._makeOne(lambda *arg: None)
- result = sorted(policy.principals_allowed_by_permission(blog, 'read'))
- self.assertEqual(result, ['fred'])
- result = sorted(policy.principals_allowed_by_permission(community,
- 'read'))
- self.assertEqual(result, ['chrism', 'mork', 'other'])
- result = sorted(policy.principals_allowed_by_permission(community,
- 'read'))
- result = sorted(policy.principals_allowed_by_permission(root, 'read'))
- self.assertEqual(result, ['chrism', 'jim', 'other'])
-
- def test_principals_allowed_by_permission_no_acls(self):
- policy = self._makeOne(lambda *arg: None)
- context = DummyContext()
- result = sorted(policy.principals_allowed_by_permission(context,'read'))
- self.assertEqual(result, [])
-
- def test_effective_principals(self):
- context = DummyContext()
- request = DummyRequest({})
- request.principals = ['fred']
- policy = self._makeOne(lambda request: request.principals)
- result = sorted(policy.effective_principals(request))
- from repoze.bfg.security import Everyone
- from repoze.bfg.security import Authenticated
- self.assertEqual(result,
- ['fred', Authenticated, Everyone])
-
- def test_no_effective_principals(self):
- context = DummyContext()
- request = DummyRequest({})
- request.principals = []
- policy = self._makeOne(lambda request: request.principals)
- result = sorted(policy.effective_principals(request))
- from repoze.bfg.security import Everyone
- self.assertEqual(result, [Everyone])
-
- def test_authenticated_userid(self):
- context = DummyContext()
- request = DummyRequest({})
- request.principals = ['fred']
- policy = self._makeOne(lambda request: request.principals)
- result = policy.authenticated_userid(request)
- self.assertEqual(result, 'fred')
-
- def test_no_authenticated_userid(self):
- context = DummyContext()
- request = DummyRequest({})
- request.principals = []
- policy = self._makeOne(lambda request: request.principals)
- result = policy.authenticated_userid(request)
- self.assertEqual(result, None)
-
-class TestRemoteUserACLSecurityPolicy(unittest.TestCase):
- def setUp(self):
- cleanUp()
-
- def tearDown(self):
- cleanUp()
-
- def _getTargetClass(self):
- from repoze.bfg.secpols import RemoteUserACLSecurityPolicy
- return RemoteUserACLSecurityPolicy
-
- def _makeOne(self, *arg, **kw):
- klass = self._getTargetClass()
- return klass(*arg, **kw)
-
- def test_instance_implements_ISecurityPolicy(self):
- from zope.interface.verify import verifyObject
- from repoze.bfg.interfaces import ISecurityPolicy
- verifyObject(ISecurityPolicy, self._makeOne())
-
- def test_authenticated_userid(self):
- context = DummyContext()
- request = DummyRequest({'REMOTE_USER':'fred'})
- policy = self._makeOne()
- result = policy.authenticated_userid(request)
- self.assertEqual(result, 'fred')
-
- def test_authenticated_userid_no_remote_user(self):
- context = DummyContext()
- request = DummyRequest({})
- policy = self._makeOne()
- result = policy.authenticated_userid(request)
- self.assertEqual(result, None)
-
- def test_effective_principals(self):
- context = DummyContext()
- request = DummyRequest({'REMOTE_USER':'fred'})
- policy = self._makeOne()
- result = policy.effective_principals(request)
- from repoze.bfg.security import Everyone
- from repoze.bfg.security import Authenticated
- self.assertEqual(result, [Everyone, Authenticated, 'fred'])
-
- def test_effective_principals_no_remote_user(self):
- context = DummyContext()
- request = DummyRequest({})
- policy = self._makeOne()
- result = policy.effective_principals(request)
- from repoze.bfg.security import Everyone
- self.assertEqual(result, [Everyone])
-
-class TestRemoteUserInheritingACLSecurityPolicy(TestRemoteUserACLSecurityPolicy):
- def _getTargetClass(self):
- from repoze.bfg.secpols import RemoteUserInheritingACLSecurityPolicy
- return RemoteUserInheritingACLSecurityPolicy
-
-class TestWhoACLSecurityPolicy(unittest.TestCase):
- def setUp(self):
- cleanUp()
-
- def tearDown(self):
- cleanUp()
-
- def _getTargetClass(self):
- from repoze.bfg.secpols import WhoACLSecurityPolicy
- return WhoACLSecurityPolicy
-
- def _makeOne(self, *arg, **kw):
- klass = self._getTargetClass()
- return klass(*arg, **kw)
-
- def test_instance_implements_ISecurityPolicy(self):
- from zope.interface.verify import verifyObject
- from repoze.bfg.interfaces import ISecurityPolicy
- verifyObject(ISecurityPolicy, self._makeOne())
-
- def test_authenticated_userid(self):
- context = DummyContext()
- identity = {'repoze.who.identity':{'repoze.who.userid':'fred'}}
- request = DummyRequest(identity)
- policy = self._makeOne()
- result = policy.authenticated_userid(request)
- self.assertEqual(result, 'fred')
-
- def test_authenticated_userid_no_who_ident(self):
- context = DummyContext()
- request = DummyRequest({})
- policy = self._makeOne()
- result = policy.authenticated_userid(request)
- self.assertEqual(result, None)
-
- def test_effective_principals(self):
- context = DummyContext()
- identity = {'repoze.who.identity':{'repoze.who.userid':'fred'}}
- request = DummyRequest(identity)
- policy = self._makeOne()
- result = policy.effective_principals(request)
- from repoze.bfg.security import Everyone
- from repoze.bfg.security import Authenticated
- self.assertEqual(result, [Everyone, Authenticated, 'fred'])
-
- def test_effective_principals_no_who_ident(self):
- context = DummyContext()
- request = DummyRequest({})
- policy = self._makeOne()
- result = policy.effective_principals(request)
- from repoze.bfg.security import Everyone
- self.assertEqual(result, [Everyone])
-
-class TestWhoInheritingACLSecurityPolicy(TestWhoACLSecurityPolicy):
- def _getTargetClass(self):
- from repoze.bfg.secpols import WhoInheritingACLSecurityPolicy
- return WhoInheritingACLSecurityPolicy
-
-class TestSecurityPolicyToAuthenticationPolicyAdapter(unittest.TestCase):
- def _getTargetClass(self):
- from repoze.bfg.secpols import \
- SecurityPolicyToAuthenticationPolicyAdapter
- return SecurityPolicyToAuthenticationPolicyAdapter
-
- def _makeOne(self, secpol):
- return self._getTargetClass()(secpol)
-
- def test_class_implements_IAuthenticationPolicy(self):
- from zope.interface.verify import verifyClass
- from repoze.bfg.interfaces import IAuthenticationPolicy
- verifyClass(IAuthenticationPolicy, self._getTargetClass())
-
- def test_instance_implements_IAuthenticationPolicy(self):
- from zope.interface.verify import verifyObject
- from repoze.bfg.interfaces import IAuthenticationPolicy
- verifyObject(IAuthenticationPolicy, self._makeOne(None))
-
- def test_authenticated_userid(self):
- secpol = DummySecurityPolicy(None)
- adapter = self._makeOne(secpol)
- result = adapter.authenticated_userid(None)
- self.assertEqual(result, 'fred')
-
- def test_effective_principals(self):
- secpol = DummySecurityPolicy(None)
- adapter = self._makeOne(secpol)
- result = adapter.effective_principals(None)
- self.assertEqual(result, ['fred', 'bob'])
-
- def test_remember(self):
- secpol = DummySecurityPolicy(None)
- adapter = self._makeOne(secpol)
- result = adapter.remember(None, None)
- self.assertEqual(result, [])
-
- def test_forget(self):
- secpol = DummySecurityPolicy(None)
- adapter = self._makeOne(secpol)
- result = adapter.forget(None)
- self.assertEqual(result, [])
-
-class TestSecurityPolicyToAuthorizationPolicyAdapter(unittest.TestCase):
- def _getTargetClass(self):
- from repoze.bfg.secpols import \
- SecurityPolicyToAuthorizationPolicyAdapter
- return SecurityPolicyToAuthorizationPolicyAdapter
-
- def _makeOne(self, secpol):
- return self._getTargetClass()(secpol)
-
- def test_class_implements_IAuthorizationPolicy(self):
- from zope.interface.verify import verifyClass
- from repoze.bfg.interfaces import IAuthorizationPolicy
- verifyClass(IAuthorizationPolicy, self._getTargetClass())
-
- def test_instance_implements_IAuthorizationPolicy(self):
- from zope.interface.verify import verifyObject
- from repoze.bfg.interfaces import IAuthorizationPolicy
- verifyObject(IAuthorizationPolicy, self._makeOne(None))
-
- def test_permits(self):
- from repoze.bfg.threadlocal import manager
- manager.push({'request':1})
- try:
- secpol = DummySecurityPolicy(None)
- adapter = self._makeOne(secpol)
- result = adapter.permits(None, None, None)
- self.assertEqual(result, None)
- self.assertEqual(secpol.checked, (None, 1, None))
- finally:
- manager.pop()
-
- def test_principals_allowed_by_permission(self):
- secpol = DummySecurityPolicy(None)
- adapter = self._makeOne(secpol)
- result = adapter.principals_allowed_by_permission(None, None)
- self.assertEqual(result, ['fred', 'bob'])
-
-
-
-class DummyContext:
- def __init__(self, *arg, **kw):
- self.__dict__.update(kw)
-
-class DummyRequest:
- def __init__(self, environ):
- self.environ = environ
-
-
-VIEW = 'view'
-EDIT = 'edit'
-CREATE = 'create'
-DELETE = 'delete'
-MODERATE = 'moderate'
-ADMINISTER = 'administer'
-COMMENT = 'comment'
-
-GUEST_PERMS = (VIEW, COMMENT)
-MEMBER_PERMS = GUEST_PERMS + (EDIT, CREATE, DELETE)
-MODERATOR_PERMS = MEMBER_PERMS + (MODERATE,)
-ADMINISTRATOR_PERMS = MODERATOR_PERMS + (ADMINISTER,)
-
-class DummySecurityPolicy:
- def __init__(self, result):
- self.result = result
-
- def permits(self, *args):
- self.checked = args
- return self.result
-
- def authenticated_userid(self, request):
- return 'fred'
-
- def effective_principals(self, request):
- return ['fred', 'bob']
-
- def principals_allowed_by_permission(self, context, permission):
- return ['fred', 'bob']
-
diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py
index fdf8fa9e3..9e8c70cbd 100644
--- a/repoze/bfg/tests/test_zcml.py
+++ b/repoze/bfg/tests/test_zcml.py
@@ -1976,61 +1976,6 @@ class TestZCMLConfigure(unittest.TestCase):
self.assertRaises(IOError, self._callFUT, 'configure.zcml',
self.module)
- def test_secpol_BBB(self):
- from repoze.bfg.interfaces import IAuthorizationPolicy
- from repoze.bfg.interfaces import IAuthenticationPolicy
- from repoze.bfg.interfaces import ISecurityPolicy
- from repoze.bfg.interfaces import ILogger
- secpol = DummySecurityPolicy()
- from zope.component import getGlobalSiteManager
- gsm = getGlobalSiteManager()
- gsm.registerUtility(secpol, ISecurityPolicy)
- logger = DummyLogger()
- gsm.registerUtility(logger, ILogger, name='repoze.bfg.debug')
- self._callFUT('configure.zcml', self.module)
- self.failUnless(gsm.queryUtility(IAuthenticationPolicy))
- self.failUnless(gsm.queryUtility(IAuthorizationPolicy))
- self.assertEqual(len(logger.messages), 1)
- self.failUnless('ISecurityPolicy' in logger.messages[0])
-
- def test_iunauthorized_appfactory_BBB(self):
- from repoze.bfg.interfaces import IUnauthorizedAppFactory
- from repoze.bfg.interfaces import IForbiddenView
- from zope.component import getGlobalSiteManager
- from repoze.bfg.interfaces import ILogger
- context = DummyContext()
- def factory():
- return 'yo'
- logger = DummyLogger()
- gsm = getGlobalSiteManager()
- gsm.registerUtility(factory, IUnauthorizedAppFactory)
- logger = DummyLogger()
- gsm.registerUtility(logger, ILogger, name='repoze.bfg.debug')
- self._callFUT('configure.zcml', self.module)
- self.assertEqual(len(logger.messages), 1)
- self.failUnless('forbidden' in logger.messages[0])
- forbidden = gsm.getUtility(IForbiddenView)
- self.assertEqual(forbidden(None, DummyRequest()), 'yo')
-
- def test_inotfound_appfactory_BBB(self):
- from repoze.bfg.interfaces import INotFoundAppFactory
- from repoze.bfg.interfaces import INotFoundView
- from zope.component import getGlobalSiteManager
- from repoze.bfg.interfaces import ILogger
- context = DummyContext()
- def factory():
- return 'yo'
- logger = DummyLogger()
- gsm = getGlobalSiteManager()
- gsm.registerUtility(factory, INotFoundAppFactory)
- logger = DummyLogger()
- gsm.registerUtility(logger, ILogger, name='repoze.bfg.debug')
- self._callFUT('configure.zcml', self.module)
- self.assertEqual(len(logger.messages), 1)
- self.failUnless('notfound' in logger.messages[0])
- notfound = gsm.getUtility(INotFoundView)
- self.assertEqual(notfound(None,DummyRequest()), 'yo')
-
class TestBFGViewFunctionGrokker(unittest.TestCase):
def setUp(self):
cleanUp()
diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py
index fb4e8d720..f584d17fb 100644
--- a/repoze/bfg/zcml.py
+++ b/repoze/bfg/zcml.py
@@ -28,16 +28,13 @@ from repoze.bfg.authorization import ACLAuthorizationPolicy
from repoze.bfg.interfaces import IRoutesMapper
from repoze.bfg.interfaces import IViewPermission
-from repoze.bfg.interfaces import INotFoundAppFactory
from repoze.bfg.interfaces import INotFoundView
from repoze.bfg.interfaces import IForbiddenView
from repoze.bfg.interfaces import IAuthenticationPolicy
from repoze.bfg.interfaces import IAuthorizationPolicy
-from repoze.bfg.interfaces import ISecurityPolicy
from repoze.bfg.interfaces import ISecuredView
from repoze.bfg.interfaces import IMultiView
from repoze.bfg.interfaces import IView
-from repoze.bfg.interfaces import IUnauthorizedAppFactory
from repoze.bfg.interfaces import ILogger
from repoze.bfg.interfaces import IPackageOverrides
from repoze.bfg.interfaces import IRequest
@@ -51,7 +48,6 @@ from repoze.bfg.request import create_route_request_factory
from repoze.bfg.security import Unauthorized
-from repoze.bfg.secpols import registerBBBAuthn
from repoze.bfg.settings import get_settings
from repoze.bfg.traversal import find_interface
@@ -657,73 +653,6 @@ def zcml_configure(name, package):
context.package = package
xmlconfig.include(context, name, package)
context.execute_actions(clear=False)
-
- logger = queryUtility(ILogger, name='repoze.bfg.debug')
- registry = getSiteManager()
-
- # persistence means always having to say you're sorry
-
- authentication_policy = registry.queryUtility(IAuthenticationPolicy)
-
- if not authentication_policy:
- # deal with bw compat of <= 0.8 security policies (deprecated)
- secpol = registry.queryUtility(ISecurityPolicy)
- if secpol is not None:
- logger and logger.warn(
- 'Your application is using a repoze.bfg ``ISecurityPolicy`` '
- '(probably registered via ZCML). This form of security policy '
- 'has been deprecated in BFG 0.9. See the "Security" chapter '
- 'of the repoze.bfg documentation to see how to register a more '
- 'up to date set of security policies (an authentication '
- 'policy and an authorization policy). ISecurityPolicy-based '
- 'security policies will cease to work in a later BFG '
- 'release.')
- registerBBBAuthn(secpol, registry)
-
- forbidden_view = registry.queryUtility(IForbiddenView)
- unauthorized_app_factory = registry.queryUtility(IUnauthorizedAppFactory)
-
- if unauthorized_app_factory is not None:
- if forbidden_view is None:
- warning = (
- 'Instead of registering a utility against the '
- 'repoze.bfg.interfaces.IUnauthorizedAppFactory interface '
- 'to return a custom forbidden response, you should now '
- 'use the "forbidden" ZCML directive.'
- 'The IUnauthorizedAppFactory interface was deprecated in '
- 'repoze.bfg 0.9 and will be removed in a subsequent version '
- 'of repoze.bfg. See the "Hooks" chapter of the repoze.bfg '
- 'documentation for more information about '
- 'the forbidden directive.')
- logger and logger.warn(warning)
- def forbidden(context, request):
- app = unauthorized_app_factory()
- response = request.get_response(app)
- return response
- registry.registerUtility(forbidden, IForbiddenView)
-
- notfound_view = registry.queryUtility(INotFoundView)
- notfound_app_factory = registry.queryUtility(INotFoundAppFactory)
-
- if notfound_app_factory is not None:
- if notfound_view is None:
- warning = (
- 'Instead of registering a utility against the '
- 'repoze.bfg.interfaces.INotFoundAppFactory interface '
- 'to return a custom notfound response, you should use the '
- '"notfound" ZCML directive. The '
- 'INotFoundAppFactory interface was deprecated in'
- 'repoze.bfg 0.9 and will be removed in a subsequent version '
- 'of repoze.bfg. See the "Hooks" chapter of the repoze.bfg '
- 'documentation for more information about '
- 'the "notfound" directive.')
- logger and logger.warn(warning)
- def notfound(context, request):
- app = notfound_app_factory()
- response = request.get_response(app)
- return response
- registry.registerUtility(notfound, INotFoundView)
-
return context.actions
file_configure = zcml_configure # backwards compat (>0.8.1)