Note: The following solution is for Liferay 6.1 and for later versions like Liferay 6.2 you directly have a configuration option as explained by Ankit P in his answer
Is it doable?
No and Yes.
It cannot be done through configuration alone, but can be achieved with some development effort.
There are various ways to achieve this. The simplest I can think of is with a ModelListener hook, following is a short tutorial:
- Create a hook project.
Define the following in your liferay-hook.xml
:
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
Create a portal.properties
file in your src folder and add the following:
value.object.listener.com.liferay.portal.model.User=com.my.hook.listeners.MyCustomUserListener
Create the class MyCustomUserListener
class by extending BaseModelListener<User>
:
public class MyCustomUserListener extends BaseModelListener<User> {
@Override
public void onAfterCreate(User model) throws ModelListenerException {
// Your code to associate the user with an Organization
// sample code is as follows:
long userId = model.getUserId();
// since the method adds multiple users at one go to the Organization
long[] userIds = new long[] {userId};
UserLocalServiceUtil.addOrganizationUsers(organizationId, userIds);
}
}
Build and deploy.
- Now whenever a User is created, the
onAfterCreate
method would be called.
But what will you do for Users who are already created?
- You can either create a one-time process (through a portlet or as an UpgradeProcess in the above hook) to associate all the User to the Organization.
- Or else you can create a Custom Action hook (with login post event), which will be called after the User has successfully logged-in and will associate the user to the Organization.
- Here is a good tutorial in the developer guide for this.
Hope this helps.