1 package org.tigris.scarab.actions; 2 3 48 49 import java.util.List ; 50 import java.util.ArrayList ; 51 import java.util.Arrays ; 52 import java.util.Iterator ; 53 54 import org.apache.turbine.TemplateContext; 56 import org.apache.turbine.RunData; 57 58 import org.apache.fulcrum.security.TurbineSecurity; 59 60 import org.tigris.scarab.om.ScarabUser; 62 import org.tigris.scarab.om.ScarabModule; 63 import org.tigris.scarab.om.PendingGroupUserRole; 64 import org.tigris.scarab.tools.SecurityAdminTool; 65 import org.tigris.scarab.tools.ScarabRequestTool; 66 import org.tigris.scarab.tools.ScarabLocalizationTool; 67 import org.tigris.scarab.tools.localization.L10NMessage; 68 import org.tigris.scarab.tools.localization.L10NKeySet; 69 import org.tigris.scarab.tools.localization.Localizable; 70 import org.tigris.scarab.util.ScarabConstants; 71 import org.tigris.scarab.actions.base.RequireLoginFirstAction; 72 import org.tigris.scarab.util.EmailContext; 73 import org.tigris.scarab.util.Email; 74 import org.tigris.scarab.util.SimpleSkipFiltering; 75 import org.tigris.scarab.services.security.ScarabSecurity; 76 77 83 public class HandleRoleRequests extends RequireLoginFirstAction 84 { 85 public void doRequestroles(RunData data, TemplateContext context) 86 throws Exception 87 { 88 String template = getCurrentTemplate(data, null); 89 String nextTemplate = getNextTemplate(data, template); 90 ScarabUser user = (ScarabUser)data.getUser(); 91 SecurityAdminTool scarabA = getSecurityAdminTool(context); 92 ScarabRequestTool scarabR = getScarabRequestTool(context); 93 ScarabLocalizationTool l10n = getLocalizationTool(context); 94 95 List groups = scarabA.getNonMemberGroups(user); 97 98 Iterator gi = groups.iterator(); 99 100 String autoApproveRoleSet=null; 101 String waitApproveRoleSet=null; 102 103 while (gi.hasNext()) 104 { 105 ScarabModule module = ((ScarabModule)gi.next()); 106 String [] autoRoles = module.getAutoApprovedRoles(); 107 String role = data.getParameters().getString(module.getName()); 108 if (role != null && role.length() > 0) 109 { 110 boolean autoApprove = Arrays.asList(autoRoles).contains(role); 111 if (autoApprove) 112 { 113 TurbineSecurity.grant(user, module, 114 TurbineSecurity.getRole(role)); 115 116 autoApproveRoleSet = addToRoleSet(autoApproveRoleSet,module, role); 117 } 118 else 119 { 120 try 121 { 122 sendNotification(module, user, role); 123 } 124 catch(Exception e) 125 { 126 L10NMessage l10nMessage = new L10NMessage(L10NKeySet.CouldNotSendNotification,e); 127 scarabR.setAlertMessage(l10nMessage); 128 } 129 130 PendingGroupUserRole pend = new PendingGroupUserRole(); 131 pend.setGroupId(module.getModuleId()); 132 pend.setUserId(user.getUserId()); 133 pend.setRoleName(role); 134 pend.save(); 135 136 waitApproveRoleSet = addToRoleSet(waitApproveRoleSet,module, role); 137 } 138 } 139 } 140 141 if (autoApproveRoleSet != null) 142 { 143 SimpleSkipFiltering htmlSet = new SimpleSkipFiltering(autoApproveRoleSet+"<br>"); 144 Localizable msg = new L10NMessage(L10NKeySet.RoleRequestGranted, htmlSet); 145 scarabR.setConfirmMessage(msg); 146 } 147 148 if (waitApproveRoleSet != null) 149 { 150 SimpleSkipFiltering htmlSet = new SimpleSkipFiltering(waitApproveRoleSet+"<br>"); 151 Localizable msg = new L10NMessage(L10NKeySet.RoleRequestAwaiting, htmlSet); 152 scarabR.setInfoMessage(msg); 153 } 154 155 setTarget(data, nextTemplate); 156 } 157 158 165 private String addToRoleSet(String roleSet, ScarabModule module, String role) 166 { 167 String result; 168 if(roleSet==null) 169 { 170 result = "<br> "; 171 } 172 else 173 { 174 result = roleSet + "<br> "; 175 } 176 result += module.getName()+":"+role; 177 return result; 178 } 179 180 183 private SecurityAdminTool getSecurityAdminTool(TemplateContext context) 184 { 185 return (SecurityAdminTool)context 186 .get(ScarabConstants.SECURITY_ADMIN_TOOL); 187 } 188 189 196 private void sendNotification(ScarabModule module, ScarabUser user, 197 String role) 198 throws Exception 199 { 200 EmailContext econtext = new EmailContext(); 201 202 econtext.setModule(module); 203 econtext.setUser(user); 204 econtext.put("role", role); 205 206 List approvers = Arrays.asList(module. 208 getUsers(ScarabSecurity.USER__APPROVE_ROLES)); 209 210 List approversWithRole = new ArrayList (); 212 for(Iterator i = approvers.iterator(); i.hasNext();) 213 { 214 ScarabUser u = (ScarabUser)i.next(); 215 if (u.hasAnyRoleIn(module)) 216 { 217 approversWithRole.add(u); 218 } 219 } 220 221 if (!approversWithRole.isEmpty()) 223 { 224 approvers = approversWithRole; 225 } 226 227 Email.sendEmail(econtext, module, 228 "scarab.email.default", module.getSystemEmail(), 229 approvers, null, 230 "RoleRequest.vm"); 231 } 232 } 233 234 | Popular Tags |