diff --git a/README.rst b/README.rst index ed0b216..ac2f1bf 100644 --- a/README.rst +++ b/README.rst @@ -96,8 +96,9 @@ An event is triggered when one of the multiple policies configured is selected. # Track policy used, for prefixing user_id and for logging. def on_policy_selected(event): - print("%s (%s) was selected for request %s" % (event.policy_name, - event.policy, - event.request)) + print("%s (%s) authenticated %s for request %s" % (event.policy_name, + event.policy, + event.userid, + event.request)) config.add_subscriber(on_policy_selected, MultiAuthPolicySelected) diff --git a/pyramid_multiauth/__init__.py b/pyramid_multiauth/__init__.py index 0b488da..1440286 100644 --- a/pyramid_multiauth/__init__.py +++ b/pyramid_multiauth/__init__.py @@ -37,10 +37,11 @@ def track_policy(event): print("We selected policy %s" % event.policy) """ - def __init__(self, policy, request): + def __init__(self, policy, request, userid=None): self.policy = policy self.policy_name = getattr(policy, "_pyramid_multiauth_name", None) self.request = request + self.userid = userid @implementer(IAuthenticationPolicy) @@ -77,7 +78,8 @@ def authenticated_userid(self, request): userid = policy.authenticated_userid(request) if userid is not None: request.registry.notify(MultiAuthPolicySelected(policy, - request)) + request, + userid)) if self._callback is None: break @@ -117,6 +119,9 @@ def effective_principals(self, request): userid = policy.authenticated_userid(request) if userid is None: continue + request.registry.notify(MultiAuthPolicySelected(policy, + request, + userid)) groups = self._callback(userid, request) if groups is not None: break diff --git a/pyramid_multiauth/tests.py b/pyramid_multiauth/tests.py index 3b6f7b3..bacd21c 100644 --- a/pyramid_multiauth/tests.py +++ b/pyramid_multiauth/tests.py @@ -198,9 +198,17 @@ def track_policy(event): self.assertEquals(selected_policy[0].policy, policies[0]) self.assertEquals(selected_policy[0].policy_name, "name") + self.assertEquals(selected_policy[0].userid, "test2") self.assertEquals(selected_policy[0].request, request) self.assertEquals(len(selected_policy), 1) + # Effective principals also triggers an event when groupfinder + # is provided. + policy_with_group = MultiAuthenticationPolicy(policies, + lambda u, r: ['foo']) + policy_with_group.effective_principals(request) + self.assertEquals(len(selected_policy), 2) + def test_stacking_of_unauthenticated_userid(self): policies = [TestAuthnPolicy2(), TestAuthnPolicy3()] policy = MultiAuthenticationPolicy(policies)