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 import org.apache.commons.lang.StringUtils; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import com.methodhead.auth.AuthUtil; 34 import com.methodhead.auth.AuthUser; 35 import com.methodhead.auth.AuthAction; 36 import com.methodhead.util.OperationContext; 37 import com.methodhead.util.StrutsUtil; 38 import com.methodhead.aikp.IntKey; 39 import com.methodhead.sitecontext.SiteContext; 40 import com.methodhead.event.Event; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 import java.util.Iterator ; 44 45 public class RolesAction 46 extends 47 AuthAction { 48 49 51 53 55 57 protected ActionForward doRolesForm( 58 OperationContext op, 59 RegPolicy policy ) { 60 61 String msg = policy.isRolesFormAuthorized( op ); 65 if ( msg != null ) { 66 StrutsUtil.addMessage( op.request, msg, null, null, null ); 67 return op.mapping.findForward( "accessDenied" ); 68 } 69 70 return new ActionForward( op.mapping.getInput() ); 71 } 72 73 protected ActionForward doRoles( 74 OperationContext op, 75 RegPolicy policy ) { 76 77 String msg = policy.isRolesAuthorized( op ); 81 if ( msg != null ) { 82 StrutsUtil.addMessage( op.request, msg, null, null, null ); 83 return op.mapping.findForward( "accessDenied" ); 84 } 85 86 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) { 90 91 return new ActionForward( "/user.do?action=edit&id=" + op.form.get( "userid" ) ); 95 } 96 97 User user = policy.newRegUser(); 101 user.load( new IntKey( op.form.get( "userid" ) ) ); 102 103 SiteContext siteContext = new SiteContext(); 107 siteContext.load( new IntKey( op.form.get( "siteid" ) ) ); 108 109 if ( StringUtils.isNotBlank( ( String )op.form.get( "select" ) ) ) { 113 114 List roles = new ArrayList (); 118 for ( Iterator iter = user.getRoles().iterator(); iter.hasNext(); ) { 119 Role role = ( Role )iter.next(); 120 121 if ( siteContext.equals( role.getSiteContext() ) ) 122 roles.add( role.getName() ); 123 } 124 125 op.form.set( "roles", roles.toArray( new String [ roles.size() ] ) ); 126 127 return new ActionForward( op.mapping.getInput() ); 131 } 132 133 for ( Iterator iter = user.getRoles().iterator(); iter.hasNext(); ) { 137 Role role = ( Role )iter.next(); 138 139 if ( role.getSiteContext().equals( siteContext ) ) 140 iter.remove(); 141 } 142 143 String [] roles = ( String [] )op.form.get( "roles" ); 147 for ( int i = 0; i < roles.length; i++ ) { 148 Role role = new Role(); 149 role.setSiteContext( siteContext ); 150 role.setName( roles[ i ] ); 151 152 user.getRoles().add( role ); 153 } 154 155 user.save(); 159 160 Event.log( 164 SiteContext.getDefaultContext(), 165 op.user.getLogin(), 166 "reg", 167 "Updated roles for " + user.getLogin() + " on " + siteContext + "." ); 168 169 return new ActionForward( 173 "/user.do?action=edit&id=" + user.getInt( "id" ) ); 174 } 175 176 public ActionForward doExecute( 177 ActionMapping mapping, 178 ActionForm form, 179 HttpServletRequest request, 180 HttpServletResponse response ) 181 throws 182 Exception { 183 184 DynaActionForm dynaForm = ( DynaActionForm )form; 188 RegPolicy policy = ( RegPolicy )StrutsUtil.getPolicy( mapping ); 189 AuthUser user = AuthUtil.getUser( request ); 190 191 OperationContext op = 192 new OperationContext( mapping, dynaForm, request, response, user ); 193 194 if ( mapping.getPath().equals( "/rolesForm" ) ) { 198 return doRolesForm( op, policy ); 199 } 200 if ( mapping.getPath().equals( "/roles" ) ) { 201 return doRoles( op, policy ); 202 } 203 204 throw 205 new Exception ( "Unexpected mapping path \"" + mapping.getPath() + "\"" ); 206 } 207 208 210 } 212 | Popular Tags |