From 140fdbb54c467159313ede564dd3ad4077e30f20 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sat, 2 Mar 2019 11:26:37 -0800 Subject: Implement bw-compat authenticated_userid and unauthenticated_userid --- src/pyramid/request.py | 4 +++- src/pyramid/security.py | 20 ++++++++++++++------ tests/test_security.py | 12 ++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/pyramid/request.py b/src/pyramid/request.py index 726f485e7..bb0dcaa2b 100644 --- a/src/pyramid/request.py +++ b/src/pyramid/request.py @@ -16,7 +16,9 @@ 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, + SecurityAPIMixin, + AuthenticationAPIMixin, + AuthorizationAPIMixin, ) from pyramid.url import URLMethodsMixin from pyramid.util import ( diff --git a/src/pyramid/security.py b/src/pyramid/security.py index efc0c193c..66e314f79 100644 --- a/src/pyramid/security.py +++ b/src/pyramid/security.py @@ -312,10 +312,14 @@ class AuthenticationAPIMixin(object): Use ``request.identity`` instead. """ - policy = _get_authentication_policy(self) - if policy is None: + authn = _get_authentication_policy(self) + security = _get_security_policy(self) + if authn is not None: + return authn.authenticated_userid(self) + elif security is not None: + return security.identify(self) + else: return None - return policy.authenticated_userid(self) @property def unauthenticated_userid(self): @@ -332,10 +336,14 @@ class AuthenticationAPIMixin(object): Use ``request.identity`` instead. """ - policy = _get_authentication_policy(self) - if policy is None: + authn = _get_authentication_policy(self) + security = _get_security_policy(self) + if authn is not None: + return authn.unauthenticated_userid(self) + elif security is not None: + return security.identify(self) + else: return None - return policy.unauthenticated_userid(self) @property def effective_principals(self): diff --git a/tests/test_security.py b/tests/test_security.py index 514175a92..dd2c225d3 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -369,6 +369,12 @@ class TestAuthenticatedUserId(unittest.TestCase): def test_with_authentication_policy(self): request = _makeRequest() _registerAuthenticationPolicy(request.registry, 'yo') + _registerSecurityPolicy(request.registry, 'wat') + self.assertEqual(request.authenticated_userid, 'yo') + + def test_with_security_policy(self): + request = _makeRequest() + _registerSecurityPolicy(request.registry, 'yo') self.assertEqual(request.authenticated_userid, 'yo') def test_with_authentication_policy_no_reg_on_request(self): @@ -395,6 +401,12 @@ class TestUnAuthenticatedUserId(unittest.TestCase): def test_with_authentication_policy(self): request = _makeRequest() _registerAuthenticationPolicy(request.registry, 'yo') + _registerSecurityPolicy(request.registry, 'wat') + self.assertEqual(request.unauthenticated_userid, 'yo') + + def test_with_security_policy(self): + request = _makeRequest() + _registerSecurityPolicy(request.registry, 'yo') self.assertEqual(request.unauthenticated_userid, 'yo') def test_with_authentication_policy_no_reg_on_request(self): -- cgit v1.2.3