1 package com.dotmarketing.portlets.usermanager.action; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.util.Date ; 5 import java.util.List ; 6 7 import javax.portlet.ActionRequest; 8 import javax.portlet.ActionResponse; 9 import javax.portlet.PortletConfig; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpSession ; 12 13 import org.apache.commons.beanutils.BeanUtils; 14 import org.apache.struts.Globals; 15 import org.apache.struts.action.ActionErrors; 16 import org.apache.struts.action.ActionForm; 17 import org.apache.struts.action.ActionMapping; 18 import org.apache.struts.action.ActionMessage; 19 import org.apache.struts.action.ActionMessages; 20 21 import com.dotmarketing.beans.Host; 22 import com.dotmarketing.beans.UserProxy; 23 import com.dotmarketing.cms.factories.PublicAddressFactory; 24 import com.dotmarketing.cms.factories.PublicEncryptionFactory; 25 import com.dotmarketing.cms.factories.PublicUserFactory; 26 import com.dotmarketing.db.DotHibernate; 27 import com.dotmarketing.factories.EmailFactory; 28 import com.dotmarketing.factories.HostFactory; 29 import com.dotmarketing.factories.InodeFactory; 30 import com.dotmarketing.factories.UserProxyFactory; 31 import com.dotmarketing.portal.struts.DotPortletAction; 32 import com.dotmarketing.portlets.categories.factories.CategoryFactory; 33 import com.dotmarketing.portlets.categories.model.Category; 34 import com.dotmarketing.portlets.usermanager.struts.UserManagerForm; 35 import com.dotmarketing.portlets.usermanager.struts.UserManagerListSearchForm; 36 import com.dotmarketing.util.Logger; 37 import com.dotmarketing.util.UtilMethods; 38 import com.dotmarketing.util.Validator; 39 import com.dotmarketing.util.WebKeys; 40 import com.liferay.portal.ejb.UserLocalManagerUtil; 41 import com.liferay.portal.model.Address; 42 import com.liferay.portal.model.User; 43 import com.liferay.portlet.ActionRequestImpl; 44 import com.liferay.util.servlet.SessionMessages; 45 46 49 public class RegisterUserAction extends DotPortletAction{ 50 51 public static boolean debug = false; 52 53 public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req, 54 ActionResponse res) throws Exception { 55 56 DotHibernate.startTransaction(); 57 String cmd = req.getParameter(com.liferay.portal.util.Constants.CMD); 58 String referer = req.getParameter("referer"); 59 60 req.setAttribute(WebKeys.USERMANAGER_EDIT_FORM, form); 61 62 if (com.liferay.portal.util.Constants.SAVE.equals(cmd)) 64 { 65 Logger.debug(this, "Saving UserInfo"); 66 67 if (!Validator.validate(req, form, mapping)) { 69 Logger.debug(this, "Form Validation Failed"); 70 req.setAttribute(WebKeys.USERMANAGER_EDIT_FORM, form); 71 } else { 72 try { 73 String userId = _save(form, req, res); 74 if (UtilMethods.isSet(referer) && UtilMethods.isSet(userId)) { 75 referer += "&userId=" + userId; 76 Logger.debug("After registering user going to:" + referer); 77 _sendToReferral(req,res,referer); 78 return; 79 } 80 req.setAttribute(WebKeys.USERMANAGER_EDIT_FORM, form); 81 } catch (Exception e) { 82 _handleException(e, req); 83 } 84 } 85 } else if (cmd != null && cmd.equals("updateUserProxy")) 86 { 87 try { 88 _updateUserProxy(form, req, res); 89 _sendToReferral(req,res,referer); 90 } catch (Exception e) { 91 _handleException(e, req); 92 } 93 } else if(UtilMethods.isSet(cmd) && cmd.equals("forgotPassword")) 94 { 95 _forgotPassword(form, req, res); 96 } 97 setForward(req, "portlet.ext.usermanager.register_user"); 98 DotHibernate.commitTransaction(); 99 } 100 101 102 103 104 private Address retrieveAddress(String userID) throws Exception { 105 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 106 107 Address address = null; 108 if (UtilMethods.isSet(userID)) { 109 List addresses = PublicAddressFactory.getAddressesByUserId(userID); 110 if (addresses.size() == 0) { 111 address = PublicAddressFactory.getInstance(); 112 address.setCompanyId(companyId); 113 address.setUserId(userID); 114 } else { 115 address = (Address) addresses.get(0); 116 } 117 } else { 118 address = PublicAddressFactory.getInstance(); 119 address.setCompanyId(companyId); 120 address.setUserId(userID); 121 } 122 return address; 123 } 124 125 private void _updateUserProxy(ActionForm form, ActionRequest req, ActionResponse res){ 126 127 UserManagerForm userForm = new UserManagerForm(); 128 try { 129 BeanUtils.copyProperties(userForm,form); 130 131 UserProxy userProxy = UserProxyFactory.getUserProxy(userForm.getUserID()); 132 133 if (!userForm.getPrefix().equals("other")) 134 userProxy.setPrefix(userForm.getPrefix()); 135 else 136 userProxy.setPrefix(userForm.getOtherPrefix()); 137 138 userProxy.setSuffix(userForm.getSuffix()); 139 userProxy.setTitle(userForm.getTitle()); 140 userProxy.setSchool(userForm.getSchool()); 141 userProxy.setGraduation_year(userForm.getGraduation_year()); 142 143 InodeFactory.saveInode(userProxy); 144 145 } catch (IllegalAccessException e) { 146 e.printStackTrace(); 148 } catch (InvocationTargetException e) { 149 e.printStackTrace(); 151 } 152 } 153 private String _save(ActionForm form, ActionRequest req, ActionResponse res) 155 throws Exception { 156 157 UserManagerForm userForm = (UserManagerForm) form; 158 String userId = userForm.getUserID(); 159 160 ActionErrors ae = new ActionErrors (); 161 162 if(!UtilMethods.isSet(userForm.getUserID())) 163 { 164 if(!validateUniqueEmail(userForm.getEmailAddress())) 165 { 166 ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.forgotPasswordClickHere","javascript:forgotPassword();")); 167 } 168 } 169 170 if ((ae != null) && (ae.size() > 0)) 171 { 172 req.setAttribute(Globals.ERROR_KEY,ae); 173 return null; 174 } 175 176 User user = retrieveMember(userId, userForm); 178 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 179 180 long userProxyInode = userProxy.getInode(); 181 String userProxyUserId = userProxy.getUserId(); 182 183 userProxy = (UserProxy) InodeFactory.getInode(userProxyInode, UserProxy.class); 184 185 userProxy.setInode(userProxyInode); 186 userProxy.setUserId(userProxyUserId); 187 188 if (user.isNew()) { 189 userProxy.setUserId(user.getUserId()); 190 } 191 192 user.setFirstName(userForm.getFirstName()); 193 user.setLastName(userForm.getLastName()); 194 if (!userForm.getPrefix().equals("other")) 195 userProxy.setPrefix(userForm.getPrefix()); 196 else 197 userProxy.setPrefix(userForm.getOtherPrefix()); 198 199 userProxy.setSuffix(userForm.getSuffix()); 200 userProxy.setTitle(userForm.getTitle()); 201 userProxy.setSchool(userForm.getSchool()); 202 userProxy.setGraduation_year(userForm.getGraduation_year()); 203 204 if (!UtilMethods.isSet(user.getEmailAddress()) || !user.getEmailAddress().equals(userForm.getEmailAddress())) 206 user.setEmailAddress(userForm.getEmailAddress()); 207 208 if (user.isNew() || userForm.getPassChanged().equals("true")) { 209 user.setPassword(PublicEncryptionFactory.digestString(userForm.getPassword())); 210 user.setPasswordEncrypted(true); 211 } 212 213 PublicUserFactory.save(user); 214 215 InodeFactory.saveInode(userProxy); 216 217 Address address = retrieveAddress(user.getUserId()); 219 220 address.setDescription(userForm.getDescription()); 221 address.setStreet1(userForm.getStreet1()); 222 address.setStreet2(userForm.getStreet2()); 223 address.setCity(userForm.getCity()); 224 address.setState(userForm.getState()); 225 address.setZip(userForm.getZip()); 226 address.setPhone(userForm.getPhone()); 227 address.setFax(userForm.getFax()); 228 address.setCountry(userForm.getCountry()); 229 230 PublicAddressFactory.save(address); 231 232 if(userForm.getCategory() != null){ 234 String [] categories = userForm.getCategory(); 235 for(int i = 0 ; i < categories.length ;++i){ 236 Category cat = CategoryFactory.getCategory(categories[i]); 237 userProxy.addChild(cat); 238 } 239 } 240 241 UserManagerListSearchForm searchForm = new UserManagerListSearchForm(); 242 BeanUtils.copyProperties(searchForm, user); 243 BeanUtils.copyProperties(searchForm, address); 244 BeanUtils.copyProperties(searchForm, userProxy); 245 246 SessionMessages.add(req, "message", "message.usermanager.saved"); 247 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 248 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 249 HttpSession session = httpReq.getSession(); 250 session.setAttribute(WebKeys.USERMANAGERLISTFORM,searchForm); 251 252 return user.getUserId(); 253 } 254 255 private boolean validateUniqueEmail(String emailAddress) 256 { 257 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 258 boolean returnValue = true; 259 User member = null; 260 if (UtilMethods.isSet(emailAddress)) 261 { 262 try 263 { 264 member = UserLocalManagerUtil.getUserByEmailAddress(companyId,emailAddress); 265 } 266 catch(Exception ex) 267 { 268 Logger.debug(this,ex.toString()); 269 } 270 } 271 if(!(member == null)) 272 { 273 returnValue = false; 274 } 275 return returnValue; 276 } 277 278 private User retrieveMember(String userId, UserManagerForm form) throws Exception 279 { 280 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 281 User member = new User(); 282 283 if (UtilMethods.isSet(userId)) 284 { 285 member = UserLocalManagerUtil.getUserById(companyId, userId); 286 } 287 else 288 { 289 member = PublicUserFactory.getInstance(); 290 member.setCompanyId(companyId); 291 member.setActive(true); 292 member.setSkinId("01"); 293 member.setCreateDate(new Date ()); 294 member.setGreeting("Welcome, "+ form.getFirstName()+" "+ form.getLastName()+"!"); 295 member.setMiddleName(""); 296 member.setNickName(""); 297 member.setNew(true); 298 member.setLanguageId("en_US"); 299 member.setTimeZoneId("US/Eastern"); 300 member.setResolution("1024x768"); 301 member.setRefreshRate("900"); 302 member.setLayoutIds(""); 303 } 304 return member; 305 } 306 307 private void _forgotPassword(ActionForm form, ActionRequest request, ActionResponse res) throws Exception 308 { 309 UserManagerForm userForm = (UserManagerForm) form; 310 ActionErrors aes = new ActionErrors(); 312 313 User user = PublicUserFactory.getUserByEmail(userForm.getEmailAddress()); 314 if(user.isNew()) 315 { 316 aes = new ActionErrors(); 317 aes.add(Globals.ERROR_KEY, new ActionMessage("error.forgotPasswordUserNotFound")); 318 request.setAttribute(Globals.ERROR_KEY,aes); 319 return; 320 } 321 String pass = PublicEncryptionFactory.getRandomPassword(); 322 user.setPassword(PublicEncryptionFactory.digestString(pass)); 323 PublicUserFactory.save(user); 324 Host host = HostFactory.getCurrentHost(request); 325 EmailFactory.sendForgotPassword(user, pass, host.getInode()); 326 327 aes = new ActionErrors(); 328 aes.add(Globals.ERROR_KEY, new ActionMessage("error.forgotPasswordMailSend")); 329 request.setAttribute(Globals.ERROR_KEY,aes); 330 return; 331 } 332 } 333 334 335 | Popular Tags |