Forums

Home » Liferay Portal » English » 2. Using Liferay » General

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ay Kay
How to auto assign new user to organization?
December 9, 2012 2:43 PM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

Hi there,

I need to manually assign each and every new user to my default organization for there is no auto-assign mechanism? Eerrr..... Really?

That means you guys really want me to hire an additional secretary?



Shaking my head in disbelief over Liferay's status of stone age or alternatively over the fact how well hidden the concering switch is....
Hitoshi Ozawa
RE: How to auto assign new user to organization?
December 9, 2012 2:53 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7954

Join Date: March 23, 2010

Recent Posts

That means you guys really want me to hire an additional secretary?


Well, that'll help the economy. :-)

That aside, go to "Control Panel" -> "Users and Organization" -> select "Action" button to the right of the default organization -> "Assign Users" -> Select "Available" tab -> select the top checkbox to check all users -> select "Update Associations"
Ay Kay
RE: How to auto assign new user to organization?
December 10, 2012 2:37 AM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

No. Just no.

That means, Liferay forces us to e-mail or phone up each and every newly registered user to ask from what page he or she was actually registering from in order to correctly assign them one by one to the appropriate organisation? Are you serious on that?

I try to make the scenario clear once again. Hopefully.
On monday morning I see 200 newly registered users in my list. 120 Americans, 30 Italians, 30 French guys, 20 Germans. All of them registered from their specific country based "location site". All of them end up in that long list of 200 new users you mention. Unrecognizable in terms of interest in language branch. Now you tell me, I mark all of them to assign them to an organisation? You see your loophole?!

I need to automatically assign them to their appropriate organisation depending on from what location site they were registering. See?!
Hitoshi Ozawa
RE: How to auto assign new user to organization?
December 10, 2012 6:30 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7954

Join Date: March 23, 2010

Recent Posts

Should have written your question fully beforehand. Seems like you didn't design your portal property.
Ay Kay
RE: How to auto assign new user to organization?
December 10, 2012 12:44 PM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

Just for clearance, I put it another way.
QUESTION: How do you automatically assign a newly registered user to the organisation whose location site he or she happened to be on when registering?
Hitoshi Ozawa:
Seems like you didn't design your portal property.

When all this is closely related to how the portal is set up, then what portal property missing or falsely set up prevents this basic behaviour from fireing?

Sorry for loosing my temper, but this sort of stuff (programming basics) heavily eats into our budget.
Ay Kay
RE: How to auto assign new user to organization?
December 10, 2012 4:11 PM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

Ay Kay:
Hitoshi Ozawa:
Seems like you didn't design your portal property.

When all this is closely related to how the portal is set up, then what portal property missing or falsely set up prevents this basic behaviour from fireing?

I am afraid I won't get an answer to this. Since this is fundamental function that simply needs to work, I spend the last couple of hours writing a solution myself.

Really can't believe this is true. This is the first programming I do in Liferay and it's something that is so elementary that I (and the whole team here) expected it to be an out-of-the-box feature. I cannot believe all Liferay administrators cheerfully assign newly registered users to their relevant organisation by hand. So even if I invented a solution meanwhile, please someone point me to the official approach.

Just in case such official approach does not exist, I would happily share my solution with all those poor souls out there that administrate this by hand.
I finally sketch my approach.
It's a hook (called it AutoAssigner-hook) that you simply deploy with your portal instance. After that you can simply use standard login portlet on any organisations "location's site". (Sorry if I use wrong terms, still new to this.) That leads to any user signing up being assigned to his or her correct organization. (That one he or she was coming from.) Tested it, does fly.
Luis Mas
RE: How to auto assign new user to organization?
December 11, 2012 8:08 AM
Answer

Luis Mas

Rank: Regular Member

Posts: 146

Join Date: May 18, 2009

Recent Posts

You could open an issue in http://issues.liferay.com and contribute your solution.
Ay Kay
RE: How to auto assign new user to organization?
December 12, 2012 4:37 PM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

Luis Mas:
You could open an issue in http://issues.liferay.com and contribute your solution.

Well, yeah. I don't see that Liferay, Inc. has an issue with this at all. So I rather don't. But I'll post it in my blog as soon as I can spare some time.
Ay Kay
RE: How to auto assign new user to organization?
December 17, 2012 3:46 PM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

Okay, my solution to this 2002 juggernaut piece a shhoftware.

/AutoAssigner-hook/docroot/WEB-INF/liferay-hook.xml
1<?xml version="1.0"?>
2<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.1.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_1_0.dtd">
3
4<hook>
5    <portal-properties>portal.properties</portal-properties>
6</hook>

/AutoAssigner-hook/docroot/WEB-INF/src/portal.properties
1value.object.listener.com.liferay.portal.model.User=<YOUR-PACKAGE>.NewUserListener

/AutoAssigner-hook/docroot/WEB-INF/src/<YOUR-PACKAGE>/NewUserListener.java
Code can be improved. (Proper exception handling, Logging, additionally handling web sites, not only organizations, etc. ...)
 1
 2package YOUR-PACKAGE;
 3
 4import com.liferay.portal.ModelListenerException;
 5import com.liferay.portal.kernel.exception.PortalException;
 6import com.liferay.portal.kernel.exception.SystemException;
 7import com.liferay.portal.model.BaseModelListener;
 8import com.liferay.portal.model.Group;
 9import com.liferay.portal.model.User;
10import com.liferay.portal.service.GroupLocalServiceUtil;
11import com.liferay.portal.service.ServiceContextThreadLocal;
12import com.liferay.portal.service.UserLocalServiceUtil;
13
14public class NewUserListener extends BaseModelListener<User>
15{
16    @Override
17    public void onAfterCreate(User model) throws ModelListenerException
18    {
19        System.out.println("********************************************************************************");
20        System.out.println("NEW USER");
21        System.out.println("********************************************************************************");
22
23        String portalUrl = ServiceContextThreadLocal.getServiceContext().getPortalURL();
24        String layoutUrl = ServiceContextThreadLocal.getServiceContext().getLayoutURL();
25        layoutUrl = layoutUrl.substring(portalUrl.length()); // Consume prefix such as "http://localhost:8080"
26
27        int slashPos = layoutUrl.indexOf('/', 1); // Get position of slash after servlet context path such as "/web/"
28        String friendlyUrl = layoutUrl.substring(slashPos);
29        slashPos = friendlyUrl.indexOf('/', 1); // Try to get next slash position
30        if (slashPos != -1) // User is deeper inside navigation structure
31            friendlyUrl = friendlyUrl.substring(0, slashPos); // Chop off suffixing navigation structure
32
33        Long companyId = ServiceContextThreadLocal.getServiceContext().getCompanyId();
34
35        try
36        {
37            Group group = GroupLocalServiceUtil.getFriendlyURLGroup(companyId, friendlyUrl);
38            long organizationId = group.getClassPK();
39            long[] userIds = new long[] { model.getUserId() };
40            UserLocalServiceUtil.addOrganizationUsers(organizationId, userIds);
41
42            System.out.println("Added user " + model.getLogin() + " (id " + model.getUserId()
43                    + ") to organisation (friendly URL: " + friendlyUrl + ", Id: " + organizationId + ")");
44        }
45        catch (PortalException exception)
46        {
47            exception.printStackTrace();
48        }
49        catch (SystemException exception)
50        {
51            exception.printStackTrace();
52        }
53    }
54}


Again, it's a 4-liner doing a job that's delivering most basic stuff that should have shipped with version 0.1.

With all due respect, I'd firmly like to stress how astonishingly poor the whole documentation here is for a 2002 state of the art software. The book "LIFERAY in Action" is a joke with code that doens't run. "ActionUtil"? Are you kidding me? Most basic stuff is simply NOT explained. People need to refer to Youtube. Poor job, ....