1 package com.dotmarketing.cms.myaccount.action; 2 3 import javax.servlet.http.HttpServletRequest ; 4 import javax.servlet.http.HttpServletResponse ; 5 6 import org.apache.commons.beanutils.BeanUtils; 7 import org.apache.struts.action.ActionErrors; 8 import org.apache.struts.action.ActionForm; 9 import org.apache.struts.action.ActionForward; 10 import org.apache.struts.action.ActionMapping; 11 import org.apache.struts.action.ActionMessage; 12 import org.apache.struts.action.ActionMessages; 13 import org.apache.struts.actions.DispatchAction; 14 15 import com.dotmarketing.beans.UserProxy; 16 import com.dotmarketing.cms.factories.PublicEncryptionFactory; 17 import com.dotmarketing.cms.factories.PublicUserFactory; 18 import com.dotmarketing.cms.myaccount.struts.MyAccountForm; 19 import com.dotmarketing.factories.InodeFactory; 20 import com.dotmarketing.factories.UserProxyFactory; 21 import com.dotmarketing.util.Mailer; 22 import com.dotmarketing.util.Validator; 23 import com.dotmarketing.util.WebKeys; 24 import com.liferay.portal.model.User; 25 26 27 public class MyAccountAction extends DispatchAction { 28 29 public ActionForward unspecified(ActionMapping mapping, ActionForm lf, 30 HttpServletRequest request, HttpServletResponse response) 31 throws Exception { 32 33 if (request.getSession().getAttribute(WebKeys.CMS_USER) == null) { 34 return new ActionForward("/dotCMS/login"); 35 } 36 37 MyAccountForm form = (MyAccountForm) lf; 39 40 User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER); 42 String userId = user.getUserId(); 43 44 loadUserInfoInRequest(form, userId, request); 45 46 return mapping.findForward("myAccountPage"); 47 } 48 49 public ActionForward saveUserInfo(ActionMapping mapping, ActionForm lf, 50 HttpServletRequest request, HttpServletResponse response) 51 throws Exception { 52 53 if (request.getSession().getAttribute(WebKeys.CMS_USER) == null) { 54 return new ActionForward("/dotCMS/login"); 55 } 56 57 MyAccountForm form = (MyAccountForm) lf; 58 String userEmail = form.getUserName(); 59 60 if (!Validator.validate(request, lf, mapping)) 61 return mapping.findForward("myAccountPage"); 62 63 65 User user = PublicUserFactory.getUserByEmail(userEmail); 66 67 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 68 69 user.setFirstName(form.getFirstName()); 70 user.setLastName(form.getLastName()); 71 72 userProxy.setOrganization(form.getOrganization()); 73 userProxy.setWebsite(form.getWebsite()); 74 userProxy.setMailSubscription(form.isMailSubscription()); 75 76 if (form.isPasswordChanged()) { 78 if (!user.getPassword().equals(PublicEncryptionFactory.digestString(form.getOldpassword()))) 79 { 80 ActionErrors errors = new ActionErrors (); 81 errors.add("password", new ActionMessage ("current.password.incorrect")); 82 saveMessages(request, errors); 83 return mapping.findForward("myAccountPage"); 84 } 85 user.setPassword(PublicEncryptionFactory.digestString(form.getPassword1())); 86 user.setPasswordEncrypted(true); 87 } 88 89 if (form.isMailSubscription()) { 90 subscribeDotCMSMailingList (user); 91 } else { 92 unSubsribeDotCMSMailingList (user); 93 } 94 95 PublicUserFactory.save(user); 96 InodeFactory.saveInode(userProxy); 97 98 loadUserInfoInRequest(form, user.getUserId(), request); 99 100 ActionErrors ae = new ActionErrors(); 101 ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.createaccount.success")); 102 saveMessages(request, ae); 103 104 return mapping.findForward("myAccountPage"); 105 } 106 107 108 109 private void loadUserInfoInRequest(MyAccountForm form, String userId, 110 HttpServletRequest request) throws Exception { 111 112 113 User user = PublicUserFactory.getUserByUserId(userId); 114 115 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 117 118 119 if (userProxy.getInode() == 0) { 120 userProxy.setUserId(user.getUserId()); 121 InodeFactory.saveInode(userProxy); 122 } 123 124 BeanUtils.copyProperties(form, user); 126 127 BeanUtils.copyProperties(form, userProxy); 128 129 130 form.setUserName(user.getEmailAddress()); 132 133 } 134 135 public static boolean subscribeDotCMSMailingList(User user) { 136 String to = "dotcms-subscribe@yahoogroups.com"; 137 String from = user.getEmailAddress(); 138 String subject = "Subscribe to dotCMS mailing list"; 139 return sendEmailForMailingList(to, from, subject); 140 } 141 142 public static boolean unSubsribeDotCMSMailingList(User user) { 143 String to = "dotcms-unsubscribe@yahoogroups.com"; 144 String from = user.getEmailAddress(); 145 String subject = "UnSubscribe to dotCMS mailing list"; 146 return sendEmailForMailingList(to, from, subject); 147 } 148 149 protected static boolean sendEmailForMailingList(String to, String from, String subject) { 150 Mailer m = new Mailer(); 151 m.setToEmail(to); 152 m.setFromEmail(from); 153 m.setSubject(subject); 154 return m.sendMessage(); 155 } 156 157 public ActionForward back(ActionMapping mapping, ActionForm lf, 158 HttpServletRequest request, HttpServletResponse response) 159 throws Exception { 160 161 if (request.getSession().getAttribute(WebKeys.CMS_USER) == null) { 162 return new ActionForward("/dotCMS/login"); 163 } 164 165 MyAccountForm form = (MyAccountForm) lf; 167 168 User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER); 170 String userId = user.getUserId(); 171 172 loadUserInfoInRequest(form, userId, request); 173 174 return mapping.findForward("myAccountPage"); 175 } 176 } 177 178 | Popular Tags |