1 19 20 package com.sslexplorer.security.actions; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 35 import com.sslexplorer.boot.Util; 36 import com.sslexplorer.core.CoreUtil; 37 import com.sslexplorer.policyframework.Permission; 38 import com.sslexplorer.policyframework.PolicyConstants; 39 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 40 import com.sslexplorer.policyframework.PolicyUtil; 41 import com.sslexplorer.policyframework.ResourceStack; 42 import com.sslexplorer.policyframework.ResourceType; 43 import com.sslexplorer.policyframework.ResourceUtil; 44 import com.sslexplorer.policyframework.actions.AbstractResourcesDispatchAction; 45 import com.sslexplorer.security.AuthenticationScheme; 46 import com.sslexplorer.security.Constants; 47 import com.sslexplorer.security.DefaultAuthenticationScheme; 48 import com.sslexplorer.security.SessionInfo; 49 import com.sslexplorer.security.SystemDatabaseFactory; 50 import com.sslexplorer.security.User; 51 import com.sslexplorer.security.forms.AuthenticationSchemesForm; 52 53 61 public class ShowAuthenticationSchemesDispatchAction extends AbstractResourcesDispatchAction { 62 65 public ShowAuthenticationSchemesDispatchAction() { 66 super(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE); 67 } 68 69 76 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 77 return list(mapping, form, request, response); 78 } 79 80 private AuthenticationScheme getAuthenticationScheme(ActionForm form) throws Exception { 81 AuthenticationSchemesForm schemesForm = (AuthenticationSchemesForm) form; 82 int id = schemesForm.getSelectedResource(); 83 AuthenticationScheme scheme = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequence(id); 84 if (scheme == null) { 85 throw new Exception ("No scheme with Id of " + id + "."); 86 } 87 return scheme; 88 } 89 90 97 public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 98 PolicyUtil.checkPermission(getResourceType(), PolicyConstants.PERM_DELETE, request); 99 AuthenticationSchemesForm schemesForm = (AuthenticationSchemesForm) form; 100 int id = schemesForm.getSelectedResource(); 101 AuthenticationScheme scheme = getAuthenticationScheme(form); 102 if (scheme.isSystemScheme()) { 103 throw new Exception ("Cannot remove system schemes."); 104 } 105 106 int nextEnabled = getNextEnabledAuthenticationScheme(scheme); 107 if (nextEnabled == -1) { 108 saveError(request, "authenticationSchemes.error.mustHaveOneEnabledScheme", scheme); 109 return list(mapping, form, request, response); 110 } 111 112 List resourceIds = ResourceUtil.getSignonAuthenticationSchemeIDs(getSessionInfo(request).getUser()); 113 resourceIds.remove(new Integer (id)); 114 if (resourceIds.size() == 0) { 115 saveError(request, "authenticationSchemes.error.mustHavePolicySuperUserAssociation", scheme); 116 return list(mapping, form, request, response); 117 } 118 119 PolicyUtil.checkPermission(getResourceType(), PolicyConstants.PERM_DELETE, request); 120 return mapping.findForward("confirmRemove"); 121 } 122 123 private int getNextEnabledAuthenticationScheme(AuthenticationScheme scheme) throws Exception { 124 List <AuthenticationScheme> allSchemes = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences(); 125 int nextEnabled = -1; 126 for (AuthenticationScheme oseq : allSchemes) { 127 if (!oseq.equals(scheme) && oseq.getEnabled() && !oseq.isSystemScheme()) { 128 nextEnabled = oseq.getResourceId(); 129 } 130 } 131 return nextEnabled; 132 } 133 134 143 public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 144 throws Exception { 145 PolicyUtil.checkPermission(getResourceType(), PolicyConstants.PERM_DELETE, request); 146 AuthenticationScheme scheme = getAuthenticationScheme(form); 147 int nextEnabled = getNextEnabledAuthenticationScheme(scheme); 148 if (nextEnabled == -1) { 149 saveError(request, "authenticationSchemes.error.mustHaveOneEnabledScheme", scheme); 150 return list(mapping, form, request, response); 151 } 152 super.remove(mapping, form, request, response); 153 saveMessage(request, "authenticationSchemes.message.schemeDeleted", scheme); 154 return getRedirectWithMessages(mapping, request); 155 } 156 157 166 public ActionForward disable(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 167 throws Exception { 168 PolicyUtil.checkPermission(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, PolicyConstants.PERM_EDIT_AND_ASSIGN, request); 169 AuthenticationSchemesForm schemesForm = (AuthenticationSchemesForm) form; 170 int id = schemesForm.getSelectedResource(); 171 AuthenticationScheme scheme = getAuthenticationScheme(form); 172 173 List resourceIds = PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(getSessionInfo(request).getUser(), 174 PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE); 175 resourceIds.remove(new Integer (3)); 177 resourceIds.remove(new Integer (4)); 178 resourceIds.remove(new Integer (id)); 179 180 if (resourceIds.size() == 0) { 181 saveError(request, "authenticationSchemes.error.mustHavePolicySuperUserAssociation", scheme); 182 return list(mapping, form, request, response); 183 } 184 185 List <AuthenticationScheme> authSchemes = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences(); 186 int enabled = 0; 187 for (Iterator i = authSchemes.iterator(); i.hasNext();) { 188 AuthenticationScheme oseq = (DefaultAuthenticationScheme) i.next(); 189 if (oseq.getResourceId() == id && !oseq.getEnabled()) { 190 throw new Exception ("Scheme already disabled."); 191 } 192 if (oseq.getEnabled() && !oseq.isSystemScheme()) { 193 enabled++; 194 } 195 } 196 if (enabled == 1) { 197 saveError(request, "authenticationSchemes.error.cantDisableLastEnabledScheme", scheme); 198 return list(mapping, form, request, response); 199 } 200 scheme.setEnabled(false); 201 SystemDatabaseFactory.getInstance().updateAuthenticationSchemeSequence(scheme); 202 saveMessage(request, "authenticationSchemes.message.schemeDisabled", scheme); 203 return getRedirectWithMessages(mapping, request); 204 } 205 206 215 public ActionForward enable(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 216 throws Exception { 217 PolicyUtil.checkPermission(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, PolicyConstants.PERM_EDIT_AND_ASSIGN, request); 218 AuthenticationScheme scheme = getAuthenticationScheme(form); 219 if (scheme.getEnabled()) { 220 throw new Exception ("Alreadty enabled."); 221 } 222 scheme.setEnabled(true); 223 SystemDatabaseFactory.getInstance().updateAuthenticationSchemeSequence(scheme); 224 saveMessage(request, "authenticationSchemes.message.schemeEnabled", scheme); 225 return getRedirectWithMessages(mapping, request); 226 } 227 228 237 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 238 throws Exception { 239 PolicyUtil.checkPermissions(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, new Permission[] { 240 PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, 241 PolicyConstants.PERM_ASSIGN }, request); 242 AuthenticationScheme seq = getAuthenticationScheme(form); 243 ResourceStack.pushToEditingStack(request.getSession(), seq); 244 return mapping.findForward("edit"); 245 } 246 247 255 public ActionForward moveUp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 256 throws Exception { 257 AuthenticationScheme scheme = getAuthenticationScheme(form); 258 if (scheme.getPriorityInt() == 1) { 259 saveError(request, "authenticationSchemes.error.moveup.top", scheme); 260 return unspecified(mapping, form, request, response); 261 } 262 263 PolicyUtil.checkPermission(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, PolicyConstants.PERM_EDIT_AND_ASSIGN, request); 264 List <AuthenticationScheme> schemes = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences(); 265 SystemDatabaseFactory.getInstance().moveAuthenticationSchemeUp(scheme, schemes); 266 saveMessage(request, "authenticationSchemes.message.moveup", scheme); 267 return getRedirectWithMessages(mapping, request); 268 } 269 270 278 public ActionForward moveDown(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 279 AuthenticationScheme scheme = getAuthenticationScheme(form); 280 List <AuthenticationScheme> schemes = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences(); 281 if (schemes.indexOf(scheme) == schemes.size() - 1) { 282 saveError(request, "authenticationSchemes.error.movedown.bottom", scheme); 283 return unspecified(mapping, form, request, response); 284 } 285 286 PolicyUtil.checkPermission(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, PolicyConstants.PERM_EDIT_AND_ASSIGN, request); 287 SystemDatabaseFactory.getInstance().moveAuthenticationSchemeDown(scheme, schemes); 288 saveMessage(request, "authenticationSchemes.message.movedown", scheme); 289 return getRedirectWithMessages(mapping, request); 290 } 291 292 302 public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 303 throws Exception { 304 305 response.setHeader("Cache-Control", "no-cache"); 306 response.setHeader("Pragma", "must-revalidate"); 307 308 CoreUtil.clearFlow(request); 309 310 AuthenticationSchemesForm schemesForm = (AuthenticationSchemesForm) form; 311 schemesForm.initialize(getSessionInfo(request), getSessionInfo(request).getNavigationContext() == SessionInfo.MANAGEMENT_CONSOLE_CONTEXT ? 312 SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences(getSessionInfo(request).getUser().getRealm().getRealmID()) 313 : ResourceUtil.getGrantedResource(getSessionInfo(request), getResourceType())); 314 Util.noCache(response); 315 return mapping.findForward("display"); 316 } 317 318 325 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 326 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 327 } 328 } | Popular Tags |