1 16 17 package org.apache.jetspeed.modules.actions.portlets.security; 18 19 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Vector ; 23 24 import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction; 25 import org.apache.jetspeed.om.security.Permission; 26 import org.apache.jetspeed.om.security.Role; 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.jetspeed.services.security.JetspeedSecurityException; 33 import org.apache.turbine.util.DynamicURI; 34 import org.apache.turbine.util.RunData; 35 import org.apache.turbine.util.StringUtils; 36 import org.apache.velocity.context.Context; 37 38 45 public class RolePermissionUpdateAction extends SecureVelocityPortletAction 46 { 47 private static final String TEMP_ROLE = "tempRole"; 48 49 52 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RolePermissionUpdateAction.class.getName()); 53 54 61 protected void buildMaximizedContext( VelocityPortlet portlet, 62 Context context, 63 RunData rundata ) 64 { 65 buildNormalContext( portlet, context, rundata); 66 } 67 68 76 protected void buildConfigureContext( VelocityPortlet portlet, 77 Context context, 78 RunData rundata ) 79 { 80 81 buildNormalContext( portlet, context, rundata); 82 } 83 84 91 protected void buildNormalContext( VelocityPortlet portlet, 92 Context context, 93 RunData rundata ) 94 { 95 try 96 { 97 Role role = null; 98 101 String mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE); 102 103 String entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID); 107 if (entityid == null || entityid.trim().length() == 0) 108 { 109 return; 110 } 111 112 buildRolePermissionContext(portlet, context, rundata, entityid); 113 114 String msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID); 118 if (msgid != null) 119 { 120 int id = Integer.parseInt(msgid); 121 if (id < SecurityConstants.MESSAGES.length) 122 context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]); 123 } 124 125 } 126 catch (Exception e) 127 { 128 logger.error("Error in Jetspeed Role Permission Security", e); 129 rundata.setMessage("Error in Jetspeed Role Permission Security: " + e.toString()); 130 rundata.setStackTrace(StringUtils.stackTrace(e), e); 131 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error")); 132 } 133 } 134 135 136 142 public void doUpdate(RunData rundata, Context context) 143 throws Exception 144 { 145 String entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID); 146 if (entityid == null || entityid.trim().length() == 0) 147 { 148 logger.error("RolePermissionBrowser: Failed to get entity: " + entityid ); 149 DynamicURI duri = new DynamicURI (rundata); 150 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE); 151 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER); 152 rundata.setRedirectURI(duri.toString()); 153 return; 154 } 155 Role role = JetspeedSecurity.getRole(entityid); 156 if (null == role) 157 { 158 logger.error("RolePermissionBrowser: Failed to get role: " + entityid ); 159 DynamicURI duri = new DynamicURI (rundata); 160 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE); 161 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER); 162 rundata.setRedirectURI(duri.toString()); 163 return; 164 } 165 166 167 try 168 { 169 List permissions = (List )rundata.getUser().getTemp(SecurityConstants.CONTEXT_PERMISSIONS); 170 List selected = (List )rundata.getUser().getTemp(SecurityConstants.CONTEXT_SELECTED); 171 172 if (permissions == null || selected == null) 173 { 174 DynamicURI duri = new DynamicURI (rundata); 175 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE); 176 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER); 177 rundata.setRedirectURI(duri.toString()); 178 return; 179 } 180 181 for (int ix = 0; ix < permissions.size(); ix++) 186 { 187 String permissionName = ((Permission)permissions.get(ix)).getName(); 188 boolean newValue = rundata.getParameters().getBoolean("box_" + permissionName, false); 189 boolean oldValue = ((Boolean )selected.get(ix + 1)).booleanValue(); 190 if (newValue != oldValue) 191 { 192 if (newValue == true) 193 { 194 JetspeedSecurity.grantPermission(role.getName(), permissionName); 196 } 197 else 198 { 199 JetspeedSecurity.revokePermission(role.getName(), permissionName); 201 } 202 } 203 } 204 205 rundata.getUser().setTemp(SecurityConstants.CONTEXT_PERMISSIONS, null); 207 rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, null); 208 209 210 } 211 catch (Exception e) 212 { 213 logger.error("Failed update role+permission", e); 215 216 DynamicURI duri = new DynamicURI (rundata); 220 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE); 221 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED); 222 if (role != null) 223 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, role.getName()); 224 rundata.setRedirectURI(duri.toString()); 225 226 } 227 } 228 229 237 private void buildRolePermissionContext(VelocityPortlet portlet, 238 Context context, 239 RunData rundata, 240 String roleName) 241 throws JetspeedSecurityException 242 { 243 Iterator master = JetspeedSecurity.getPermissions(); 245 246 Role role = JetspeedSecurity.getRole(roleName); 248 if (null == role) 249 { 250 logger.error("RolePermissionBrowser: Failed to get role: " + roleName); 252 return; 253 } 254 255 Iterator subset = JetspeedSecurity.getPermissions(roleName); 257 258 Vector permissions = new Vector (); 259 Vector selected = new Vector (); 260 boolean sel = false; 261 int ix = 0; 262 selected.add(0, new Boolean (sel)); 263 while(master.hasNext()) 264 { 265 Permission permission = (Permission) master.next(); 266 permissions.add(permission); 267 sel = JetspeedSecurity.hasPermission(roleName, permission.getName()); 268 ix = ix + 1; 269 selected.add(ix, new Boolean (sel)); 270 } 271 selected.trimToSize(); 272 permissions.trimToSize(); 273 274 rundata.getUser().setTemp(SecurityConstants.CONTEXT_PERMISSIONS, permissions); 275 rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, selected); 276 context.put(SecurityConstants.CONTEXT_PERMISSIONS, permissions); 277 context.put(SecurityConstants.CONTEXT_SELECTED, selected); 278 context.put(SecurityConstants.CONTEXT_ROLE, role); 279 } 280 281 282 } | Popular Tags |