1 package org.tigris.scarab.actions.admin; 2 3 48 49 import java.util.Iterator ; 50 import java.util.List ; 51 52 import org.apache.fulcrum.parser.ParameterParser; 53 import org.apache.fulcrum.security.TurbineSecurity; 54 import org.apache.fulcrum.security.util.AccessControlList; 55 import org.apache.fulcrum.security.util.DataBackendException; 56 import org.apache.torque.om.NumberKey; 57 import org.apache.turbine.RunData; 58 import org.apache.turbine.TemplateContext; 59 import org.apache.turbine.Turbine; 60 import org.tigris.scarab.actions.base.RequireLoginFirstAction; 61 import org.tigris.scarab.om.IssueTemplateInfo; 62 import org.tigris.scarab.om.IssueTemplateInfoPeer; 63 import org.tigris.scarab.om.Module; 64 import org.tigris.scarab.om.PendingGroupUserRole; 65 import org.tigris.scarab.om.Query; 66 import org.tigris.scarab.om.QueryPeer; 67 import org.tigris.scarab.om.ScarabUser; 68 import org.tigris.scarab.om.ScarabUserManager; 69 import org.tigris.scarab.om.Scope; 70 import org.tigris.scarab.services.security.ScarabSecurity; 71 import org.tigris.scarab.tools.ScarabLocalizationTool; 72 import org.tigris.scarab.tools.ScarabRequestTool; 73 import org.tigris.scarab.tools.SecurityAdminTool; 74 import org.tigris.scarab.tools.localization.L10NKeySet; 75 import org.tigris.scarab.tools.localization.L10NMessage; 76 import org.tigris.scarab.tools.localization.LocalizationKey; 77 import org.tigris.scarab.util.Email; 78 import org.tigris.scarab.util.EmailContext; 79 import org.tigris.scarab.util.ScarabConstants; 80 import org.tigris.scarab.util.ScarabException; 81 82 88 public class Approval extends RequireLoginFirstAction 89 { 90 private static final String REJECT = "reject"; 91 private static final String APPROVE = "approve"; 92 private static final String COMMENT = "comment"; 93 94 private static final Integer QUERY = new Integer (0); 95 private static final Integer ISSUE_ENTRY_TEMPLATE = new Integer (1); 96 private static final Integer COMMENTED = new Integer (2); 97 98 private static final Integer REJECTED = QUERY; 99 private static final Integer APPROVED = ISSUE_ENTRY_TEMPLATE; 100 101 public void doSubmit(RunData data, TemplateContext context) 102 throws Exception 103 { 104 ScarabRequestTool scarabR = (ScarabRequestTool)context 105 .get(ScarabConstants.SCARAB_REQUEST_TOOL); 106 ScarabLocalizationTool l10n = getLocalizationTool(context); 107 ScarabUser user = (ScarabUser)data.getUser(); 108 Module module = scarabR.getCurrentModule(); 109 String globalComment = data.getParameters().getString("global_comment"); 110 111 ParameterParser params = data.getParameters(); 112 Object [] keys = params.getKeys(); 113 String key; 114 String action = null; 115 Integer actionWord = null; 116 Integer artifact = null; 117 String artifactName = null; 118 String comment = null; 119 ScarabUser toUser = null; 120 String userId; 121 boolean success = true; 122 123 for (int i =0; i<keys.length; i++) 124 { 125 action="none"; 126 key = keys[i].toString(); 127 if (key.startsWith("query_id_")) 128 { 129 String queryId = key.substring(9); 130 Query query = QueryPeer.retrieveByPK(new NumberKey(queryId)); 131 132 action = params.getString("query_action_" + queryId); 133 comment = params.getString("query_comment_" + queryId); 134 135 userId = params.getString("query_user_" + queryId); 136 toUser = scarabR.getUser(userId); 137 artifact = QUERY; 138 artifactName = query.getName(); 139 140 if (query.getApproved()) 141 { 142 success = false; 143 boolean isApproved = Scope.MODULE__PK.equals(query.getScopeId()); 144 LocalizationKey l10nKey = (isApproved) ? 145 L10NKeySet.ItemAlreadyApproved:L10NKeySet.ItemAlreadyRejected; 146 L10NMessage l10nMessage = new L10NMessage(l10nKey,artifactName); 147 scarabR.setAlertMessage(l10nMessage); 148 } 149 else 150 { 151 if (action.equals(REJECT)) 152 { 153 try 154 { 155 query.approve(user, false); 156 } 157 catch (ScarabException e) 158 { 159 L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e); 160 scarabR.setAlertMessage(msg); 161 } 162 actionWord = REJECTED; 163 } 164 else if (action.equals(APPROVE)) 165 { 166 try 167 { 168 query.approve(user, true); 169 } 170 catch(ScarabException e) 171 { 172 L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e); 173 scarabR.setAlertMessage(msg); 174 } 175 actionWord = APPROVED; 176 } 177 else if (action.equals(COMMENT)) 178 { 179 actionWord = COMMENTED; 180 } 181 } 182 } 183 else if (key.startsWith("template_id_")) 184 { 185 String templateId = key.substring(12); 186 IssueTemplateInfo info = IssueTemplateInfoPeer 187 .retrieveByPK(new NumberKey(templateId)); 188 189 action = params.getString("template_action_" + templateId); 190 comment = params.getString("template_comment_" + templateId); 191 192 userId = params.getString("template_user_" + templateId); 193 toUser = scarabR.getUser(userId); 194 artifact = ISSUE_ENTRY_TEMPLATE; 195 artifactName = info.getName(); 196 197 if (info.getApproved()) 198 { 199 success = false; 200 boolean isApproved = Scope.MODULE__PK.equals(info.getScopeId()); 201 LocalizationKey l10nKey = (isApproved) ? 202 L10NKeySet.ItemAlreadyApproved:L10NKeySet.ItemAlreadyRejected; 203 L10NMessage l10nMessage = new L10NMessage(l10nKey,artifactName); 204 scarabR.setAlertMessage(l10nMessage); 205 } 206 else 207 { 208 if (action.equals(REJECT)) 209 { 210 try 211 { 212 info.approve(user, false); 213 } 214 catch(ScarabException e) 215 { 216 L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e); 217 scarabR.setAlertMessage(msg); 218 } 219 actionWord = REJECTED; 220 } 221 else if (action.equals(APPROVE)) 222 { 223 try 224 { 225 info.approve(user, true); 226 } 227 catch(ScarabException e) 228 { 229 L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e); 230 scarabR.setAlertMessage(msg); 231 } 232 actionWord = APPROVED; 233 } 234 else if (action.equals(COMMENT)) 235 { 236 actionWord = COMMENTED; 237 } 238 } 239 if (!action.equals("none") && success) 240 { 241 EmailContext ectx = new EmailContext(); 243 ectx.setUser(user); 244 ectx.put("artifactIndex", artifact); 246 ectx.put("artifactName", artifactName); 247 ectx.put("actionIndex", actionWord); 248 ectx.put("comment", comment); 249 ectx.put("globalComment", globalComment); 250 251 String template = Turbine.getConfiguration(). 252 getString("scarab.email.approval.template", 253 "Approval.vm"); 254 try 255 { 256 Email.sendEmail(ectx, module, user, 257 module.getSystemEmail(), 258 toUser, template); 259 } 260 catch (Exception e) 261 { 262 L10NMessage l10nMessage =new L10NMessage(EMAIL_ERROR,e); 263 scarabR.setAlertMessage(l10nMessage); 264 } 265 } 266 } 267 } 268 } 269 270 public void doApproveroles(RunData data, TemplateContext context) 271 throws Exception 272 { 273 String template = getCurrentTemplate(data, null); 274 String nextTemplate = getNextTemplate(data, template); 275 ScarabRequestTool scarabR = getScarabRequestTool(context); 276 ScarabLocalizationTool l10n = getLocalizationTool(context); 277 Module module = scarabR.getCurrentModule(); 278 if (((ScarabUser)data.getUser()) 279 .hasPermission(ScarabSecurity.USER__APPROVE_ROLES, module)) 280 { 281 SecurityAdminTool scarabA = getSecurityAdminTool(context); 282 List pendings = scarabA.getPendingGroupUserRoles(module); 283 Iterator i = pendings.iterator(); 284 while (i.hasNext()) 285 { 286 PendingGroupUserRole pending = (PendingGroupUserRole)i.next(); 287 ScarabUser user = 288 ScarabUserManager.getInstance(pending.getUserId()); 289 290 String checked = data.getParameters() 291 .getString("user_id_"+user.getUserName()); 292 293 if(checked != null && checked.equals("on")) 294 { 295 String role = data.getParameters() 296 .getString(user.getUserName()); 297 if (role != null && role.length() > 0) 298 { 299 if (role.equalsIgnoreCase(l10n.get(L10NKeySet.Deny))) 300 { 301 pending.delete(); 302 } 303 else 304 { 305 try 306 { 307 TurbineSecurity.grant(user, 308 (org.apache.fulcrum.security.entity.Group)module, 309 TurbineSecurity.getRole(role)); 310 } 311 catch (DataBackendException e) 312 { 313 AccessControlList acl = TurbineSecurity 316 .getACL(user); 317 if (acl 318 .hasRole( 319 TurbineSecurity.getRole(role), 320 (org.apache.fulcrum.security.entity.Group) module)) 321 { 322 String [] args = {role, 323 user.getUserName(), 324 module.getRealName()}; 325 String msg = l10n 326 .format( 327 "RolePreviouslyApprovedForUserInModule", 328 args); 329 String info = (String ) scarabR 330 .getInfoMessage(); 331 if (info == null) 332 { 333 info = msg; 334 } 335 else 336 { 337 info += " " + msg; 338 } 339 340 scarabR.setInfoMessage(info); 341 } 342 else 343 { 344 throw e; } 346 } 347 pending.delete(); 348 } 349 } 350 } 351 } 352 scarabR.setConfirmMessage(L10NKeySet.AllRolesProcessed); 353 } 354 setTarget(data, nextTemplate); 355 } 356 357 360 private SecurityAdminTool getSecurityAdminTool(TemplateContext context) 361 { 362 return (SecurityAdminTool)context 363 .get(ScarabConstants.SECURITY_ADMIN_TOOL); 364 } 365 } 366 | Popular Tags |