1 16 17 package org.apache.jetspeed.modules.actions.portlets.security; 18 19 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction; 25 import org.apache.jetspeed.om.security.Group; 26 import org.apache.jetspeed.om.security.JetspeedUser; 27 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 28 import org.apache.jetspeed.services.JetspeedSecurity; 29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 30 import org.apache.jetspeed.services.logging.JetspeedLogger; 31 import org.apache.jetspeed.services.resources.JetspeedResources; 32 import org.apache.turbine.util.DynamicURI; 33 import org.apache.turbine.util.RunData; 34 import org.apache.turbine.util.StringUtils; 35 import org.apache.velocity.context.Context; 36 37 38 45 public class UserGroupUpdateAction extends SecureVelocityPortletAction 46 { 47 50 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserGroupUpdateAction.class.getName()); 51 52 59 protected void buildMaximizedContext( VelocityPortlet portlet, 60 Context context, 61 RunData rundata ) 62 { 63 buildNormalContext( portlet, context, rundata); 64 } 65 66 74 protected void buildConfigureContext( VelocityPortlet portlet, 75 Context context, 76 RunData rundata ) 77 { 78 79 buildNormalContext( portlet, context, rundata); 80 } 81 82 89 protected void buildNormalContext( VelocityPortlet portlet, 90 Context context, 91 RunData rundata ) 92 { 93 try 94 { 95 Group group = null; 96 99 String mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE); 100 101 String entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID); 105 if (entityid == null || entityid.trim().length() == 0) 106 { 107 return; 108 } 109 110 buildUserGroupsContext(portlet, context, rundata, entityid); 111 112 String msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID); 116 if (msgid != null) 117 { 118 int id = Integer.parseInt(msgid); 119 if (id < SecurityConstants.MESSAGES.length) 120 context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]); 121 } 122 123 } 124 catch (Exception e) 125 { 126 logger.error("Error in Jetspeed User Group Security", e); 127 rundata.setMessage("Error in Jetspeed User Group Security: " + e.toString()); 128 rundata.setStackTrace(StringUtils.stackTrace(e), e); 129 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error")); 130 } 131 } 132 133 134 140 public void doUpdate(RunData rundata, Context context) 141 throws Exception 142 { 143 String entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID); 144 if (entityid == null || entityid.trim().length() == 0) 145 { 146 logger.error("UserGroupBrowser: Failed to get entity: " + entityid ); 147 DynamicURI duri = new DynamicURI (rundata); 148 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm"); 149 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER); 150 rundata.setRedirectURI(duri.toString()); 151 return; 152 } 153 154 JetspeedUser user = JetspeedSecurity.getUser(entityid); 155 if (null == user) 156 { 157 logger.error("UserGroupBrowser: Failed to get user: " + entityid ); 158 DynamicURI duri = new DynamicURI (rundata); 159 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm"); 160 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER); 161 rundata.setRedirectURI(duri.toString()); 162 return; 163 } 164 165 166 try 167 { 168 List groups = (List )rundata.getUser().getTemp(SecurityConstants.CONTEXT_GROUPS); 169 List selected = (List )rundata.getUser().getTemp(SecurityConstants.CONTEXT_SELECTED); 170 171 if (groups == null || selected == null) 172 { 173 DynamicURI duri = new DynamicURI (rundata); 174 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm"); 175 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER); 176 rundata.setRedirectURI(duri.toString()); 177 return; 178 } 179 180 for (int ix = 0; ix < groups.size(); ix++) 185 { 186 boolean newValue = rundata.getParameters().getBoolean("box_" + ((Group)groups.get(ix)).getName(), false); 187 boolean oldValue = ((Boolean )selected.get(ix + 1)).booleanValue(); 188 if (newValue != oldValue) 189 { 190 if (newValue == true) 191 { 192 JetspeedSecurity.joinGroup( user.getUserName(), 194 ((Group)groups.get(ix)).getName() ); 195 } 196 else 197 { 198 JetspeedSecurity.unjoinGroup( user.getUserName(), 200 ((Group)groups.get(ix)).getName() ); 201 } 202 } 203 } 204 205 rundata.getUser().setTemp(SecurityConstants.CONTEXT_GROUPS, null); 207 rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, null); 208 209 } 210 catch (Exception e) 211 { 212 logger.error("Failed update role+permission: ", e); 214 215 DynamicURI duri = new DynamicURI (rundata); 219 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm"); 220 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED); 221 if (user != null) 222 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, user.getUserName()); 223 rundata.setRedirectURI(duri.toString()); 224 225 } 226 } 227 228 236 private void buildUserGroupsContext(VelocityPortlet portlet, 237 Context context, 238 RunData rundata, 239 String userid) 240 throws Exception 241 { 242 JetspeedUser user = JetspeedSecurity.getUser(userid); 244 if (null == user) 245 { 246 logger.error("UserGroupBrowser: Failed to get user: " + userid ); 248 return; 249 } 250 Iterator groups = JetspeedSecurity.getGroups(); 252 ArrayList masterGroups = new ArrayList (); 253 ArrayList selected = new ArrayList (); 254 int ix = 0; 255 boolean sel = false; 256 selected.add(ix, new Boolean (sel)); 257 while(groups.hasNext()) 258 { 259 Group group = (Group)groups.next(); 260 masterGroups.add(group); 261 sel = JetspeedSecurity.inGroup(user.getUserName(), group.getName()); 262 ix = ix + 1; 263 selected.add(ix, new Boolean (sel)); 264 } 265 masterGroups.trimToSize(); 266 selected.trimToSize(); 267 268 rundata.getUser().setTemp(SecurityConstants.CONTEXT_GROUPS, masterGroups); 269 rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, selected); 270 context.put(SecurityConstants.CONTEXT_USER, user); 271 context.put(SecurityConstants.CONTEXT_GROUPS, masterGroups); 272 context.put(SecurityConstants.CONTEXT_SELECTED, selected); 273 274 } 275 276 277 } | Popular Tags |