예제 #1
0
        public void AllowTypesAttribute_ShouldThrowExceptionIfNoExtensionsProvided(string fileName, string[] allowedExtensions)
        {
            var stream = FileHelpers.ReadFile(fileName);

            var attributeToTest = new AllowedTypesAttribute(allowedExtensions);

            Assert.Throws <InvalidOperationException>(() => attributeToTest.IsValid(stream));
        }
        //Calls the VerifyAllowedTypesAttribute in the parent class with reflection.
        //NOTE: Don't rename this method since the name of the method is used to call the parent method.
        private static void VerifyAllowedTypesAttribute(AllowedTypesAttribute attribute, PropertyInfo property)
        {
            var methodName = MethodBase.GetCurrentMethod().Name;
            var method     = GetMethodFromParent(methodName, BindingFlags.Static | BindingFlags.NonPublic);
            var parameters = new object[] { attribute, property };

            method.Invoke(null, parameters);
        }
예제 #3
0
        public void AllowedTypeAttribute_ShouldAllowOnlyPointedTypes(string fileName, string[] allowedExtensions, bool expectedResult)
        {
            var stream = FileHelpers.ReadFile(fileName);

            var attributeToTest = new AllowedTypesAttribute(allowedExtensions);

            Assert.AreEqual(expectedResult, attributeToTest.IsValid(stream));
        }
예제 #4
0
        public void AllowTypesAttribute_ShouldReturnFalseIfOneOfTheFilesIsNotAllowed()
        {
            var fileNames = new[] { "test.zip", "test.7z", "test.gz" };
            var files     = FileHelpers.ReadFiles(fileNames);

            var attributeToTest = new AllowedTypesAttribute(FileExtension.Zip, FileExtension.SevenZ);

            Assert.IsFalse(attributeToTest.IsValid(files));
        }
예제 #5
0
        public void AllowTypesAttribute_ShouldValidateMultipleFiles()
        {
            var fileNames = new[] { "test.zip", "test.7z", "test.gz" };
            var files     = FileHelpers.ReadFiles(fileNames);

            var attributeToTest = new AllowedTypesAttribute(FileExtension.Zip, FileExtension.SevenZ, FileExtension.Gz);

            Assert.IsTrue(attributeToTest.IsValid(files));
        }
예제 #6
0
        public void MergeAttributes_SecondArgumentNull()
        {
            var allowedTypesAttribute = new AllowedTypesAttribute
            {
                AllowedTypes = new[] { typeof(StandardPage) }
            };

            var result = InjectedAllowedTypes.MergeAttributes(allowedTypesAttribute, null);

            result.AllowedTypes.ShouldBe(new[] { typeof(StandardPage) });
        }
예제 #7
0
        public void MergeAttributes_ShouldContainBothSpecifiedAndInjectedAttribute_Distinct()
        {
            var allowedTypesAttribute = new AllowedTypesAttribute
            {
                AllowedTypes = new[] { typeof(StandardPage) }
            };

            var injectedAllowedTypesAttribute = new InjectedAllowedTypesAttribute(_contentLoader)
            {
                AllowedTypes = new[] { typeof(DifferentPage), typeof(StandardPage) }
            };

            var result = InjectedAllowedTypes.MergeAttributes(allowedTypesAttribute, injectedAllowedTypesAttribute);

            result.AllowedTypes.ShouldBe(new[] { typeof(StandardPage), typeof(DifferentPage) });
        }
예제 #8
0
        public static AllowedTypesAttribute GetInjectedAllowedTypesAttribute(Type contentType,
                                                                             string propertyName)
        {
            var allInjectedAttributes = _injectedAllowedTypesAttributes;
            var key = string.Format("{0}.{1}", contentType.Name, propertyName);
            var injectedAttribute = allInjectedAttributes.FirstOrDefault(x => x.Key.Equals(key));

            if (injectedAttribute.Value == null)
            {
                return(null);
            }

            var allowedTypeAttribute = new AllowedTypesAttribute
            {
                AllowedTypes    = injectedAttribute.Value.AllowedTypes,
                RestrictedTypes = injectedAttribute.Value.RestrictedTypes
            };

            return(allowedTypeAttribute);
        }
예제 #9
0
        /// <summary>
        /// Merges a AllowedTypesAttribute with a InjectedAllowedTypesAttribute
        /// </summary>
        /// <param name="injectedAllowedTypesAttribute">The AllowedTypesAttribute specified in the InitializationModule(RegisterInjectedAllowedTypesAttributes)</param>
        /// <param name="specifiedAllowedTypesAttribute">The InjectedAllowedTypesAttribute specified on a property with an attribute</param>
        /// <returns></returns>
        public static AllowedTypesAttribute MergeAttributes(
            AllowedTypesAttribute injectedAllowedTypesAttribute, InjectedAllowedTypesAttribute specifiedAllowedTypesAttribute)
        {
            if (injectedAllowedTypesAttribute == null)
            {
                injectedAllowedTypesAttribute = SetDefaultAllowedTypesAttributeValue();
            }

            var mergedAllowedTypesAttribute = new AllowedTypesAttribute
            {
                AllowedTypes    = injectedAllowedTypesAttribute.AllowedTypes,
                RestrictedTypes = injectedAllowedTypesAttribute.RestrictedTypes
            };

            if (specifiedAllowedTypesAttribute != null)
            {
                mergedAllowedTypesAttribute = new AllowedTypesAttribute
                {
                    AllowedTypes =
                        injectedAllowedTypesAttribute.AllowedTypes.Concat(specifiedAllowedTypesAttribute.AllowedTypes).Distinct().ToArray(),
                    RestrictedTypes =
                        injectedAllowedTypesAttribute.RestrictedTypes.Concat(specifiedAllowedTypesAttribute.RestrictedTypes).Distinct().ToArray()
                };
            }

            //We(and episerver) are adding IContentData as default, here we make sure to remove IContentData if we have any specific AllowedTypes set.
            if (injectedAllowedTypesAttribute.AllowedTypes.Any())
            {
                mergedAllowedTypesAttribute.AllowedTypes =
                    mergedAllowedTypesAttribute.AllowedTypes.Where(x => x != typeof(IContentData)).ToArray();
            }

            if (mergedAllowedTypesAttribute.AllowedTypes.Any() || mergedAllowedTypesAttribute.RestrictedTypes.Any())
            {
                return(mergedAllowedTypesAttribute);
            }

            return(null);
        }
예제 #10
0
            private static bool IsFhirElement(MemberInfo member, object criterium)
            {
                string fhirElementName     = (string)criterium;
                FhirElementAttribute feAtt = member.GetCustomAttribute <FhirElementAttribute>();

                if (feAtt != null)
                {
                    if (fhirElementName.Equals(feAtt.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(true);
                    }
                    if (fhirElementName.StartsWith(feAtt.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (feAtt.Choice == ChoiceType.DatatypeChoice || feAtt.Choice == ChoiceType.ResourceChoice)
                        {
                            AllowedTypesAttribute atAtt = member.GetCustomAttribute <AllowedTypesAttribute>();
                            if (atAtt != null)
                            {
                                foreach (Type allowedType in atAtt.Types)
                                {
                                    var  curTypeName = fhirElementName.Remove(0, feAtt.Name.Length);
                                    Type curType     = ModelInfo.GetTypeForFhirType(curTypeName);
                                    if (allowedType.IsAssignableFrom(curType))
                                    //                                        if (link.propertyName.Equals(feAtt.Name + ModelInfo.FhirCsTypeToString[allowedType], StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        return(true);
                                    }
                                    //if (fhirElementName.Equals(feAtt.Name + ModelInfo.FhirCsTypeToString[allowedType], StringComparison.InvariantCultureIgnoreCase))
                                    //    return true;
                                }
                            }
                        }
                    }
                }
                //if it has no FhirElementAttribute, it is not a FhirElement...
                return(false);
            }
예제 #11
0
 public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable <Attribute> attributes)
 {
     if (typeof(IExperimentTracking).IsAssignableFrom(metadata.Model.GetOriginalType()))
     {
         foreach (var modelMetadata in metadata.Properties)
         {
             var property = (ExtendedMetadata)modelMetadata;
             if (property.PropertyName == AddExperimentTrackingPropertyInit.ExperimentEventsPropertyName)
             {
                 var allowedTypes = new AllowedTypesAttribute(new[] { typeof(ExperimentEventTracking) });
                 property.InitializeFromAttributes(new Attribute[] { allowedTypes });
                 var fullName = typeof(ExperimentEventTracking).FullName;
                 if (fullName != null)
                 {
                     var allowedType = fullName.ToLower();
                     if (property.EditorConfiguration.ContainsKey("AllowedTypes"))
                     {
                         property.EditorConfiguration["AllowedTypes"] = new string[] { allowedType };
                     }
                     else
                     {
                         property.EditorConfiguration.Add("AllowedTypes", new string[] { allowedType });
                     }
                     if (property.EditorConfiguration.ContainsKey("AllowedDndTypes"))
                     {
                         property.EditorConfiguration["AllowedDndTypes"] = new string[] { allowedType + ".fragment" };
                     }
                     else
                     {
                         property.EditorConfiguration.Add("AllowedDndTypes",
                                                          new string[] { allowedType + ".fragment" });
                     }
                 }
             }
         }
     }
 }
예제 #12
0
            private List <Segment> BuildSegments(string classname, List <string> chain)
            {
                var segments = new List <Segment>();

                Type baseType = ModelInfo.FhirTypeToCsType[classname];

                foreach (string linkString in chain)
                {
                    var segment = new Segment();
                    segment.FhirType = baseType;
                    var predicateRegex = new Regex(@"(?<propname>[^\[]*)(\[(?<predicate>.*)\])?");
                    var match          = predicateRegex.Match(linkString);
                    var predicate      = match.Groups["predicate"].Value;
                    segment.Name = match.Groups["propname"].Value;

                    segment.Filter = ParsePredicate(predicate);

                    var matchingFhirElements = baseType.FindMembers(MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public, new MemberFilter(IsFhirElement), segment.Name);
                    if (matchingFhirElements.Any())
                    {
                        segment.Property = baseType.GetProperty(matchingFhirElements.First().Name);
                        // TODO: Ugly repetitive code from IsFhirElement(), since that method can only return a boolean...
                        FhirElementAttribute feAtt = segment.Property.GetCustomAttribute <FhirElementAttribute>();
                        if (feAtt != null)
                        {
                            if (feAtt.Choice == ChoiceType.DatatypeChoice || feAtt.Choice == ChoiceType.ResourceChoice)
                            {
                                AllowedTypesAttribute atAtt = segment.Property.GetCustomAttribute <AllowedTypesAttribute>();
                                if (atAtt != null)
                                {
                                    foreach (Type allowedType in atAtt.Types)
                                    {
                                        var  curTypeName = segment.Name.Remove(0, feAtt.Name.Length);
                                        Type curType     = ModelInfo.GetTypeForFhirType(curTypeName);
                                        if (allowedType.IsAssignableFrom(curType))
                                        //if (link.propertyName.Equals(feAtt.Name + ModelInfo.FhirCsTypeToString[allowedType], StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            segment.AllowedType = allowedType;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        segment.Property = baseType.GetProperty(segment.Name);
                    }
                    if (segment.Property == null)
                    {
                        break;
                    }

                    segments.Add(segment);
                    //infoChain.Add(Tuple.Create<Type, string, PropertyInfo, Type>(baseType, propertyname, info, choiceType));

                    if (segment.Property.PropertyType.IsGenericType)
                    {
                        //For instance AllergyIntolerance.Event, which is a List<Hl7.Fhir.Model.AllergyIntolerance.AllergyIntoleranceEventComponent>
                        baseType = segment.Property.PropertyType.GetGenericArguments().First();
                    }
                    else if (segment.AllowedType != null)
                    {
                        baseType = segment.AllowedType;
                    }
                    else
                    {
                        baseType = segment.Property.PropertyType;
                    }
                }

                return(segments);
            }