1 16 17 package org.apache.jetspeed.modules.actions.portlets.security; 18 19 import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction; 21 import org.apache.jetspeed.om.security.JetspeedRoleFactory; 22 import org.apache.jetspeed.om.security.Role; 23 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 24 import org.apache.jetspeed.services.JetspeedSecurity; 25 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 26 import org.apache.jetspeed.services.logging.JetspeedLogger; 27 import org.apache.jetspeed.services.resources.JetspeedResources; 28 import org.apache.jetspeed.services.security.RoleException; 29 import org.apache.turbine.util.DynamicURI; 30 import org.apache.turbine.util.RunData; 31 import org.apache.turbine.util.StringUtils; 32 import org.apache.velocity.context.Context; 33 34 35 41 public class RoleUpdateAction extends SecureVelocityPortletAction 42 { 43 private static final String TEMP_ROLE = "tempRole"; 44 45 48 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RoleUpdateAction.class.getName()); 49 50 57 protected void buildMaximizedContext( VelocityPortlet portlet, 58 Context context, 59 RunData rundata ) 60 { 61 buildNormalContext( portlet, context, rundata); 62 } 63 64 72 protected void buildConfigureContext( VelocityPortlet portlet, 73 Context context, 74 RunData rundata ) 75 { 76 77 buildNormalContext( portlet, context, rundata); 78 } 79 80 87 protected void buildNormalContext( VelocityPortlet portlet, 88 Context context, 89 RunData rundata ) 90 { 91 try 92 { 93 Role role = null; 94 95 98 String mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE); 99 100 if (mode != null && (mode.equals(SecurityConstants.PARAM_MODE_UPDATE) || 103 mode.equals(SecurityConstants.PARAM_MODE_DELETE))) 104 { 105 String rolename = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID); 107 role = JetspeedSecurity.getRole(rolename); 108 context.put(SecurityConstants.CONTEXT_ROLE, role); 109 } 110 111 String msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID); 115 if (msgid != null) 116 { 117 int id = Integer.parseInt(msgid); 118 if (id < SecurityConstants.MESSAGES.length) 119 context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]); 120 121 Role tempRole = (Role)rundata.getUser().getTemp(TEMP_ROLE); 123 if (tempRole != null) 124 context.put(SecurityConstants.CONTEXT_ROLE, tempRole); 125 } 126 context.put(SecurityConstants.PARAM_MODE, mode); 127 128 } 129 catch (Exception e) 130 { 131 logger.error("Error in Jetspeed User Security", e); 132 rundata.setMessage("Error in Jetspeed User Security: " + e.toString()); 133 rundata.setStackTrace(StringUtils.stackTrace(e), e); 134 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error")); 135 } 136 } 137 138 144 public void doInsert(RunData rundata, Context context) 145 throws Exception 146 { 147 Role role = null; 148 try 149 { 150 String name = rundata.getParameters().getString("name"); 154 if (name == null || name.trim().length() == 0) 155 { 156 DynamicURI duri = new DynamicURI (rundata); 157 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE); 158 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_INVALID_ENTITY_NAME); 159 rundata.setRedirectURI(duri.toString()); 160 rundata.getUser().setTemp(TEMP_ROLE, null); 161 return; 162 } 163 164 168 role = JetspeedRoleFactory.getInstance(); 169 role.setName(name); 170 171 JetspeedSecurity.addRole(role); 175 176 } 177 catch (RoleException e) 178 { 179 logger.error("Exception", e); 181 182 DynamicURI duri = new DynamicURI (rundata); 186 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE); 187 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_ENTITY_ALREADY_EXISTS); 188 rundata.setRedirectURI(duri.toString()); 189 190 if (role != null) 192 rundata.getUser().setTemp(TEMP_ROLE, role); 193 } 194 } 195 196 202 public void doUpdate(RunData rundata, Context context) 203 throws Exception 204 { 205 Role role = null; 206 try 207 { 208 role = JetspeedSecurity.getRole( 212 rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID)); 213 214 JetspeedSecurity.saveRole(role); 218 219 } 220 catch (Exception e) 221 { 222 logger.error("Exception", e); 224 225 DynamicURI duri = new DynamicURI (rundata); 229 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE); 230 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED); 231 if (role != null) 232 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, role.getName()); 233 duri.addQueryData(SecurityConstants.PARAM_MODE, SecurityConstants.PARAM_MODE_UPDATE); 234 rundata.setRedirectURI(duri.toString()); 235 236 if (role != null) 238 rundata.getUser().setTemp(TEMP_ROLE, role); 239 240 } 241 } 242 243 249 public void doDelete(RunData rundata, Context context) 250 throws Exception 251 { 252 Role role = null; 253 254 try 255 { 256 role = JetspeedSecurity.getRole( 260 rundata.getParameters().getString( SecurityConstants.PARAM_ENTITY_ID) ); 261 262 JetspeedSecurity.removeRole(role.getName()); 266 267 } 268 catch (Exception e) 269 { 270 logger.error("Exception", e); 272 273 DynamicURI duri = new DynamicURI (rundata); 277 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE); 278 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_DELETE_FAILED); 279 if (role != null) 280 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, role.getName()); 281 duri.addQueryData(SecurityConstants.PARAM_MODE, SecurityConstants.PARAM_MODE_DELETE); 282 rundata.setRedirectURI(duri.toString()); 283 284 if (role != null) 286 rundata.getUser().setTemp(TEMP_ROLE, role); 287 288 } 289 290 } 291 292 293 } | Popular Tags |