0

I'm trying to add "User Group" Selection field while creating a user (Self User Registration form - create_account.jsp). Here Custom fields is not helpful because Usergroup is already exists in db. I want to insert into existing Users_UserGroups table. I'm using below hook: But User is not added in the group and no exception is printed.

Please suggest me for any other way to achieve this.

public class CustomCreateAccountAction extends BaseStrutsPortletAction
{ 
    public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) 
                    throws Exception 
            { 
                System.out.println("My Custom Process Action Method is Called"); 

                String emailid=ParamUtil.getString(actionRequest, "emailAddress");
                long[] userGroupIds = null;

                originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest,actionResponse); 

                System.out.println("This is after user is registered");
                if (SessionErrors.isEmpty(actionRequest)) 
                {
                ThemeDisplay themeDisplay =(ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

                long newlyCreatedUserId=UserLocalServiceUtil.getUserIdByEmailAddress(themeDisplay.getCompanyId(), emailid);

                long userIds[]={newlyCreatedUserId};
                long dummygroupid=16206;

                System.out.println("TEST UserID="+newlyCreatedUserId);
                System.out.println("TEST GroupID="+dummygroupid);
                //Everything went well until here.
                UserServiceUtil.addUserGroupUsers(dummygroupid, userIds);
                //below sysout is not printed. and no exception or user in group db created. 
                System.out.println("user added to group");
                }

            } 


            public String render(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception 
            { 
                System.out.println("My Custom Render Method is Called"); 
                return originalStrutsPortletAction.render(null, portletConfig, renderRequest, renderResponse);   
                } 

        } 

for more info please have a look at this thread.

Pavan JDev
  • 202
  • 1
  • 11

2 Answers2

1

Using UserLocalServiceUtil instead of UserServiceUtil worked. Basically, the difference is that *ServiceUtil checks permissions and *LocalServiceUtil does not.

Pavan JDev
  • 202
  • 1
  • 11
0

I'm not sure this is the best idea, but you can use the hook for both modify the user creación jsp and save the value via model listener on user create/modify.

Regsrds

Eduardo Pérez
  • 488
  • 3
  • 10
  • i'm done with modifying jsp using hook. But User model doesn't have "User Group" . So i can't able to make that call. – Pavan JDev May 16 '15 at 16:01
  • You can store on custom field as it was a cache and the use the modelListener to move this value where you want. – Eduardo Pérez May 16 '15 at 17:15
  • My bad, Using UserLocalServiceUtil worked instead of UserServiceUtil Basically, the difference is that *ServiceUtil checks permissions and *LocalServiceUtil does not. But thanks for asking me to go through "Model Listener" mostly works the same way(Pre-defined). – Pavan JDev May 16 '15 at 23:31