| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.taglibs.velocity; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.acl.AclManager; |
| 19 |
|
|
| 20 |
|
import org.acegisecurity.taglibs.authz.AclTag; |
| 21 |
|
import org.acegisecurity.taglibs.authz.AuthenticationTag; |
| 22 |
|
import org.acegisecurity.taglibs.authz.AuthorizeTag; |
| 23 |
|
|
| 24 |
|
import org.springframework.context.ApplicationContext; |
| 25 |
|
|
| 26 |
|
import javax.servlet.jsp.JspException; |
| 27 |
|
import javax.servlet.jsp.PageContext; |
| 28 |
|
import javax.servlet.jsp.tagext.Tag; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
0 |
public class AuthzImpl implements Authz { |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
static final int ALL_GRANTED = 1; |
| 40 |
|
static final int ANY_GRANTED = 2; |
| 41 |
|
static final int NONE_GRANTED = 3; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
private ApplicationContext appCtx; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
public boolean allGranted(String roles) { |
| 50 |
0 |
return ifGranted(roles, ALL_GRANTED); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
public boolean anyGranted(String roles) { |
| 54 |
0 |
return ifGranted(roles, ANY_GRANTED); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
public ApplicationContext getAppCtx() { |
| 58 |
0 |
return appCtx; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
public String getPrincipal() { |
| 69 |
0 |
MyAuthenticationTag authenticationTag = new MyAuthenticationTag(); |
| 70 |
|
|
| 71 |
0 |
authenticationTag.setOperation("username"); |
| 72 |
|
|
| 73 |
|
try { |
| 74 |
0 |
authenticationTag.doStartTag(); |
| 75 |
0 |
} catch (JspException je) { |
| 76 |
0 |
je.printStackTrace(); |
| 77 |
0 |
throw new IllegalArgumentException(je.getMessage()); |
| 78 |
0 |
} |
| 79 |
|
|
| 80 |
0 |
return authenticationTag.getLastMessage(); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
public boolean hasPermission(Object domainObject, String permissions) { |
| 94 |
0 |
MyAclTag aclTag = new MyAclTag(); |
| 95 |
0 |
aclTag.setPageContext(null); |
| 96 |
0 |
aclTag.setContext(getAppCtx()); |
| 97 |
0 |
aclTag.setDomainObject(domainObject); |
| 98 |
0 |
aclTag.setHasPermission(permissions); |
| 99 |
|
|
| 100 |
0 |
int result = -1; |
| 101 |
|
|
| 102 |
|
try { |
| 103 |
0 |
result = aclTag.doStartTag(); |
| 104 |
0 |
} catch (JspException je) { |
| 105 |
0 |
throw new IllegalArgumentException(je.getMessage()); |
| 106 |
0 |
} |
| 107 |
|
|
| 108 |
0 |
if (Tag.EVAL_BODY_INCLUDE == result) { |
| 109 |
0 |
return true; |
| 110 |
|
} else { |
| 111 |
0 |
return false; |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
private boolean ifGranted(String roles, int grantType) { |
| 126 |
0 |
AuthorizeTag authorizeTag = new AuthorizeTag(); |
| 127 |
|
|
| 128 |
0 |
int result = -1; |
| 129 |
|
|
| 130 |
|
try { |
| 131 |
0 |
switch (grantType) { |
| 132 |
|
case ALL_GRANTED: |
| 133 |
0 |
authorizeTag.setIfAllGranted(roles); |
| 134 |
|
|
| 135 |
0 |
break; |
| 136 |
|
|
| 137 |
|
case ANY_GRANTED: |
| 138 |
0 |
authorizeTag.setIfAnyGranted(roles); |
| 139 |
|
|
| 140 |
0 |
break; |
| 141 |
|
|
| 142 |
|
case NONE_GRANTED: |
| 143 |
0 |
authorizeTag.setIfNotGranted(roles); |
| 144 |
|
|
| 145 |
0 |
break; |
| 146 |
|
|
| 147 |
|
default: |
| 148 |
0 |
throw new IllegalArgumentException("invalid granted type : " + grantType + " role=" + roles); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
0 |
result = authorizeTag.doStartTag(); |
| 152 |
0 |
} catch (JspException je) { |
| 153 |
0 |
throw new IllegalArgumentException(je.getMessage()); |
| 154 |
0 |
} |
| 155 |
|
|
| 156 |
0 |
if (Tag.EVAL_BODY_INCLUDE == result) { |
| 157 |
0 |
return true; |
| 158 |
|
} else { |
| 159 |
0 |
return false; |
| 160 |
|
} |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
public boolean noneGranted(String roles) { |
| 164 |
0 |
return ifGranted(roles, NONE_GRANTED); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
public void setAppCtx(ApplicationContext appCtx) { |
| 173 |
0 |
this.appCtx = appCtx; |
| 174 |
0 |
} |
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
0 |
private class MyAclTag extends AclTag { |
| 184 |
|
private static final long serialVersionUID = 6752340622125924108L; |
| 185 |
|
ApplicationContext context; |
| 186 |
|
|
| 187 |
|
protected ApplicationContext getContext(PageContext pageContext) { |
| 188 |
0 |
return context; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
protected void setContext(ApplicationContext context) { |
| 192 |
0 |
this.context = context; |
| 193 |
0 |
} |
| 194 |
|
} |
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
0 |
private class MyAuthenticationTag extends AuthenticationTag { |
| 201 |
|
private static final long serialVersionUID = -1094246833893599161L; |
| 202 |
0 |
String lastMessage = null; |
| 203 |
|
|
| 204 |
|
public String getLastMessage() { |
| 205 |
0 |
return lastMessage; |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
protected void writeMessage(String msg) throws JspException { |
| 209 |
0 |
lastMessage = msg; |
| 210 |
0 |
} |
| 211 |
|
} |
| 212 |
|
} |