Plone 5.2.5
Products.PlonePAS 6.0.8
I have created two groups (One and Two).
What can I do so that all new registered users are automatically added to these two groups? (self-registration activated)
Thanks!
Plone 5.2.5
Products.PlonePAS 6.0.8
I have created two groups (One and Two).
What can I do so that all new registered users are automatically added to these two groups? (self-registration activated)
Thanks!
Unlikely possible without coding.
Something like this (from ChatGPT-4 )
<subscriber
for="zope.interface.Interface
Products.PluggableAuthService.interfaces.events.IUserLoggedInEvent"
handler=".event_subscribers.add_user_to_group"
/>
Not the best/modern code, but reasonable:
from Products.CMFCore.utils import getToolByName
def add_user_to_group(event):
"""Event subscriber to add a user to specific groups after the first login."""
# Get the user object from the event
user = event.object
# Get the portal object
acl_users = getToolByName(user, 'acl_users')
portal = getToolByName(acl_users, 'portal_url').getPortalObject()
# Get the portal_groups tool
portal_groups = getToolByName(portal, 'portal_groups')
# Get the user id
user_id = user.getId()
# Check if user already exists in any of the groups
# Assuming you don't want to re-add users who are already members
group_ids = [group.id for group in portal_groups.getGroupsByUserId(user_id)]
if 'GroupOne' in group_ids and 'GroupTwo' in group_ids:
return
# Add the user to the groups
portal_groups.addPrincipalToGroup(user_id, 'GroupOne')
portal_groups.addPrincipalToGroup(user_id, 'GroupTwo')
You got the idea..
If you want it to be dynamic, you can add a IGroupsPlugin like here:
and add a user to the group you want without storing them in the group source but assigning it by some rules.
Programmatically I'd use plone.api
Thank you very much!
Is there a content rule that does this in versions higher than Plone 5.2.5?
Thanks!
For me the simplest would be the following (simplified steps):
Site Setup -> Content Rules -> Add content rule -> Triggering event = User Created -> Action = Add to groups -> select the desired groups -> Save.
I've looked to see if the action "Add to groups" exists, but I can't find it.
Does not exist? Is this something pending to be programmed and released?
Thanks!
PS: I have searched the following sites:
My Plone 5.2.5
Plone 6 Demo: Content Rules
Search results ยท PyPI
https://github.com/search?q=plone%20contentrules&type=repositories
I have this working, it will check 'usergroup settings in the registry' and add users to groups on every login:
<subscriber
for="Products.PluggableAuthService.interfaces.events.IUserLoggedInEvent"
handler=".event_subscribers.add_user_to_group"
/>
# -*- coding: utf-8 -*-
from plone import api
from my.addon.interfaces import IUserGroupSettings
def add_user_to_group(event):
"""Event subscriber to add a user to specific groups after the first login."""
# Get the user id
user_id = api.user.get_current()
groups = api.portal.get_registry_record('usergroup', interface=IUserGroupSettings)
# Add the user to the groups
if groups:
for group in groups:
if not group in user_id.getGroups():
api.group.add_user(groupname=group, user=user_id)
#return True
I dont think there is a event for 'first time log in', but it is possible to check a users 'last login', so if that exists (or is newer than 2000/01/01 which might be the date if they have not logged in).
There is an event for the initial login:
Thanks
(For reference, for only first login, use this:)
<subscriber
for="Products.PlonePAS.events.IUserInitialLoginInEvent"
handler=".event_subscribers.add_user_to_group"
/>
Manuel told me this only works AFTER the first login, if self registration is enabled. Is there another way of doing this?
UPDATE: I installed it on a new site, and it seems to work as expected, so not sure about the above.