summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTheron Luhn <theron@luhn.com>2019-03-03 08:49:58 -0800
committerTheron Luhn <theron@luhn.com>2019-03-03 08:55:02 -0800
commit5abdd1d7636a8f7c5cda4c8fcf2669c3937c1186 (patch)
treebcbf0d8499152be3efb4b592657a55b179421b2a /src
parent140fdbb54c467159313ede564dd3ad4077e30f20 (diff)
downloadpyramid-5abdd1d7636a8f7c5cda4c8fcf2669c3937c1186.tar.gz
pyramid-5abdd1d7636a8f7c5cda4c8fcf2669c3937c1186.tar.bz2
pyramid-5abdd1d7636a8f7c5cda4c8fcf2669c3937c1186.zip
Implement new request.has_permission.
Deleted AuthorizationAPIMixin
Diffstat (limited to 'src')
-rw-r--r--src/pyramid/request.py7
-rw-r--r--src/pyramid/security.py67
-rw-r--r--src/pyramid/testing.py2
3 files changed, 29 insertions, 47 deletions
diff --git a/src/pyramid/request.py b/src/pyramid/request.py
index bb0dcaa2b..5c68abe69 100644
--- a/src/pyramid/request.py
+++ b/src/pyramid/request.py
@@ -15,11 +15,7 @@ from pyramid.interfaces import (
from pyramid.decorator import reify
from pyramid.i18n import LocalizerRequestMixin
from pyramid.response import Response, _get_response_factory
-from pyramid.security import (
- SecurityAPIMixin,
- AuthenticationAPIMixin,
- AuthorizationAPIMixin,
-)
+from pyramid.security import SecurityAPIMixin, AuthenticationAPIMixin
from pyramid.url import URLMethodsMixin
from pyramid.util import (
InstancePropertyHelper,
@@ -153,7 +149,6 @@ class Request(
LocalizerRequestMixin,
SecurityAPIMixin,
AuthenticationAPIMixin,
- AuthorizationAPIMixin,
ViewMethodsMixin,
):
"""
diff --git a/src/pyramid/security.py b/src/pyramid/security.py
index 66e314f79..4881d94a6 100644
--- a/src/pyramid/security.py
+++ b/src/pyramid/security.py
@@ -299,6 +299,34 @@ class SecurityAPIMixin(object):
return None
return policy.identify(self)
+ def has_permission(self, permission, context=None):
+ """ Given a permission and an optional context, returns an instance of
+ :data:`pyramid.security.Allowed` if the permission is granted to this
+ request with the provided context, or the context already associated
+ with the request. Otherwise, returns an instance of
+ :data:`pyramid.security.Denied`. This method delegates to the current
+ security policy. Returns
+ :data:`pyramid.security.Allowed` unconditionally if no security
+ policy has been registered for this request. If ``context`` is not
+ supplied or is supplied as ``None``, the context used is the
+ ``request.context`` attribute.
+
+ :param permission: Does this request have the given permission?
+ :type permission: str
+ :param context: A resource object or ``None``
+ :type context: object
+ :returns: Either :class:`pyramid.security.Allowed` or
+ :class:`pyramid.security.Denied`.
+
+ """
+ if context is None:
+ context = self.context
+ policy = _get_security_policy(self)
+ if policy is None:
+ return Allowed('No security policy in use.')
+ identity = policy.identify(self)
+ return policy.permits(self, context, identity, permission)
+
class AuthenticationAPIMixin(object):
@property
@@ -361,45 +389,6 @@ class AuthenticationAPIMixin(object):
return policy.effective_principals(self)
-class AuthorizationAPIMixin(object):
- def has_permission(self, permission, context=None):
- """ Given a permission and an optional context, returns an instance of
- :data:`pyramid.security.Allowed` if the permission is granted to this
- request with the provided context, or the context already associated
- with the request. Otherwise, returns an instance of
- :data:`pyramid.security.Denied`. This method delegates to the current
- authentication and authorization policies. Returns
- :data:`pyramid.security.Allowed` unconditionally if no authentication
- policy has been registered for this request. If ``context`` is not
- supplied or is supplied as ``None``, the context used is the
- ``request.context`` attribute.
-
- :param permission: Does this request have the given permission?
- :type permission: str
- :param context: A resource object or ``None``
- :type context: object
- :returns: Either :class:`pyramid.security.Allowed` or
- :class:`pyramid.security.Denied`.
-
- .. versionadded:: 1.5
-
- """
- if context is None:
- context = self.context
- reg = _get_registry(self)
- authn_policy = reg.queryUtility(IAuthenticationPolicy)
- if authn_policy is None:
- return Allowed('No authentication policy in use.')
- authz_policy = reg.queryUtility(IAuthorizationPolicy)
- if authz_policy is None:
- raise ValueError(
- 'Authentication policy registered without '
- 'authorization policy'
- ) # should never happen
- principals = authn_policy.effective_principals(self)
- return authz_policy.permits(context, principals, permission)
-
-
@implementer(ISecurityPolicy)
class LegacySecurityPolicy:
"""
diff --git a/src/pyramid/testing.py b/src/pyramid/testing.py
index 7a85aff85..90a49c04a 100644
--- a/src/pyramid/testing.py
+++ b/src/pyramid/testing.py
@@ -19,7 +19,6 @@ from pyramid.security import (
Everyone,
SecurityAPIMixin,
AuthenticationAPIMixin,
- AuthorizationAPIMixin,
)
from pyramid.threadlocal import get_current_registry, manager
@@ -306,7 +305,6 @@ class DummyRequest(
LocalizerRequestMixin,
SecurityAPIMixin,
AuthenticationAPIMixin,
- AuthorizationAPIMixin,
ViewMethodsMixin,
):
""" A DummyRequest object (incompletely) imitates a :term:`request` object.