4
votes

In the web application I'm working,

  • I've created an organization site which can be accessed by group/game/home.
  • I've made this default landing page after login. By putting this value in the portal-ext.properties file default.landing.page.path=/group/games/home
  • I've also set these two properties to make sure the user is redirected to the default landing page after successfully login. layout.show.portlet.access.denied=false auth.login.url=/web/guest/login

Now, when I add a new user to the application, the user is created successfully.

But, since the user is not added to the organization, it doesn't have right to access the default landing page.

I have to manually login through admin panel and go to control panel > User and Organisation and then Update Association.

Can someone please help me understand if this can be done at the time of user creation? Is it doable?

NOTE

I'm using Liferay 6.2, Ankit P's suggestion worked for me.

If anyone is using earlier version, Please refer to Prakash K's suggestion.

3

3 Answers

2
votes

After adding user to Liferay. You can use below code to add user to Organization

 UserLocalServiceUtil.addOrganizationUser(organizationId,userId);
 OrganizationLocalServiceUtil.updateOrganization(organization, true);
2
votes

You can set this association by default from UI starting from Liferay v6.2+.

  1. Login as administrator and navigate to
  2. Control PanelPortal SettingsUsers (Under Configuration) → Select Default User Associations
  3. Add your desired Organization Name and Save.

This setting will automatically associate Users to that particular organization when Users are created.

You can also tick the Apply to Existing Users checkbox to update the association for Users already created in database, the User in this case associated when the User logs in to the system first time after this setting.

Hope it helps !

2
votes

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:

  1. Create a hook project.
  2. Define the following in your liferay-hook.xml:

    <hook>
        <portal-properties>portal.properties</portal-properties>
    </hook>
    
  3. 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
    
  4. 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);
        }
    }
    
  5. Build and deploy.

  6. Now whenever a User is created, the onAfterCreate method would be called.

But what will you do for Users who are already created?

  1. 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.
  2. 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.
  3. Here is a good tutorial in the developer guide for this.

Hope this helps.