1 20 21 package com.methodhead.reg; 22 23 import org.apache.struts.action.Action; 24 import org.apache.struts.action.ActionMapping; 25 import org.apache.struts.action.ActionForm; 26 import org.apache.struts.action.DynaActionForm; 27 import org.apache.struts.action.ActionForward; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import com.methodhead.aikp.AikpAction; 32 import com.methodhead.aikp.AutoIntKeyPersistable; 33 import com.methodhead.aikp.IntKey; 34 import com.methodhead.util.OperationContext; 35 import com.methodhead.sitecontext.SiteContext; 36 import com.methodhead.util.StrutsUtil; 37 import com.methodhead.event.Event; 38 import com.methodhead.auth.AuthUser; 39 import com.methodhead.auth.AuthUtil; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Collections ; 43 import org.apache.commons.lang.StringUtils; 44 45 public class UserAction 46 extends 47 AikpAction { 48 49 51 53 55 57 protected AutoIntKeyPersistable createPersistable( 58 OperationContext op ) { 59 return new User(); 60 } 61 62 protected void populateForm( 63 DynaActionForm form, 64 AutoIntKeyPersistable persistable ) { 65 66 User user = ( User )persistable; 67 68 form.set( "id", "" + user.getInt( "id" ) ); 69 form.set( "firstname", user.getContact().getString( "firstname" ) ); 70 form.set( "middlename", user.getContact().getString( "middlename" ) ); 71 form.set( "lastname", user.getContact().getString( "lastname" ) ); 72 form.set( "company", user.getContact().getString( "company" ) ); 73 form.set( "address1", user.getContact().getString( "address1" ) ); 74 form.set( "address2", user.getContact().getString( "address2" ) ); 75 form.set( "city", user.getContact().getString( "city" ) ); 76 form.set( "state", user.getContact().getString( "state" ) ); 77 form.set( "zip", user.getContact().getString( "zip" ) ); 78 form.set( "country", user.getContact().getString( "country" ) ); 79 form.set( "phone", user.getContact().getString( "phone" ) ); 80 form.set( "fax", user.getContact().getString( "fax" ) ); 81 form.set( "email", user.getContact().getString( "email" ) ); 82 form.set( "url", user.getContact().getString( "url" ) ); 83 form.set( "password", "" ); 84 form.set( "verifypassword", "" ); 85 } 86 87 protected void populatePersistable( 88 AutoIntKeyPersistable persistable, 89 DynaActionForm form ) { 90 91 User user = ( User )persistable; 92 93 user.getContact().setString( 94 "firstname", ( String )form.get( "firstname" ) ); 95 user.getContact().setString( 96 "middlename", ( String )form.get( "middlename" ) ); 97 user.getContact().setString( 98 "lastname", ( String )form.get( "lastname" ) ); 99 user.getContact().setString( 100 "company", ( String )form.get( "company" ) ); 101 user.getContact().setString( 102 "address1", ( String )form.get( "address1" ) ); 103 user.getContact().setString( 104 "address2", ( String )form.get( "address2" ) ); 105 user.getContact().setString( 106 "city", ( String )form.get( "city" ) ); 107 user.getContact().setString( 108 "state", ( String )form.get( "state" ) ); 109 user.getContact().setString( 110 "zip", ( String )form.get( "zip" ) ); 111 user.getContact().setString( 112 "country", ( String )form.get( "country" ) ); 113 user.getContact().setString( 114 "phone", ( String )form.get( "phone" ) ); 115 user.getContact().setString( 116 "fax", ( String )form.get( "fax" ) ); 117 user.getContact().setString( 118 "email", ( String )form.get( "email" ) ); 119 user.getContact().setString( 120 "url", ( String )form.get( "url" ) ); 121 122 if ( !StringUtils.isBlank( ( String )form.get( "password" ) ) ) 123 user.setString( "password", ( String )form.get( "password" ) ); 124 } 125 126 protected ActionForward getForwardForSave( 127 OperationContext op, 128 Object policy ) { 129 130 return new ActionForward( "/user.do?action=list" ); 131 } 132 133 protected ActionForward getForwardForDelete( 134 OperationContext op, 135 Object policy ) { 136 137 return new ActionForward( "/user.do?action=list" ); 138 } 139 140 protected ActionForward doCancelSave( 141 OperationContext op, 142 Object policy ) { 143 144 return new ActionForward( "/user.do?action=list" ); 145 } 146 147 protected ActionForward doList( 148 OperationContext op, 149 Object policy ) { 150 151 String msg = ( ( RegPolicy )policy ).isListUsersAuthorized( op ); 152 153 if ( msg != null ) { 154 StrutsUtil.addMessage( 155 op.request, msg, null, null, null ); 156 return op.mapping.findForward( "accessDenied" ); 157 } 158 159 ActionForward forward = super.doList( op, policy ); 160 161 List list = ( List )op.form.get( "list" ); 162 163 for ( Iterator iter = ( list ).iterator(); iter.hasNext(); ) { 167 User u = ( User )iter.next(); 168 u.load( new IntKey( u.getInt( "id" ) ) ); 169 } 170 171 Collections.sort( list ); 175 176 return forward; 177 } 178 179 protected ActionForward doDelete( 180 OperationContext op, 181 Object policy ) { 182 183 String msg = ( ( RegPolicy )policy ).isDeleteUserAuthorized( op ); 184 185 if ( msg != null ) { 186 StrutsUtil.addMessage( 187 op.request, msg, null, null, null ); 188 return op.mapping.findForward( "accessDenied" ); 189 } 190 191 String email = 192 StringUtils.defaultString( 193 op.request.getParameter( "email" ), "" + op.form.get( "id" ) ); 194 195 ActionForward forward = super.doDelete( op, policy ); 196 197 Event.log( 198 SiteContext.getDefaultContext(), 199 op.user.getLogin(), 200 email, 201 "User deleted." ); 202 203 return forward; 204 } 205 206 protected ActionForward doSave( 207 OperationContext op, 208 Object policy ) { 209 210 String msg = ( ( RegPolicy )policy ).isSaveUserAuthorized( op ); 211 212 if ( msg != null ) { 213 StrutsUtil.addMessage( 214 op.request, msg, null, null, null ); 215 return op.mapping.findForward( "accessDenied" ); 216 } 217 218 ActionForward forward = super.doSave( op, policy ); 219 220 Event.log( 221 SiteContext.getDefaultContext(), 222 op.user.getLogin(), ( String )op.form.get( "email" ), "User updated." ); 223 224 return forward; 225 } 226 227 protected ActionForward doSaveNew( 228 OperationContext op, 229 Object policy ) { 230 231 String msg = ( ( RegPolicy )policy ).isSaveNewUserAuthorized( op ); 232 233 if ( msg != null ) { 234 StrutsUtil.addMessage( 235 op.request, msg, null, null, null ); 236 return op.mapping.findForward( "accessDenied" ); 237 } 238 239 ActionForward forward = super.doSaveNew( op, policy ); 240 241 Event.log( 242 SiteContext.getDefaultContext(), 243 op.user.getLogin(), ( String )op.form.get( "email" ), "User created." ); 244 245 return forward; 246 } 247 248 protected ActionForward doEdit( 249 OperationContext op, 250 Object policy ) { 251 252 String msg = ( ( RegPolicy )policy ).isEditUserAuthorized( op ); 253 254 if ( msg != null ) { 255 StrutsUtil.addMessage( 256 op.request, msg, null, null, null ); 257 return op.mapping.findForward( "accessDenied" ); 258 } 259 260 return super.doEdit( op, policy ); 261 } 262 263 protected ActionForward doNew( 264 OperationContext op, 265 Object policy ) { 266 267 String msg = ( ( RegPolicy )policy ).isNewUserAuthorized( op ); 268 269 if ( msg != null ) { 270 StrutsUtil.addMessage( 271 op.request, msg, null, null, null ); 272 return op.mapping.findForward( "accessDenied" ); 273 } 274 275 return super.doNew( op, policy ); 276 } 277 278 280 } 282 | Popular Tags |