1 19 20 package com.sslexplorer.policyframework; 21 22 import java.util.List ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import com.sslexplorer.core.CoreAttributeConstants; 30 import com.sslexplorer.core.CoreEvent; 31 import com.sslexplorer.core.CoreEventConstants; 32 import com.sslexplorer.core.CoreServlet; 33 import com.sslexplorer.properties.Property; 34 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey; 35 import com.sslexplorer.security.AccountLock; 36 import com.sslexplorer.security.AccountLockedException; 37 import com.sslexplorer.security.AuthenticationScheme; 38 import com.sslexplorer.security.InvalidLoginCredentialsException; 39 import com.sslexplorer.security.LogonControllerFactory; 40 import com.sslexplorer.security.SessionInfo; 41 import com.sslexplorer.security.SystemDatabaseFactory; 42 import com.sslexplorer.security.User; 43 44 50 51 public class PolicyUtil { 52 53 final static Log log = LogFactory.getLog(PolicyUtil.class); 54 55 61 public static boolean canLogin(Principal principal) throws Exception { 62 PolicyDatabase policyDatabase = PolicyDatabaseFactory.getInstance(); 63 List <Integer > grantedResourcesOfType = policyDatabase.getGrantedResourcesOfType(principal, PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE); 64 for (Integer schemeId : grantedResourcesOfType) { 65 AuthenticationScheme scheme = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequence(schemeId); 66 if(scheme!=null && !scheme.isSystemScheme() && scheme.getEnabled()) { 67 return true; 68 } 69 } 70 return false; 71 } 72 73 81 public static void checkLogin(User user) throws InvalidLoginCredentialsException, AccountLockedException { 82 try { 83 if (!canLogin(user)) { 84 throw new InvalidLoginCredentialsException("You do not have permission to logon."); 85 } 86 if (!isEnabled(user)) { 87 throw new AccountLockedException(user.getPrincipalName(), "Account locked. Please contact your administrator.", true, 0); 88 } 89 } catch (InvalidLoginCredentialsException lce) { 90 throw lce; 91 } catch (AccountLockedException ale) { 92 throw ale; 93 } catch (Exception e) { 94 log.error("Failed to test if logon for " + user.getPrincipalName() + " is allowed.", e); 95 throw new InvalidLoginCredentialsException("You do not have permission to logon."); 96 } 97 } 98 99 106 public static boolean isEnabled(User user) throws Exception { 107 return Property.getPropertyBoolean(new UserAttributeKey(user, User.USER_ATTR_ENABLED)); 108 } 109 110 119 public static void setEnabled(User user, boolean enabled, AccountLock lock, SessionInfo session) throws Exception { 120 CoreServlet servlet = CoreServlet.getServlet(); 121 try { 122 servlet.fireCoreEvent(new CoreEvent(servlet, CoreEventConstants.ACCOUNT_LOCKED, lock, session)); 123 Property.setProperty(new UserAttributeKey(user, User.USER_ATTR_ENABLED), enabled, session); 124 servlet.fireCoreEvent(new CoreEvent(CoreServlet.getServlet(), enabled ? CoreEventConstants.GRANT_ACCESS : CoreEventConstants.REVOKE_ACCESS, null, session, 125 CoreEvent.STATE_SUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, user 126 .getPrincipalName())); 127 } catch (Exception e) { 128 servlet.fireCoreEvent(new CoreEvent(servlet, enabled ? CoreEventConstants.GRANT_ACCESS : CoreEventConstants.REVOKE_ACCESS, null, session, 129 CoreEvent.STATE_UNSUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, user 130 .getPrincipalName())); 131 throw e; 132 } 133 134 } 135 136 145 public static void checkPermissions(ResourceType resourceType, Permission[] permissions, HttpServletRequest request) 146 throws NoPermissionException { 147 for(int i = 0 ; i < permissions.length; i++) { 148 try { 149 checkPermission(resourceType, permissions[i], request); 150 break; 151 } 152 catch(NoPermissionException npe) { 153 if(i == ( permissions.length - 1 ) ) { 154 throw npe; 155 } 156 } 157 } 158 } 159 160 169 public static void checkPermission(ResourceType resourceType, Permission permission, HttpServletRequest request) 170 throws NoPermissionException { 171 try { 172 User user = LogonControllerFactory.getInstance().getUser(request); 173 checkPermission(resourceType, permission, user); 174 } catch (NoPermissionException npe) { 175 throw npe; 176 } catch (Exception e) { 177 throw new NoPermissionException("Failed to check permission. ", e, null, resourceType); 178 } 179 } 180 181 190 public static void checkPermission(ResourceType resourceType, Permission permission, User user) 191 throws NoPermissionException { 192 try { 193 PolicyDatabase policyDatabase = PolicyDatabaseFactory.getInstance(); 194 if (user == null) { 195 throw new NoPermissionException("Failed to get user.", null, resourceType); 196 } 197 if (!policyDatabase.isPermitted(resourceType, 198 new Permission[] { 199 permission 200 }, user, false)) { 201 throw new NoPermissionException("Permission denied.", user, resourceType); 202 } 203 } catch (NoPermissionException npe) { 204 throw npe; 205 } catch (Exception e) { 206 throw new NoPermissionException("Failed to check permission. ", e, null, resourceType); 207 } 208 } 209 } 210 | Popular Tags |