[BlueBream] zope.session (no events)

Stephan Richter stephan.richter at gmail.com
Sat Mar 5 10:26:36 EST 2011


On Saturday, March 05, 2011, Jonah Crawford wrote:
> I would like to originate a notification event when an authenticated login
> occurs. I had hoped to hook onto objects being added
> to PersistentSessionDataContainer as the notification could send the
> session_id along with it -- but as things
> stand PersistentSessionDataContainer has no interface. Should I
> monkeypatch or perhaps someone could suggest a more elegant approach.

Hi,

we have a similar use case where we want to log when a user logs in. In our 
case, we implemented our own SessionCredentialsPlugin where we mark 
credentials with an initial flag. In the PrincipalFolder implementation we 
simply look for the flag and mark the event. Here are some snippets:

class UserSessionCredentials(SessionCredentialsPlugin):

    def extractCredentials(self, request):
        credentials = SessionCredentialsPlugin.extractCredentials(self, 
request)
        if credentials:
            login = request.get(self.loginfield, None)
            password = request.get(self.passwordfield, None)
            if login and password:
                # This lets us distinguish the beginning of a login session
                # (login & password provided in the request and stored in
                # a browser session) from its continuation (login & password
                # retrieved from the browser session)
                credentials['initial'] = True
            else:
                credentials['initial'] = False
        return credentials

class UserFolder(PrincipalFolder):

    def authenticateCredentials(self, credentials):
        info = PrincipalFolder.authenticateCredentials(self, credentials)
        if info is not None and credentials.get('initial'):
            alsoProvides(info, interfaces.IInitialLogin)
        return info

@adapter(IAuthenticatedPrincipalCreated)
def postLoginEvent(event):
    if interfaces.IInitialLogin.providedBy(event.info):
        notify(interfaces.UserLoggedIn(event.principal))

BTW, credit goes to Marius for this code.

Regards,
Stephan
-- 
Entrepreneur and Software Geek
Google me. "Zope Stephan Richter"


More information about the bluebream mailing list