[58] | 1 | /* Copyright 2006-2009 the original author or authors. |
---|
| 2 | * |
---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | * you may not use this file except in compliance with the License. |
---|
| 5 | * You may obtain a copy of the License at |
---|
| 6 | * |
---|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | * |
---|
| 9 | * Unless required by applicable law or agreed to in writing, software |
---|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | * See the License for the specific language governing permissions and |
---|
| 13 | * limitations under the License. |
---|
| 14 | */ |
---|
| 15 | package org.codehaus.groovy.grails.plugins.springsecurity |
---|
| 16 | |
---|
| 17 | import org.springframework.security.GrantedAuthority |
---|
| 18 | import org.springframework.security.GrantedAuthorityImpl |
---|
| 19 | import org.springframework.security.context.SecurityContextHolder as SCH |
---|
| 20 | import org.springframework.util.StringUtils as STU |
---|
| 21 | |
---|
| 22 | import grails.util.GrailsUtil |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * Helper methods. |
---|
| 26 | * @author Tsuyoshi Yamamoto |
---|
| 27 | */ |
---|
| 28 | class AuthorizeTools { |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Extract the role names from authorities. |
---|
| 32 | * @param authorities the authorities |
---|
| 33 | * @return the names |
---|
| 34 | */ |
---|
| 35 | static Set<String> authoritiesToRoles(authorities) { |
---|
| 36 | def roles = [] as Set |
---|
| 37 | authorities.each { authority -> |
---|
| 38 | if (null == authority.authority) { |
---|
| 39 | throw new IllegalArgumentException( |
---|
| 40 | "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process " |
---|
| 41 | + authority) |
---|
| 42 | } |
---|
| 43 | roles.add(authority.authority) |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | return roles |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Get the current user's authorities. |
---|
| 51 | * @return a list of authorities (empty if not authenticated). |
---|
| 52 | */ |
---|
| 53 | static List getPrincipalAuthorities() { |
---|
| 54 | def currentUser = SCH.context.authentication |
---|
| 55 | if (null == currentUser) { |
---|
| 56 | return Collections.emptyList() |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | if (!currentUser.authorities) { |
---|
| 60 | return Collections.emptyList() |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | return Arrays.asList(currentUser.authorities) |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Split the role names and create {@link GrantedAuthority}s for each. |
---|
| 68 | * @param authorizationsString comma-delimited role names |
---|
| 69 | * @return authorities (possibly empty) |
---|
| 70 | */ |
---|
| 71 | static Set<GrantedAuthority> parseAuthoritiesString(String authorizationsString) { |
---|
| 72 | def requiredAuthorities = [] as Set |
---|
| 73 | STU.commaDelimitedListToStringArray(authorizationsString).each { auth -> |
---|
| 74 | requiredAuthorities << new GrantedAuthorityImpl(auth) |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | return requiredAuthorities |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | static Set<String> retainAll(granted, required) { |
---|
| 81 | def grantedRoles = authoritiesToRoles(granted) |
---|
| 82 | def requiredRoles = authoritiesToRoles(required) |
---|
| 83 | grantedRoles.retainAll(requiredRoles) |
---|
| 84 | |
---|
| 85 | return rolesToAuthorities(grantedRoles, granted) |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | static Set<String> rolesToAuthorities(grantedRoles, granted) { |
---|
| 89 | def target = new HashSet() |
---|
| 90 | grantedRoles.each { role -> |
---|
| 91 | def auth = granted.find { authority -> authority.authority == role } |
---|
| 92 | if (auth != null) { |
---|
| 93 | target.add(auth.authority) |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | return target |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | static boolean ifAllGranted(role) { |
---|
| 101 | def granted = getPrincipalAuthorities() |
---|
| 102 | return granted.containsAll(parseAuthoritiesString(role)) |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | static boolean ifNotGranted(role) { |
---|
| 106 | def granted = getPrincipalAuthorities() |
---|
| 107 | Set grantedCopy = retainAll(granted, parseAuthoritiesString(role)) |
---|
| 108 | return grantedCopy.empty |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | static boolean ifAnyGranted(role) { |
---|
| 112 | def granted = getPrincipalAuthorities() |
---|
| 113 | Set grantedCopy = retainAll(granted, parseAuthoritiesString(role)) |
---|
| 114 | return !grantedCopy.empty |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | static ConfigObject getSecurityConfig() { |
---|
| 118 | |
---|
| 119 | GroovyClassLoader classLoader = new GroovyClassLoader(AuthorizeTools.getClassLoader()) |
---|
| 120 | |
---|
| 121 | def slurper = new ConfigSlurper(GrailsUtil.environment) |
---|
| 122 | ConfigObject userConfig |
---|
| 123 | try { |
---|
| 124 | userConfig = slurper.parse(classLoader.loadClass('SecurityConfig')) |
---|
| 125 | } |
---|
| 126 | catch (e) { |
---|
| 127 | // ignored, use defaults |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | ConfigObject config |
---|
| 131 | ConfigObject defaultConfig = slurper.parse(classLoader.loadClass('DefaultSecurityConfig')) |
---|
| 132 | if (userConfig) { |
---|
| 133 | config = defaultConfig.merge(userConfig) |
---|
| 134 | } |
---|
| 135 | else { |
---|
| 136 | config = defaultConfig |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | return config |
---|
| 140 | } |
---|
| 141 | } |
---|