Coverage Report - org.trails.security.annotation.SecurityAnnotationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SecurityAnnotationHandler
77% 
100% 
0
 
 1  
 package org.trails.security.annotation;
 2  
 
 3  
 import java.lang.reflect.AnnotatedElement;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 
 7  
 import org.trails.descriptor.annotation.AbstractAnnotationHandler;
 8  
 import org.trails.security.ClassSecurityRestriction;
 9  
 import org.trails.security.PropertySecurityRestriction;
 10  
 import org.trails.security.RestrictionType;
 11  
 
 12  
 public class SecurityAnnotationHandler extends AbstractAnnotationHandler
 13  
 {
 14  
 
 15  
         public SecurityAnnotationHandler()
 16  
         {
 17  96
                 super();
 18  
                 // TODO Auto-generated constructor stub
 19  96
         }
 20  
 
 21  
         public List buildClassRestrictions(Class type)
 22  
         {
 23  
                 // TODO refactor the build methods with better reusability
 24  93
                 ArrayList<ClassSecurityRestriction> classRestrictions = new ArrayList<ClassSecurityRestriction>();
 25  
                 
 26  93
                 if (type.getAnnotation(ViewRequiresRole.class) != null) {
 27  57
                         ClassSecurityRestriction classRestriction = new ClassSecurityRestriction();
 28  57
                         classRestriction.setRequiredRole( ((ViewRequiresRole)type.getAnnotation(ViewRequiresRole.class)).value()) ;
 29  57
                         classRestriction.setRestrictionType(RestrictionType.VIEW);
 30  57
                         classRestrictions.add(classRestriction);
 31  
                 }
 32  93
                 if (type.getAnnotation(UpdateRequiresRole.class) != null) {
 33  30
                         ClassSecurityRestriction classRestriction = new ClassSecurityRestriction();
 34  30
                         classRestriction.setRequiredRole( ((UpdateRequiresRole)type.getAnnotation(UpdateRequiresRole.class)).value()) ;
 35  30
                         classRestriction.setRestrictionType(RestrictionType.UPDATE);
 36  30
                         classRestrictions.add(classRestriction);
 37  
                 }
 38  93
                 if (type.getAnnotation(RemoveRequiresRole.class) != null) {
 39  0
                         ClassSecurityRestriction classRestriction = new ClassSecurityRestriction();
 40  0
                         classRestriction.setRequiredRole( ((RemoveRequiresRole)type.getAnnotation(RemoveRequiresRole.class)).value()) ;
 41  0
                         classRestriction.setRestrictionType(RestrictionType.REMOVE );
 42  0
                         classRestrictions.add(classRestriction);
 43  
                 }
 44  
                 
 45  
                 /*
 46  
                 for (int i = 0; i < restrictionsLength; i++)
 47  
                 {
 48  
                         ClassSecurityRestriction classRestriction = new ClassSecurityRestriction();
 49  
                         setPropertiesFromAnnotation(securityAnnotation.restrictions()[i], classRestriction);
 50  
                         classRestrictions.add(classRestriction);
 51  
                 }
 52  
                 */
 53  
                 
 54  
                 
 55  93
                 return classRestrictions;
 56  
         }
 57  
 
 58  
         public List<PropertySecurityRestriction> buildPropertyRestrictions(AnnotatedElement annotatedElement, String propertyName)
 59  
         {
 60  1011
                 ArrayList<PropertySecurityRestriction> propertyRestrictions = new ArrayList<PropertySecurityRestriction>();
 61  1011
                 if (annotatedElement.getAnnotation(ViewRequiresRole.class) != null) {
 62  30
                         PropertySecurityRestriction propertyRestriction = new PropertySecurityRestriction();
 63  30
                         propertyRestriction.setPropertyName(propertyName);
 64  30
                         propertyRestriction.setRestrictionType(RestrictionType.VIEW);
 65  30
                         propertyRestriction.setRequiredRole(annotatedElement.getAnnotation(ViewRequiresRole.class).value());
 66  30
                         propertyRestrictions.add(propertyRestriction);
 67  
                 }
 68  1011
                 if (annotatedElement.getAnnotation(UpdateRequiresRole.class) != null) {
 69  18
                         PropertySecurityRestriction propertyRestriction = new PropertySecurityRestriction();
 70  18
                         propertyRestriction.setPropertyName(propertyName);
 71  18
                         propertyRestriction.setRestrictionType(RestrictionType.UPDATE);
 72  18
                         propertyRestriction.setRequiredRole(annotatedElement.getAnnotation(UpdateRequiresRole.class).value());
 73  18
                         propertyRestrictions.add(propertyRestriction);
 74  
                 }
 75  1011
                 if (annotatedElement.getAnnotation(RemoveRequiresRole.class) != null) {
 76  0
                         PropertySecurityRestriction propertyRestriction = new PropertySecurityRestriction();
 77  0
                         propertyRestriction.setPropertyName(propertyName);
 78  0
                         propertyRestriction.setRestrictionType(RestrictionType.REMOVE);
 79  0
                         propertyRestriction.setRequiredRole(annotatedElement.getAnnotation(RemoveRequiresRole.class).value());
 80  0
                         propertyRestrictions.add(propertyRestriction);
 81  
                 }
 82  
                 
 83  
                 /* The old way is clearly shorter, but the new is easier for the user
 84  
                 for (int i = 0; i < securityAnnotation.restrictions().length; i++)
 85  
                 {
 86  
                         PropertySecurityRestriction propertyRestriction = new PropertySecurityRestriction();
 87  
                         propertyRestriction.setPropertyName(propertyName);
 88  
                         setPropertiesFromAnnotation(securityAnnotation.restrictions()[i], propertyRestriction);
 89  
                         propertyRestrictions.add(propertyRestriction);
 90  
                 }
 91  
                 */
 92  1011
                 return propertyRestrictions;
 93  
         }
 94  
 
 95  
 }