1 2 package org.roller.presentation.website.actions; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.struts.action.ActionError; 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.roller.RollerException; 14 import org.roller.model.UserManager; 15 import org.roller.pojos.UserData; 16 import org.roller.presentation.MainPageAction; 17 import org.roller.presentation.RollerContext; 18 import org.roller.presentation.RollerRequest; 19 import org.roller.presentation.pagecache.PageCacheFilter; 20 import org.roller.presentation.website.formbeans.UserFormEx; 21 import org.roller.util.StringUtils; 22 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.roller.config.RollerRuntimeConfig; 32 33 43 public class UserNewAction extends UserBaseAction 44 { 45 private static Log mLogger = 46 LogFactory.getFactory().getInstance(UserNewAction.class); 47 48 50 public ActionForward createUser( 51 ActionMapping mapping, 52 ActionForm actionForm, 53 HttpServletRequest request, 54 HttpServletResponse response) 55 throws IOException , ServletException 56 { 57 UserFormEx userForm = (UserFormEx)actionForm; 58 userForm.setAdminCreated(true); 59 return registerUser(mapping, actionForm, request, response); 60 } 61 62 64 public ActionForward registerUser( 65 ActionMapping mapping, 66 ActionForm actionForm, 67 HttpServletRequest request, 68 HttpServletResponse response) 69 throws IOException , ServletException 70 { 71 ActionForward forward = mapping.findForward("registerUser.page"); 72 ActionErrors errors = new ActionErrors(); 73 RollerRequest rreq = RollerRequest.getRollerRequest(request); 74 try 75 { 76 UserFormEx userForm = (UserFormEx)actionForm; 77 loadRequestObjects(request, rreq, null, userForm); 78 userForm.setLocale(request.getLocale().toString()); 79 80 userForm.setPasswordText(null); 82 userForm.setPasswordConfirm(null); 83 } 84 catch (Exception e) 85 { 86 errors.add(ActionErrors.GLOBAL_ERROR, 87 new ActionError("error.editing.user", e.toString())); 88 mLogger.error("ERROR in newUser", e); 89 } 90 return forward; 91 } 92 93 95 public ActionForward add( 96 ActionMapping mapping, 97 ActionForm actionForm, 98 HttpServletRequest request, 99 HttpServletResponse response) 100 throws IOException , ServletException 101 { 102 UserFormEx form = (UserFormEx)actionForm; 103 RollerRequest rreq = RollerRequest.getRollerRequest(request); 104 ServletContext ctx = rreq.getServletContext(); 105 RollerContext rollerContext = RollerContext.getRollerContext(ctx); 106 107 boolean reg_allowed = 108 RollerRuntimeConfig.getBooleanProperty("users.registration.enabled"); 109 110 if ( !reg_allowed && !request.isUserInRole("admin")) 111 { 112 throw new ServletException ("New users disabled!"); 113 } 114 115 ActionMessages msgs = new ActionMessages(); 116 ActionMessages errors = validate(form, new ActionErrors()); 117 if (!errors.isEmpty()) 118 { 119 saveErrors(request, errors); 120 } 121 else try 122 { 123 UserManager mgr = rreq.getRoller().getUserManager(); 125 126 rreq.getRoller().setUser(UserData.SYSTEM_USER); 128 129 UserData ud = new UserData(); 130 form.copyTo(ud, request.getLocale()); ud.setId(null); 132 ud.setDateCreated(new java.util.Date ()); 133 134 if ( !StringUtils.isEmpty(form.getPasswordText()) 136 && !StringUtils.isEmpty(form.getPasswordConfirm())) 137 { 138 ud.resetPassword(rreq.getRoller(), 139 form.getPasswordText(), form.getPasswordConfirm()); 140 } 141 142 String theme = form.getTheme(); 143 HashMap pages = rollerContext.readThemeMacros(theme); 144 mgr.addUser( ud, pages, theme, form.getLocale(), form.getTimezone() ); 145 rreq.getRoller().commit(); 146 147 PageCacheFilter.removeFromCache( request, ud ); 149 MainPageAction.flushMainPageCache(); 150 151 if (form.getAdminCreated()) 152 { 153 msgs.add(ActionMessages.GLOBAL_MESSAGE, 155 new ActionMessage("newUser.created")); 156 saveMessages(request, msgs); 157 form.reset(mapping, request); 158 return createUser(mapping, actionForm, request, response); 159 } 160 else 161 { 162 String weblogURL = rollerContext.getAbsoluteContextUrl(request) 164 + "/page/"+ud.getUserName(); 165 request.setAttribute("weblogURL",weblogURL); 166 String rssURL = rollerContext.getAbsoluteContextUrl(request) 167 + "/rss/"+ud.getUserName(); 168 request.setAttribute("rssURL",rssURL); 169 request.setAttribute("contextURL", 170 rollerContext.getAbsoluteContextUrl(request)); 171 return mapping.findForward("welcome.page"); 172 } 173 } 174 catch (RollerException e) 175 { 176 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getMessage())); 177 saveErrors(request,errors); 178 mLogger.error("ERROR in addUser", e); 179 form.setUserName(""); 180 } 181 182 if (form.getAdminCreated()) 183 { 184 return mapping.findForward("createUser"); 185 } 186 else 187 { 188 return mapping.findForward("registerUser"); 190 } 191 } 192 193 195 protected ActionMessages validate( UserFormEx form, ActionMessages errors ) { 196 super.validate(form, errors); 197 if ( StringUtils.isEmpty(form.getPasswordText()) 198 && StringUtils.isEmpty(form.getPasswordConfirm())) 199 { 200 errors.add( ActionErrors.GLOBAL_ERROR, 201 new ActionError("error.add.user.missingPassword")); 202 } 203 return errors; 204 } 205 } | Popular Tags |