liferay - How to land users to different page as per their roles -
i using liferay , want show different landing pages after users log in: if admin of portal try login-in, land page , if guest login portal login page b. @today15 have done this..
package com.landing.page.pagetwo; import java.util.arraylist; import java.util.list; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import com.liferay.portal.kernel.log.logfactoryutil; import com.liferay.portal.kernel.events.action; import com.liferay.portal.kernel.events.actionexception; import com.liferay.portal.kernel.exception.portalexception; import com.liferay.portal.kernel.exception.systemexception; import com.liferay.portal.kernel.log.log; import com.liferay.portal.kernel.struts.lastpath; import com.liferay.portal.kernel.util.prefspropsutil; import com.liferay.portal.kernel.util.propskeys; import com.liferay.portal.kernel.util.stringpool; import com.liferay.portal.kernel.util.validator; import com.liferay.portal.kernel.util.webkeys; import com.liferay.portal.model.group; import com.liferay.portal.model.user; import com.liferay.portal.service.grouplocalserviceutil; import com.liferay.portal.util.portalutil; public class landingpage extends action { @override public void run(httpservletrequest httpsreq, httpservletresponse httpsres) throws actionexception { try { dorun(httpsreq, httpsres); } catch (exception e) { throw new actionexception(e); } } protected void dorun( httpservletrequest request, httpservletresponse response) throws exception { long companyid = portalutil.getcompanyid(request); string path = prefspropsutil.getstring( companyid, propskeys.default_landing_page_path); if (_log.isinfoenabled()) { _log.info(propskeys.default_landing_page_path + stringpool.equal + path); } if (validator.isnull(path)) { path = getcustomlandingpage(request); } httpsession session = request.getsession(); session.setattribute(webkeys.last_path, new lastpath( stringpool.blank, path)); } private string getcustomlandingpage(httpservletrequest request) throws portalexception, systemexception { string customlandingpagepath = stringpool.blank; customlandingpagepath = getsitepath(portalutil.getuser(request), false); return customlandingpagepath; } private string getsitepath(user user, boolean includelanguage) throws portalexception, systemexception { string sitepath = stringpool.blank; list<group> usersites = getsites(user.getuserid()); string language = stringpool.blank; if (includelanguage) { language = stringpool.slash + user.getlocale().getlanguage(); } if ((usersites != null) && !usersites.isempty()) { string sitefriendlyurl = usersites.get(0).getfriendlyurl(); sitepath = language + "/group" + sitefriendlyurl + "/pagetwo"; } return sitepath; } private list<group> getsites(long userid) throws portalexception, systemexception { list<group> sites = new arraylist<group>(); (group group : grouplocalserviceutil.getusergroups(userid)) { if (group.isregularsite() && !"guest".equalsignorecase(group.getname())) { sites.add(group); break; } } return sites; } private static log _log = logfactoryutil.getlog(landingpage.class); }
now can land on site's custom page. how ristrict other user land page, , how land them other page.
first of all: users guests, until log in. every authenticated user have "user" role. have check if user admin. having said that, take approach: create custom field role , call landingpage example. create postloginhook.
package com.liferay.sample.hook; import com.liferay.portal.kernel.events.action; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class loginaction extends action { public void run(httpservletrequest req, httpservletresponse res) { system.out.println("## custom login action"); } }
in run method, check value of landingpage custom field, , set lastpath property accordingly. example:
map params = new hashmap(); params.put("p_l_id", new string[] {"pri.1.1"}); lastpath lastpath = new lastpath("/c", "/portal/layout", params); session.setattribute(webkeys.last_path, lastpath);
you may want take @ defaultlandingpageaction class liferay itself: https://github.com/liferay/liferay-portal/blob/master/portal-impl/src/com/liferay/portal/events/defaultlandingpageaction.java
i recommend use of custom field, have possibility change landing pages @ times. advantage be, other roles well.
Comments
Post a Comment