Plone: subscribe to an event when a new user is registered

When a new user is registered in Plone, we want to do automated actions with the user.

Let's define the code for the automated action to run, when a new Plone user is registered.

from datetime import datetime
from plone import api

def user_registered(user, event):
    if not hasattr(user, '__ac_permissions__'):
        # not a Plone user
        return
    member = api.user.get(userid=user.getId())
    now = datetime.now()
    member.setMemberProperties(
        mapping={
            'custom_registration_date': now,
        },
    )

Now let's register this function for the Plone PrincipalCreatedEvent event, in our configure.zcml.

<subscriber
    for="* Products.PluggableAuthService.interfaces.events.IPrincipalCreatedEvent"
    handler=".events.user_registered"
    />
By @MrTango in
Tags :