1 18 19 package org.apache.roller.ui.core.struts.actions; 20 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 import java.util.Locale ; 24 import java.util.TimeZone ; 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.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.struts.action.ActionError; 34 import org.apache.struts.action.ActionErrors; 35 import org.apache.struts.action.ActionForm; 36 import org.apache.struts.action.ActionForward; 37 import org.apache.struts.action.ActionMapping; 38 import org.apache.struts.action.ActionMessage; 39 import org.apache.struts.action.ActionMessages; 40 import org.apache.roller.RollerException; 41 import org.apache.roller.config.RollerConfig; 42 import org.apache.roller.config.RollerRuntimeConfig; 43 import org.apache.roller.model.RollerFactory; 44 import org.apache.roller.model.UserManager; 45 import org.apache.roller.pojos.UserData; 46 import org.apache.roller.ui.core.BasePageModel; 47 import org.apache.roller.ui.core.RollerContext; 48 import org.apache.roller.ui.core.RollerRequest; 49 import org.apache.roller.ui.core.security.CustomUserRegistry; 50 import org.apache.roller.ui.authoring.struts.formbeans.UserFormEx; 51 import org.apache.commons.lang.StringUtils; 52 53 54 63 public class UserNewAction extends UserBaseAction { 64 65 private static Log mLogger = LogFactory.getLog(UserNewAction.class); 66 67 68 69 public ActionForward createUser( 70 ActionMapping mapping, 71 ActionForm actionForm, 72 HttpServletRequest request, 73 HttpServletResponse response) 74 throws IOException , ServletException { 75 UserFormEx userForm = (UserFormEx)actionForm; 76 userForm.setAdminCreated(true); 77 return registerUser(mapping, actionForm, request, response); 78 } 79 80 81 public ActionForward cancel( 82 ActionMapping mapping, 83 ActionForm actionForm, 84 HttpServletRequest request, 85 HttpServletResponse response) 86 throws Exception { 87 return mapping.findForward("main"); 88 } 89 90 91 92 public ActionForward registerUser( 93 ActionMapping mapping, 94 ActionForm actionForm, 95 HttpServletRequest request, 96 HttpServletResponse response) 97 throws IOException , ServletException { 98 ActionForward forward = mapping.findForward("registerUser.page"); 99 ActionErrors errors = new ActionErrors(); 100 RollerRequest rreq = RollerRequest.getRollerRequest(request); 101 try { 102 UserFormEx userForm = (UserFormEx)actionForm; 103 104 userForm.setLocale(Locale.getDefault().toString()); 105 userForm.setTimeZone(TimeZone.getDefault().getID()); 106 userForm.setDataFromSSO(false); 107 108 boolean usingSSO = RollerConfig.getBooleanProperty("users.sso.enabled"); 111 if(usingSSO) { 112 UserData fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication(); 113 if(fromSSO != null) { 114 userForm.copyFrom(fromSSO, request.getLocale()); 115 userForm.setDataFromSSO(true); 116 } 117 } 118 119 userForm.setPasswordText(null); 120 userForm.setPasswordConfirm(null); 121 request.setAttribute("model", new BasePageModel( 122 "newUser.addNewUser", request, response, mapping)); 123 } catch (Exception e) { 124 errors.add(ActionErrors.GLOBAL_ERROR, 125 new ActionError("error.editing.user", e.toString())); 126 mLogger.error("ERROR in newUser", e); 127 } 128 return forward; 129 } 130 131 132 133 public ActionForward add( 134 ActionMapping mapping, 135 ActionForm actionForm, 136 HttpServletRequest request, 137 HttpServletResponse response) 138 throws IOException , ServletException { 139 UserFormEx form = (UserFormEx)actionForm; 140 RollerRequest rreq = RollerRequest.getRollerRequest(request); 141 ServletContext ctx = rreq.getServletContext(); 142 RollerContext rollerContext = RollerContext.getRollerContext(); 143 144 boolean reg_allowed = 145 RollerRuntimeConfig.getBooleanProperty("users.registration.enabled"); 146 147 if ( !reg_allowed && !request.isUserInRole("admin")) { 148 throw new ServletException ("New users disabled!"); 149 } 150 151 ActionMessages msgs = new ActionMessages(); 152 ActionMessages errors = validate(form, new ActionErrors()); 153 if (!errors.isEmpty()) { 154 saveErrors(request, errors); 155 } else try { 156 UserManager mgr = RollerFactory.getRoller().getUserManager(); 158 159 UserData ud = new UserData(); 160 form.copyTo(ud, request.getLocale()); ud.setId(null); 162 ud.setDateCreated(new java.util.Date ()); 163 ud.setEnabled(Boolean.TRUE); 164 165 if ( !StringUtils.isEmpty(form.getPasswordText()) 167 && !StringUtils.isEmpty(form.getPasswordConfirm())) { 168 ud.resetPassword(RollerFactory.getRoller(), 169 form.getPasswordText(), form.getPasswordConfirm()); 170 } 171 172 mgr.addUser(ud); 174 RollerFactory.getRoller().flush(); 175 176 if (form.getAdminCreated()) { 177 msgs.add(ActionMessages.GLOBAL_MESSAGE, 179 new ActionMessage("newUser.created")); 180 saveMessages(request, msgs); 181 form.reset(mapping, request); 182 return createUser(mapping, actionForm, request, response); 183 } else { 184 request.setAttribute("contextURL", 186 RollerRuntimeConfig.getAbsoluteContextURL()); 187 return mapping.findForward("welcome.page"); 188 } 189 } catch (RollerException e) { 190 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getMessage())); 191 saveErrors(request,errors); 192 mLogger.error("ERROR in addUser", e); 193 form.setUserName(""); 194 } 195 196 if (form.getAdminCreated()) { 197 return mapping.findForward("createUser"); 198 } else { 199 return mapping.findForward("registerUser"); 201 } 202 } 203 204 205 206 protected ActionMessages validate( UserFormEx form, ActionMessages errors ) { 207 208 form.setDataFromSSO(false); 210 boolean usingSSO = RollerConfig.getBooleanProperty("users.sso.enabled"); 211 if(usingSSO) { 212 boolean storePassword = RollerConfig.getBooleanProperty("users.sso.passwords.saveInRollerDb"); 213 UserData fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication(); 214 if(fromSSO != null) { 215 String password = RollerConfig.getProperty("users.sso.passwords.defaultValue", "<unknown>"); 216 if(storePassword) { 217 password = fromSSO.getPassword(); 218 } 219 form.setPasswordText(password); 220 form.setPasswordConfirm(password); 221 form.setUserName(fromSSO.getUserName()); 222 form.setDataFromSSO(true); 223 } 224 } 225 226 super.validate(form, errors); 227 if ( StringUtils.isEmpty(form.getPasswordText()) 228 && StringUtils.isEmpty(form.getPasswordConfirm())) { 229 errors.add( ActionErrors.GLOBAL_ERROR, 230 new ActionError("error.add.user.missingPassword")); 231 } 232 return errors; 233 } 234 235 } | Popular Tags |