| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | 0 | |
| 12 | |
|
| 13 | |
|
| 14 | 0 | |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package nu.localhost.tapestry.acegi.components; |
| 19 | |
|
| 20 | |
import java.util.Arrays; |
| 21 | |
import java.util.Collection; |
| 22 | |
import java.util.Collections; |
| 23 | |
import java.util.HashSet; |
| 24 | |
import java.util.Iterator; |
| 25 | 0 | import java.util.Set; |
| 26 | |
|
| 27 | |
import org.acegisecurity.Authentication; |
| 28 | |
import org.acegisecurity.GrantedAuthority; |
| 29 | 0 | import org.acegisecurity.GrantedAuthorityImpl; |
| 30 | 0 | import org.acegisecurity.context.SecurityContextHolder; |
| 31 | |
import org.apache.tapestry.Block; |
| 32 | 0 | import org.apache.tapestry.annotations.Parameter; |
| 33 | |
import org.springframework.util.StringUtils; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | |
| 38 | |
|
| 39 | |
|
| 40 | 0 | |
| 41 | |
|
| 42 | 0 | public class IfRole { |
| 43 | 0 | |
| 44 | 0 | |
| 45 | 0 | |
| 46 | |
|
| 47 | |
|
| 48 | 0 | @Parameter(required = false, defaultPrefix = "literal", principal=true) |
| 49 | 0 | private String role; |
| 50 | |
|
| 51 | 0 | |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
@Parameter(required = false, defaultPrefix = "literal") |
| 57 | |
private String ifAllGranted; |
| 58 | |
|
| 59 | |
@Parameter(required = false, defaultPrefix = "literal") |
| 60 | 0 | private String ifAnyGranted; |
| 61 | |
|
| 62 | |
@Parameter(required = false, defaultPrefix = "literal") |
| 63 | |
private String ifNotGranted; |
| 64 | 0 | |
| 65 | 0 | |
| 66 | 0 | |
| 67 | 0 | |
| 68 | 0 | |
| 69 | |
@Parameter |
| 70 | |
private boolean negate; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
@Parameter(name = "else") |
| 77 | |
private Block elseBlock; |
| 78 | |
|
| 79 | |
private boolean test; |
| 80 | |
|
| 81 | |
@SuppressWarnings("unchecked") |
| 82 | |
private Collection getPrincipalAuthorities() { |
| 83 | 0 | Authentication currentUser = null; |
| 84 | 0 | currentUser = SecurityContextHolder.getContext().getAuthentication(); |
| 85 | |
|
| 86 | 0 | if (null == currentUser) { |
| 87 | 0 | return Collections.EMPTY_LIST; |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | if ((null == currentUser.getAuthorities()) || (currentUser.getAuthorities().length < 1)) { |
| 91 | 0 | return Collections.EMPTY_LIST; |
| 92 | |
} |
| 93 | |
|
| 94 | 0 | Collection granted = Arrays.asList(currentUser.getAuthorities()); |
| 95 | 0 | return granted; |
| 96 | |
} |
| 97 | |
|
| 98 | |
@SuppressWarnings("unchecked") |
| 99 | |
private Set authoritiesToRoles(Collection c) { |
| 100 | 0 | Set target = new HashSet(); |
| 101 | |
|
| 102 | 0 | for (Iterator iterator = c.iterator(); iterator.hasNext();) { |
| 103 | 0 | GrantedAuthority authority = (GrantedAuthority) iterator.next(); |
| 104 | |
|
| 105 | 0 | if (null == authority.getAuthority()) { |
| 106 | 0 | throw new IllegalArgumentException( |
| 107 | 0 | "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process " |
| 108 | 0 | + authority.toString()); |
| 109 | |
} |
| 110 | |
|
| 111 | 0 | target.add(authority.getAuthority()); |
| 112 | |
} |
| 113 | |
|
| 114 | 0 | return target; |
| 115 | |
} |
| 116 | |
|
| 117 | |
@SuppressWarnings("unchecked") |
| 118 | |
private Set parseAuthoritiesString(String authorizationsString) { |
| 119 | 0 | final Set requiredAuthorities = new HashSet(); |
| 120 | 0 | final String[] authorities = StringUtils.commaDelimitedListToStringArray(authorizationsString); |
| 121 | |
|
| 122 | 0 | for (int i = 0; i < authorities.length; i++) { |
| 123 | 0 | String authority = authorities[i]; |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | 0 | String role = StringUtils.replace(authority, " ", ""); |
| 128 | 0 | role = StringUtils.replace(role, "\t", ""); |
| 129 | 0 | role = StringUtils.replace(role, "\r", ""); |
| 130 | 0 | role = StringUtils.replace(role, "\n", ""); |
| 131 | 0 | role = StringUtils.replace(role, "\f", ""); |
| 132 | |
|
| 133 | 0 | requiredAuthorities.add(new GrantedAuthorityImpl(role)); |
| 134 | |
} |
| 135 | |
|
| 136 | 0 | return requiredAuthorities; |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
@SuppressWarnings("unchecked") |
| 178 | |
private Set retainAll(final Collection granted, final Set required) { |
| 179 | 0 | Set grantedRoles = authoritiesToRoles(granted); |
| 180 | 0 | Set requiredRoles = authoritiesToRoles(required); |
| 181 | 0 | grantedRoles.retainAll(requiredRoles); |
| 182 | |
|
| 183 | 0 | return rolesToAuthorities(grantedRoles, granted); |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
@SuppressWarnings("unchecked") |
| 193 | |
private Set rolesToAuthorities(Set grantedRoles, Collection granted) { |
| 194 | 0 | Set target = new HashSet(); |
| 195 | |
|
| 196 | 0 | for (Iterator iterator = grantedRoles.iterator(); iterator.hasNext();) { |
| 197 | 0 | String role = (String) iterator.next(); |
| 198 | |
|
| 199 | 0 | for (Iterator grantedIterator = granted.iterator(); grantedIterator.hasNext();) { |
| 200 | 0 | GrantedAuthority authority = (GrantedAuthority) grantedIterator.next(); |
| 201 | |
|
| 202 | 0 | if (authority.getAuthority().equals(role)) { |
| 203 | 0 | target.add(authority); |
| 204 | 0 | break; |
| 205 | |
} |
| 206 | |
} |
| 207 | |
} |
| 208 | |
|
| 209 | 0 | return target; |
| 210 | |
} |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
@SuppressWarnings("unchecked") |
| 218 | |
private boolean checkPermission() { |
| 219 | 0 | if (((null == ifAllGranted) || "".equals(ifAllGranted)) |
| 220 | 0 | && ((null == ifAnyGranted) || "".equals(ifAnyGranted)) |
| 221 | 0 | && ((null == role) || "".equals(role)) |
| 222 | 0 | && ((null == ifNotGranted) || "".equals(ifNotGranted))) { |
| 223 | 0 | return false; |
| 224 | 0 | } |
| 225 | |
|
| 226 | 0 | final Collection granted = getPrincipalAuthorities(); |
| 227 | 0 | |
| 228 | 0 | if ((null != role) && !"".equals(role)) { |
| 229 | 0 | Set grantedCopy = retainAll(granted, parseAuthoritiesString(role)); |
| 230 | 0 | if (grantedCopy.isEmpty()) { |
| 231 | 0 | return false; |
| 232 | 0 | } |
| 233 | |
} |
| 234 | |
|
| 235 | 0 | if ((null != ifNotGranted) && !"".equals(ifNotGranted)) { |
| 236 | 0 | Set grantedCopy = retainAll(granted, parseAuthoritiesString(ifNotGranted)); |
| 237 | 0 | |
| 238 | 0 | if (!grantedCopy.isEmpty()) { |
| 239 | 0 | return false; |
| 240 | 0 | } |
| 241 | |
} |
| 242 | |
|
| 243 | 0 | if ((null != ifAllGranted) && !"".equals(ifAllGranted)) { |
| 244 | 0 | if (!granted.containsAll(parseAuthoritiesString(ifAllGranted))) { |
| 245 | 0 | return false; |
| 246 | 0 | } |
| 247 | |
} |
| 248 | |
|
| 249 | 0 | if ((null != ifAnyGranted) && !"".equals(ifAnyGranted)) { |
| 250 | 0 | Set grantedCopy = retainAll(granted, parseAuthoritiesString(ifAnyGranted)); |
| 251 | 0 | |
| 252 | 0 | if (grantedCopy.isEmpty()) { |
| 253 | 0 | return false; |
| 254 | 0 | } |
| 255 | |
} |
| 256 | |
|
| 257 | 0 | return true; |
| 258 | 0 | } |
| 259 | |
|
| 260 | |
|
| 261 | |
void setupRender() { |
| 262 | 0 | test = checkPermission(); |
| 263 | 0 | } |
| 264 | 0 | |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
Object beginRender() { |
| 271 | 0 | return test != negate ? null : elseBlock; |
| 272 | 0 | } |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
boolean beforeRenderBody() { |
| 279 | 0 | return test != negate; |
| 280 | 0 | } |
| 281 | |
} |