| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.taglibs.authz; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.Authentication; |
| 19 |
|
import org.acegisecurity.GrantedAuthority; |
| 20 |
|
import org.acegisecurity.GrantedAuthorityImpl; |
| 21 |
|
|
| 22 |
|
import org.acegisecurity.context.SecurityContextHolder; |
| 23 |
|
|
| 24 |
|
import org.springframework.util.StringUtils; |
| 25 |
|
|
| 26 |
|
import org.springframework.web.util.ExpressionEvaluationUtils; |
| 27 |
|
|
| 28 |
|
import java.util.Arrays; |
| 29 |
|
import java.util.Collection; |
| 30 |
|
import java.util.Collections; |
| 31 |
|
import java.util.HashSet; |
| 32 |
|
import java.util.Iterator; |
| 33 |
|
import java.util.Set; |
| 34 |
|
|
| 35 |
|
import javax.servlet.jsp.JspException; |
| 36 |
|
import javax.servlet.jsp.tagext.Tag; |
| 37 |
|
import javax.servlet.jsp.tagext.TagSupport; |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
40 |
public class AuthorizeTag extends TagSupport { |
| 48 |
|
|
| 49 |
|
|
| 50 |
40 |
private String ifAllGranted = ""; |
| 51 |
40 |
private String ifAnyGranted = ""; |
| 52 |
40 |
private String ifNotGranted = ""; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private Set authoritiesToRoles(Collection c) { |
| 57 |
25 |
Set target = new HashSet(); |
| 58 |
|
|
| 59 |
25 |
for (Iterator iterator = c.iterator(); iterator.hasNext();) { |
| 60 |
33 |
GrantedAuthority authority = (GrantedAuthority) iterator.next(); |
| 61 |
|
|
| 62 |
33 |
if (null == authority.getAuthority()) { |
| 63 |
1 |
throw new IllegalArgumentException( |
| 64 |
|
"Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process " |
| 65 |
|
+ authority.toString()); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
32 |
target.add(authority.getAuthority()); |
| 69 |
32 |
} |
| 70 |
|
|
| 71 |
24 |
return target; |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
public int doStartTag() throws JspException { |
| 75 |
20 |
if (((null == ifAllGranted) || "".equals(ifAllGranted)) && ((null == ifAnyGranted) || "".equals(ifAnyGranted)) |
| 76 |
|
&& ((null == ifNotGranted) || "".equals(ifNotGranted))) { |
| 77 |
1 |
return Tag.SKIP_BODY; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
19 |
final Collection granted = getPrincipalAuthorities(); |
| 81 |
|
|
| 82 |
19 |
final String evaledIfNotGranted = ExpressionEvaluationUtils.evaluateString("ifNotGranted", ifNotGranted, |
| 83 |
|
pageContext); |
| 84 |
|
|
| 85 |
19 |
if ((null != evaledIfNotGranted) && !"".equals(evaledIfNotGranted)) { |
| 86 |
5 |
Set grantedCopy = retainAll(granted, parseAuthoritiesString(evaledIfNotGranted)); |
| 87 |
|
|
| 88 |
5 |
if (!grantedCopy.isEmpty()) { |
| 89 |
3 |
return Tag.SKIP_BODY; |
| 90 |
|
} |
| 91 |
|
} |
| 92 |
|
|
| 93 |
16 |
final String evaledIfAllGranted = ExpressionEvaluationUtils.evaluateString("ifAllGranted", ifAllGranted, |
| 94 |
|
pageContext); |
| 95 |
|
|
| 96 |
16 |
if ((null != evaledIfAllGranted) && !"".equals(evaledIfAllGranted)) { |
| 97 |
6 |
if (!granted.containsAll(parseAuthoritiesString(evaledIfAllGranted))) { |
| 98 |
3 |
return Tag.SKIP_BODY; |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
|
| 102 |
13 |
final String evaledIfAnyGranted = ExpressionEvaluationUtils.evaluateString("ifAnyGranted", ifAnyGranted, |
| 103 |
|
pageContext); |
| 104 |
|
|
| 105 |
13 |
if ((null != evaledIfAnyGranted) && !"".equals(evaledIfAnyGranted)) { |
| 106 |
8 |
Set grantedCopy = retainAll(granted, parseAuthoritiesString(evaledIfAnyGranted)); |
| 107 |
|
|
| 108 |
7 |
if (grantedCopy.isEmpty()) { |
| 109 |
3 |
return Tag.SKIP_BODY; |
| 110 |
|
} |
| 111 |
|
} |
| 112 |
|
|
| 113 |
9 |
return Tag.EVAL_BODY_INCLUDE; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
public String getIfAllGranted() { |
| 117 |
1 |
return ifAllGranted; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
public String getIfAnyGranted() { |
| 121 |
1 |
return ifAnyGranted; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
public String getIfNotGranted() { |
| 125 |
1 |
return ifNotGranted; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
private Collection getPrincipalAuthorities() { |
| 129 |
19 |
Authentication currentUser = SecurityContextHolder.getContext().getAuthentication(); |
| 130 |
|
|
| 131 |
19 |
if (null == currentUser) { |
| 132 |
2 |
return Collections.EMPTY_LIST; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
17 |
if ((null == currentUser.getAuthorities()) || (currentUser.getAuthorities().length < 1)) { |
| 136 |
0 |
return Collections.EMPTY_LIST; |
| 137 |
|
} |
| 138 |
|
|
| 139 |
17 |
Collection granted = Arrays.asList(currentUser.getAuthorities()); |
| 140 |
|
|
| 141 |
17 |
return granted; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
private Set parseAuthoritiesString(String authorizationsString) { |
| 145 |
19 |
final Set requiredAuthorities = new HashSet(); |
| 146 |
19 |
final String[] authorities = StringUtils.commaDelimitedListToStringArray(authorizationsString); |
| 147 |
|
|
| 148 |
44 |
for (int i = 0; i < authorities.length; i++) { |
| 149 |
25 |
String authority = authorities[i]; |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
25 |
String role = authority.trim(); |
| 154 |
25 |
role = StringUtils.replace(role, "\t", ""); |
| 155 |
25 |
role = StringUtils.replace(role, "\r", ""); |
| 156 |
25 |
role = StringUtils.replace(role, "\n", ""); |
| 157 |
25 |
role = StringUtils.replace(role, "\f", ""); |
| 158 |
|
|
| 159 |
25 |
requiredAuthorities.add(new GrantedAuthorityImpl(role)); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
19 |
return requiredAuthorities; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
private Set retainAll(final Collection granted, final Set required) { |
| 190 |
13 |
Set grantedRoles = authoritiesToRoles(granted); |
| 191 |
12 |
Set requiredRoles = authoritiesToRoles(required); |
| 192 |
12 |
grantedRoles.retainAll(requiredRoles); |
| 193 |
|
|
| 194 |
12 |
return rolesToAuthorities(grantedRoles, granted); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
private Set rolesToAuthorities(Set grantedRoles, Collection granted) { |
| 198 |
12 |
Set target = new HashSet(); |
| 199 |
|
|
| 200 |
12 |
for (Iterator iterator = grantedRoles.iterator(); iterator.hasNext();) { |
| 201 |
7 |
String role = (String) iterator.next(); |
| 202 |
|
|
| 203 |
7 |
for (Iterator grantedIterator = granted.iterator(); grantedIterator.hasNext();) { |
| 204 |
10 |
GrantedAuthority authority = (GrantedAuthority) grantedIterator.next(); |
| 205 |
|
|
| 206 |
10 |
if (authority.getAuthority().equals(role)) { |
| 207 |
7 |
target.add(authority); |
| 208 |
|
|
| 209 |
7 |
break; |
| 210 |
|
} |
| 211 |
3 |
} |
| 212 |
7 |
} |
| 213 |
|
|
| 214 |
12 |
return target; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
public void setIfAllGranted(String ifAllGranted) throws JspException { |
| 218 |
7 |
this.ifAllGranted = ifAllGranted; |
| 219 |
7 |
} |
| 220 |
|
|
| 221 |
|
public void setIfAnyGranted(String ifAnyGranted) throws JspException { |
| 222 |
10 |
this.ifAnyGranted = ifAnyGranted; |
| 223 |
10 |
} |
| 224 |
|
|
| 225 |
|
public void setIfNotGranted(String ifNotGranted) throws JspException { |
| 226 |
5 |
this.ifNotGranted = ifNotGranted; |
| 227 |
5 |
} |
| 228 |
|
} |