How can I implement Role based Login in Liferay. I have already created a Login Hook which redirects users to their site at the time of login.
Ex:
User 1 will got to site 1.
User 2 will got to site 1.
User 3 will got to site 2.
User 4 will got to site 2.
and if a user is not assigned to any site, will be directed to default login site.
Now, What I want is if User 1(role = administrator) Logs in should be redirected to page 1(administrator page) of site 1.
and User 2(role = manager) Logs in should be redirected to page 2(manager page) of site 1.
Based on their roles. Can this be possible ?. If yes please help regarding this as how to implement it.
I'll add my code below.
@Component
(
immediate = true,
property =
{
"key=login.events.post"
},
service = LifecycleAction.class
)
public class MyCustomLoginHookLoginPostAction extends Action
{
private final static String PUBLIC_PAGE_CONTEXT = "/web";
private final static String PRIVATE_PAGE_CONTEXT = "/group";
@Override
public void run(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ActionException
{
final long companyId = PortalUtil.getCompanyId(httpServletRequest);
String path = PrefsPropsUtil.getString(companyId, PropsKeys.DEFAULT_LANDING_PAGE_PATH);
try
{
path = getCustomLandingPage(httpServletRequest);
}
catch (PortalException e)
{
throw new ActionException(e);
}
if(Validator.isNotNull(path))
{
final HttpSession httpSession = httpServletRequest.getSession();
httpSession.setAttribute(WebKeys.LAST_PATH, new LastPath(StringPool.BLANK, path));
}
}
private String getCustomLandingPage(HttpServletRequest httpServletRequest) throws PortalException
{
String path = null;
User user = PortalUtil.getUser(httpServletRequest);
List<Organization> orgs = user.getOrganizations();
if(orgs != null && !orgs.isEmpty())
{
for(Organization org : orgs)
{
Group orgSite = org.getGroup();
int publicPageCount = orgSite.getPublicLayoutsPageCount();
int privatePageCount = orgSite.getPrivateLayoutsPageCount();
if(publicPageCount > 0)
{
path = PUBLIC_PAGE_CONTEXT+ orgSite.getFriendlyURL();
break;
}
else if(privatePageCount > 0)
{
path = PRIVATE_PAGE_CONTEXT + orgSite.getFriendlyURL();
break;
}
}
}
if(Validator.isNull(path))
{
List<Group> sites = user.getGroups();
if(sites != null && !sites.isEmpty())
{
for(Group site : sites)
{
int publicPageCount = site.getPublicLayoutsPageCount();
int privatePageCount = site.getPrivateLayoutsPageCount();
if(publicPageCount > 0)
{
path = PUBLIC_PAGE_CONTEXT + site.getFriendlyURL();
break;
}
else if(privatePageCount > 0)
{
path = PRIVATE_PAGE_CONTEXT + site.getFriendlyURL();
break;
}
}
}
}
if(Validator.isNull(path))
{
path = PrefsPropsUtil.getString(PortalUtil.getCompanyId(httpServletRequest), PropsKeys.DEFAULT_LANDING_PAGE_PATH);
}
return path;
}
}
EDIT: after the answer
2022-08-24 09:23:22.822 ERROR [http-nio-8080-exec-2][MainServlet:526] java.lang.NullPointerException
java.lang.NullPointerException
at my.custom.login.hook.MyCustomLoginHookLoginPostAction.getCustomLandingPage(MyCustomLoginHookLoginPostAction.java:847)
at my.custom.login.hook.MyCustomLoginHookLoginPostAction.run(MyCustomLoginHookLoginPostAction.java:826)
at com.liferay.portal.kernel.events.InvokerAction.run(InvokerAction.java:41)
at com.liferay.portal.kernel.events.Action.processLifecycleEvent(Action.java:34)
at com.liferay.portal.events.EventsProcessorUtil.process(EventsProcessorUtil.java:98)
at com.liferay.portal.events.EventsProcessorUtil.process(EventsProcessorUtil.java:60)
at com.liferay.portal.servlet.MainServlet.loginUser(MainServlet.java:1054)
at com.liferay.portal.servlet.MainServlet.service(MainServlet.java:518)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:119)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:144)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:103)
at com.liferay.portal.apio.internal.architect.servlet.filter.APIDocumentationFilter.processFilter(APIDocumentationFilter.java:74)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:144)
at com.liferay.portal.servlet.filters.strip.StripFilter.processFilter(StripFilter.java:340)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.servlet.filters.password.modified.PasswordModifiedFilter.processFilter(PasswordModifiedFilter.java:57)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:144)
at com.liferay.portal.servlet.filters.secure.BaseAuthFilter.processFilter(BaseAuthFilter.java:340)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:144)
at com.liferay.portal.sharepoint.SharepointFilter.processFilter(SharepointFilter.java:88)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:144)
at com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.processFilter(VirtualHostFilter.java:263)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:144)
at com.liferay.portal.monitoring.internal.servlet.filter.MonitoringFilter.processFilter(MonitoringFilter.java:178)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:188)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:188)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:389)
at com.liferay.portal.servlet.filters.urlrewrite.UrlRewriteFilter.processFilter(UrlRewriteFilter.java:65)
at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:49)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:207)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:112)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:168)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:168)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:188)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:101)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Faulty Line
String path = null;
final long companyId = PortalUtil.getCompanyId(httpServletRequest);
User user = PortalUtil.getUser(httpServletRequest);
//getting role IDs
// THIS LINE Role roleAdministrator = roleLocalService.fetchRole(companyId, "Administrator");
Role roleManager = roleLocalService.fetchRole(companyId, "Manager");
if(roleLocalService.hasUserRole(user.getUserId(), roleAdministrator.getRoleId()))
{
//has adminrole
}
if(roleLocalService.hasUserRole(user.getUserId(), roleManager.getRoleId()))
{
//has adminrole
}
Sure. You can check roles with the RoleLocalService.
@Component
(
immediate = true,
property =
{
"key=login.events.post"
},
service = LifecycleAction.class
)
public class MyCustomLoginHookLoginPostAction extends Action
{
private final static String PUBLIC_PAGE_CONTEXT = "/web";
private final static String PRIVATE_PAGE_CONTEXT = "/group";
@Override
public void run(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ActionException
{
final long companyId = PortalUtil.getCompanyId(httpServletRequest);
String path = PrefsPropsUtil.getString(companyId, PropsKeys.DEFAULT_LANDING_PAGE_PATH);
try
{
path = getCustomLandingPage(httpServletRequest);
}
catch (PortalException e)
{
throw new ActionException(e);
}
if(Validator.isNotNull(path))
{
final HttpSession httpSession = httpServletRequest.getSession();
httpSession.setAttribute(WebKeys.LAST_PATH, new LastPath(StringPool.BLANK, path));
}
}
private String getCustomLandingPage(HttpServletRequest httpServletRequest) throws PortalException
{
String path = null;
final long companyId = PortalUtil.getCompanyId(httpServletRequest);
User user = PortalUtil.getUser(httpServletRequest);
//getting role IDs
Role roleAdministrator = RoleLocalServiceUtil.fetchRole(companyId, "Administrator");
Role roleManager = RoleLocalServiceUtil.fetchRole(companyId, "Manager");
if(RoleLocalServiceUtil.hasUserRole(user.getUserId(), roleAdministrator.getRoleId())) {
//has adminrole
}
if(RoleLocalServiceUtil.hasUserRole(user.getUserId(), roleManager.getRoleId())) {
//has adminrole
}
List<Organization> orgs = user.getOrganizations();
if(orgs != null && !orgs.isEmpty())
{
for(Organization org : orgs)
{
Group orgSite = org.getGroup();
int publicPageCount = orgSite.getPublicLayoutsPageCount();
int privatePageCount = orgSite.getPrivateLayoutsPageCount();
if(publicPageCount > 0)
{
path = PUBLIC_PAGE_CONTEXT+ orgSite.getFriendlyURL();
break;
}
else if(privatePageCount > 0)
{
path = PRIVATE_PAGE_CONTEXT + orgSite.getFriendlyURL();
break;
}
}
}
if(Validator.isNull(path))
{
List<Group> sites = user.getGroups();
if(sites != null && !sites.isEmpty())
{
for(Group site : sites)
{
int publicPageCount = site.getPublicLayoutsPageCount();
int privatePageCount = site.getPrivateLayoutsPageCount();
if(publicPageCount > 0)
{
path = PUBLIC_PAGE_CONTEXT + site.getFriendlyURL();
break;
}
else if(privatePageCount > 0)
{
path = PRIVATE_PAGE_CONTEXT + site.getFriendlyURL();
break;
}
}
}
}
if(Validator.isNull(path))
{
path = PrefsPropsUtil.getString(PortalUtil.getCompanyId(httpServletRequest), PropsKeys.DEFAULT_LANDING_PAGE_PATH);
}
return path;
}
}