Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
William Gosse
Overriding ServicePreAction throws exception in 6.1 GA2
September 13, 2012 6:29 AM
Answer

William Gosse

Rank: Regular Member

Posts: 108

Join Date: July 4, 2010

Recent Posts

I had an extension that I wrote for 6.0..5 the overrides com.liferay.portal.events.ServicePreAction. I was using this extension the deny access to the control panel for certain users. Here's the code I was using :
 1
 2public class SQServicePreAction extends ServicePreAction {
 3
 4    @Override
 5    protected boolean isViewableGroup(User user, long groupId,
 6            boolean privateLayout, long layoutId, String controlPanelCategory,
 7            PermissionChecker permissionChecker) throws PortalException,
 8            SystemException {
 9
10        boolean isViewable = super.isViewableGroup(user, groupId, privateLayout, layoutId,
11                controlPanelCategory, permissionChecker);
12       
13        if (isViewable) {
14            Group group = GroupLocalServiceUtil.getGroup(groupId);
15            
16            if (group.isControlPanel())
17            {
18                long companyId = group.getCompanyId();
19                long userId = user.getUserId();
20
21                if (SQPortalUtils.isUserAdminUser(companyId, userId))
22                    isViewable = true;
23                else
24                    isViewable = !SQPortalUtils.isUserEnterpriseUser(userId);
25            }
26        }   
27       
28        return isViewable;       
29    }
30
31}


I know that isViewableGroup is now deprecated so I've tried replacing it with something else. It doesn't to matter what I put in the class now because as soon as it deploys I get the following exception:

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
...

12:55:11,310 ERROR [http-bio-8080-exec-1][error_jsp:422] User ID null
12:55:11,311 ERROR [http-bio-8080-exec-1][error_jsp:423] Current URL /
12:55:11,311 ERROR [http-bio-8080-exec-1][error_jsp:424] Referer null
12:55:11,312 ERROR [http-bio-8080-exec-1][error_jsp:425] Remote address 127.0.0.1
12:55:11,315 ERROR [http-bio-8080-exec-1][error_jsp:427] javax.servlet.ServletException: javax.servlet.jsp.JspException: java.lang.NullPointerException
javax.servlet.ServletException: javax.servlet.jsp.JspException: java.lang.NullPointerException
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:907)
...

Caused by: java.lang.NullPointerException
at com.liferay.taglib.util.ThemeUtil.include(ThemeUtil.java:94)
at com.liferay.taglib.theme.IncludeTag.doEndTag(IncludeTag.java:39)
at org.apache.jsp.html.common.themes.portal_jsp._jspx_meth_liferay_002dtheme_005finclude_005f1(portal_jsp.java:479)
at org.apache.jsp.html.common.themes.portal_jsp._jspService(portal_jsp.java:424)
... 161 more

As soon as I remove servlet.service.events.pre=com.salesquest.portal.events.SQServicePreAction form my portal ext properties file the exception goes away. Can theis class no longer be overwritten for some Any help understanding this issue would be greatly appreciated.
William Gosse
RE: Overriding ServicePreAction throws exception in 6.1 GA2
September 13, 2012 8:28 AM
Answer

William Gosse

Rank: Regular Member

Posts: 108

Join Date: July 4, 2010

Recent Posts

Ok thanks to the forum I found my answer here: https://www.liferay.com/community/forums/-/message_boards/message/13109956

There is now more then on a pre service action class that need to be listed with the like the following:
servlet.service.events.pre=com.liferay.portal.events.ServicePreAction,com.liferay.portal.events.DeviceServicePreAction,com.liferay.portal.events.ThemeServicePreAction,com.myorg.portal.events.SQServicePreAction

I also change my code it no longer use the deprecated isViewableGroup method. Here's my new code:
 1
 2public class SQServicePreAction extends Action {
 3
 4    public void run(HttpServletRequest request, HttpServletResponse response)
 5            throws ActionException {
 6        try {
 7
 8            ThemeDisplay themeDisplay = (ThemeDisplay) request
 9                    .getAttribute(WebKeys.THEME_DISPLAY);
10            if (GroupLocalServiceUtil.getGroup(
11                    themeDisplay.getLayout().getGroupId()).isControlPanel()) {
12
13                User currentUser = UserServiceUtil.getUserById(themeDisplay
14                        .getUserId());
15
16                if (SQPortalUtils.isUserEnterpriseUser(currentUser.getUserId())) {
17                    throw new PrincipalException("Enterprise User, "
18                            + currentUser.getScreenName()
19                            + ", is not allowed access to the Control Panel.");
20                }
21            }
22
23        } catch (Exception ex) {
24            throw new ActionException(ex);
25        }
26
27    }
28}


I found this approach at: http://java-liferay.blogspot.com/2012/07/hide-and-disable-access-to-liferays-61.html