Konakart offers an enterprise level eCommerce solution that you can easily customize to match the requirements of your
customers. KonaKart has a rich feature set to provide your customers with all of the information and functionality that they
require in order to shop at your on line store. Konakart also provides a way to create deployable war files for Liferay. (store-front
and admin portlets)
Ref : http://www.konakart.com/docs/LiferayPortletInstallation.pdf
Out of the box portlets from Konakart only provides SSO for Konakart admin portlet in which it replicate liferay user to KonaKart
according to custom role set from Konakart Admin Portlet. This means if you are logged in to Liferay as admin,you are automatically
logged-in to KonaKart Admin Interface. However for store-front portlet (customer) it doesn’t provide such mechanism.
If a customer is logged in to Liferay, they still need to enter credential for login to KonaKart to complete the checkout.
This was not acceptable from user experience point of view and we needed a way to replicate liferay user to KonaKart.
We customized Konakart code to achieve such functionality.
Use-case scenario we covered :
- When user come across store-front portlet,
- If user is already logged-in to liferay then user will auto-login to KonaKart if user already registered with KonaKart
- If user is not registered with KonaKart, then a new KonaKart account created based on user’s profile in Liferay & user will auto-login to KonaKart
Implementation Details
To implement above use-case you need to make changes in “ BaseAction ” java file. Add your custom code in “ loggedIn ” method.
- LoggedIn method will check whether user is logged in konakart or not. It will return konakart userId if user is logged in.
protected int loggedIn(HttpServletRequest request, HttpServletResponse response,KKAppEng kkAppEng, String forwardAfterLogin)
throws KKException, KKAppException {
// check if User Logged in LR
User liferayUser = getLoggedInLRUser(request, response);
// if user not logged in LR then return -1
if (liferayUser == null) {
return -1;
}
if ((liferayUser.getEmailAddress() == null) || (liferayUser.getEmailAddress().length() == 0)) {
return -1;
}
CustomerIf customerInKK = getCustomerFromKKByEmailId(liferayUser.getEmailAddress(), kkAppEng);
if (customerInKK == null) {
int custId = -1;
custId = registerCustomerInKK(liferayUser, kkAppEng);
if (custId < 0) {
return -1;
}
customerInKK = getCustomerFromKKById(custId, kkAppEng);
}
forceLoginInKK(kkAppEng, customerInKK);
return loggedIn(request, response, kkAppEng, forwardAfterLogin, /* checkXSRF */true, /* xsrfToken */null);
}
- This method will check whether user is logged in liferay or not. It will return liferay user profile if user is logged in liferay.
private User getLoggedInLRUser(HttpServletRequest request, HttpServletResponse response) {
Principal localPrincipal = request.getUserPrincipal();
User tempLRUser = null;
if(localPrincipal == null) {
return tempLRUser;
}
tempLRUser = getUserProfile(localPrincipal.getName());
return tempLRUser;
}
private User getUserProfile(String userId) {
User localUser = null;
if (userId == null) {
return localUser;
}
try {
localUser = UserLocalServiceUtil.getUser(Long.parseLong(userId));
} catch (NumberFormatException e) {
log.debug(e.getMessage(), e);
} catch (PortalException e) {
log.debug(e.getMessage(), e);
} catch (SystemException e) {
log.debug(e.getMessage(), e);
}
if ((localUser == null) && (log.isDebugEnabled())) {
log.warn("User with id " + userId + " could not be found");
}
return localUser;
}
- This method will return customer based on user email address.
private CustomerIf getCustomerFromKKByEmailId(String userEmailAddress, KKAppEng kkAppEng) {
KKEngIf engIf = null;
CustomerIf cust = null;
try {
engIf = kkAppEng.getEng();
MgrFactory mgrFactory = new MgrFactory(engIf);
CustomerMgrIf mgr = mgrFactory.getCustMgr(true);
cust = mgr.getCustomerForEmail(userEmailAddress);
} catch (Exception e) {
log.debug(e.getMessage(), e);
}
return cust;
}
- This method will register/create user in konakart if user does not exist in konakart.
private int registerCustomerInKK(User liferayUser, KKAppEng kkAppEng) {
int tempCustId = -1;
CustomerRegistrationIf cr = new CustomerRegistration();
try {
fillUpKKCustomerRegistration(liferayUser, cr, kkAppEng);
tempCustId = kkAppEng.getEng().registerCustomer(cr);
} catch (KKException e) {
log.debug(e.getMessage(), e);
} catch (PortalException e) {
log.debug(e.getMessage(), e);
} catch (SystemException e) {
log.debug(e.getMessage(), e);
}
return tempCustId;
}
- This method will fill user details in konakart db.
private void fillUpKKCustomerRegistration(User liferayUser, CustomerRegistrationIf cr, KKAppEng kkAppEng)
throws PortalException, SystemException, KKException {
cr.setFirstName(liferayUser.getFirstName());
cr.setLastName(liferayUser.getLastName() == null ? "" : liferayUser.getLastName());
cr.setGender(liferayUser.isMale() == true ? "m" : "f");
Calendar calendar = new GregorianCalendar();
Date bDay = liferayUser.getBirthday();
if(bDay != null){
calendar.setTime(bDay);
}
cr.setBirthDate(calendar);
cr.setEmailAddr(liferayUser.getEmailAdress());
cr.setStreetAddress("23 Halden Close");
cr.setCity("Newcastle");
cr.setPostcode("ST5 ORT");
cr.setTelephoneNumber("9825098250");
cr.setPassword("password");
/* Optional attributes */
cr.setCompany("ABC Ltd.");
// Country Id needs to be set with the id of a valid country from the Countries table
cr.setCountryId(getCountryIdByName("United Kingdom", kkAppEng));
cr.setFaxNumber("01782 639707");
// Newsletter should be set to "0" (don't receive) or "1" (receive)
cr.setNewsletter("0");
cr.setSuburb("May Bank");
cr.setState("Staff");
}
- This method will return customer/user object based on customer/user konakart id.
private CustomerIf getCustomerFromKKById(int custId, KKAppEng kkAppEng) {
KKEngIf engIf = null;
CustomerIf cust = null;
try {
engIf = kkAppEng.getEng();
MgrFactory mgrFactory = new MgrFactory(engIf);
CustomerMgrIf mgr = mgrFactory.getCustMgr(true);
cust = mgr.getCustomerForId(custId);
} catch (Exception e) {
log.debug(e.getMessage(), e);
}
return cust;
}
- This method will login user if user is not logged in konakart.
private void forceLoginInKK(KKAppEng kkAppEng, CustomerIf customerInKK) throws KKException, KKAppException {
String sessionId = kkAppEng.getCustomerMgr().login(customerInKK.getEmailAddr(), "password");
kkAppEng.setSessionId(sessionId);
}
Download :
BaseAction.java
BaseAction.class