001 package org.codehaus.groovy.grails.plugins.springsecurity; 002 003 import java.lang.annotation.Annotation; 004 import java.lang.reflect.Field; 005 import java.lang.reflect.Method; 006 import java.util.Collection; 007 import java.util.HashSet; 008 import java.util.Set; 009 010 import org.springframework.core.annotation.AnnotationUtils; 011 import org.springframework.metadata.Attributes; 012 import org.springframework.security.SecurityConfig; 013 import org.springframework.security.annotation.Secured; 014 015 /** 016 * Re-implementation of Acegi's <code>SecurityAnnotationAttributes</code> as a temporary 017 * fix until I can figure out how to do this correctly in 2.0. 018 * 019 * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> 020 */ 021 public class SecurityAnnotationAttributes implements Attributes { 022 023 /** 024 * {@inheritDoc} 025 * @see org.springframework.metadata.Attributes#getAttributes(java.lang.Class) 026 */ 027 @SuppressWarnings("unchecked") 028 public Set<SecurityConfig> getAttributes(final Class target) { 029 Set<SecurityConfig> attributes = new HashSet<SecurityConfig>(); 030 031 for (Annotation annotation : target.getAnnotations()) { 032 if (annotation instanceof Secured) { 033 Secured attr = (Secured)annotation; 034 for (String auth : attr.value()) { 035 attributes.add(new SecurityConfig(auth)); 036 } 037 break; 038 } 039 } 040 041 return attributes; 042 } 043 044 /** 045 * {@inheritDoc} 046 * @see org.springframework.metadata.Attributes#getAttributes(java.lang.reflect.Method) 047 */ 048 public Set<SecurityConfig> getAttributes(final Method method) { 049 Set<SecurityConfig> attributes = new HashSet<SecurityConfig>(); 050 051 Annotation[] annotations = AnnotationUtils.getAnnotations(method); 052 for (Annotation annotation : annotations) { 053 if (annotation instanceof Secured) { 054 Secured attr = (Secured)annotation; 055 for (String auth : attr.value()) { 056 attributes.add(new SecurityConfig(auth)); 057 } 058 059 break; 060 } 061 } 062 063 return attributes; 064 } 065 066 /** 067 * {@inheritDoc} 068 * @see org.springframework.metadata.Attributes#getAttributes(java.lang.Class, java.lang.Class) 069 */ 070 @SuppressWarnings("unchecked") 071 public Collection getAttributes(final Class clazz, final Class filter) { 072 throw new UnsupportedOperationException("Unsupported operation"); 073 } 074 075 /** 076 * {@inheritDoc} 077 * @see org.springframework.metadata.Attributes#getAttributes(java.lang.reflect.Method, java.lang.Class) 078 */ 079 @SuppressWarnings("unchecked") 080 public Collection getAttributes(final Method method, final Class clazz) { 081 throw new UnsupportedOperationException("Unsupported operation"); 082 } 083 084 /** 085 * {@inheritDoc} 086 * @see org.springframework.metadata.Attributes#getAttributes(java.lang.reflect.Field) 087 */ 088 @SuppressWarnings("unchecked") 089 public Collection getAttributes(final Field field) { 090 throw new UnsupportedOperationException("Unsupported operation"); 091 } 092 093 /** 094 * {@inheritDoc} 095 * @see org.springframework.metadata.Attributes#getAttributes(java.lang.reflect.Field, java.lang.Class) 096 */ 097 @SuppressWarnings("unchecked") 098 public Collection getAttributes(final Field field, final Class clazz) { 099 throw new UnsupportedOperationException("Unsupported operation"); 100 } 101 }