1 16 17 package org.apache.struts.chain; 18 19 20 import org.apache.commons.chain.Command; 21 import org.apache.commons.chain.Context; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.struts.action.ActionServlet; 25 import org.apache.struts.config.ActionConfig; 26 import org.apache.struts.util.MessageResources; 27 28 29 37 38 public abstract class AbstractAuthorizeAction implements Command { 39 40 41 43 44 private String actionConfigKey = Constants.ACTION_CONFIG_KEY; 45 private String actionServletKey = Constants.ACTION_SERVLET_KEY; 46 47 private static final Log log = 48 LogFactory.getLog(AbstractAuthorizeAction.class); 49 50 51 53 54 59 public String getActionConfigKey() { 60 61 return (this.actionConfigKey); 62 63 } 64 65 66 73 public void setActionConfigKey(String actionConfigKey) { 74 75 this.actionConfigKey = actionConfigKey; 76 77 } 78 79 80 85 public String getActionServletKey() { 86 87 return (this.actionServletKey); 88 89 } 90 91 92 99 public void setActionServletKey(String actionServletKey) { 100 101 this.actionServletKey = actionServletKey; 102 103 } 104 105 106 108 109 119 public boolean execute(Context context) throws Exception { 120 121 ActionConfig actionConfig = (ActionConfig) 123 context.get(getActionConfigKey()); 124 125 String roles[] = actionConfig.getRoleNames(); 127 if ((roles == null) || (roles.length < 1)) { 128 return (false); 129 } 130 131 boolean throwEx = false; 132 try { 133 throwEx = !(isAuthorized(context, roles, actionConfig)); 134 } 135 catch (Exception ex) { 136 throwEx = true; 137 log.error("Unable to complete authorization process", ex); 138 } 139 140 if (throwEx) { 141 ActionServlet servlet = 143 (ActionServlet) context.get(actionServletKey); 144 MessageResources resources = servlet.getInternal(); 145 146 throw new UnauthorizedActionException( 148 resources.getMessage("notAuthorized", 149 actionConfig.getPath())); 150 } else { 151 return (false); 152 } 153 154 } 155 156 157 159 160 171 protected abstract boolean isAuthorized(Context context, String [] roles, 172 ActionConfig actionConfig) 173 throws Exception ; 174 175 } 176 | Popular Tags |