private static ParameterGenerator CreateDomainParameterGenerator(MethodInfo actionMethod, DomainAttribute attr, Type pType) { if (!string.IsNullOrEmpty(attr.Name)) { BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static; Type type = actionMethod.DeclaringType; MethodInfo /*?*/ paramGenMethod = type.GetMethod(attr.Name, flags); //try to see if it is a property if (paramGenMethod == null) { paramGenMethod = type.GetMethod("get_" + attr.Name, flags); } //check that the type is correct //finally try to see if it is a field FieldInfo paramField = null; if (paramGenMethod == null) { paramField = type.GetField(attr.Name, flags); } if (paramGenMethod != null) { ParameterInfo[] parameters = paramGenMethod.GetParameters(); if (parameters != null && parameters.Length > 0) { throw new ModelProgramUserException("Parameter generator '" + attr.Name + "' must not depend on arguments."); } if (!IsValidElementTypeOfEnumerationType(paramGenMethod.ReturnType, pType)) { throw new ModelProgramUserException("Parameter generator '" + attr.Name + "' does not support IEnumerable<T> such that values of type T can be assigned to a parameter of type " + pType); } } if (paramField != null) { if (!IsValidElementTypeOfEnumerationType(paramField.FieldType, pType)) { throw new ModelProgramUserException("Parameter generator '" + attr.Name + "' does not support IEnumerable<T> such that values of type T can be assigned to a parameter of type " + pType); } } if (paramGenMethod == null && paramField == null) { MemberInfo[] membersWithSameName = type.GetMember(attr.Name, flags | BindingFlags.Instance); if (membersWithSameName != null && membersWithSameName.Length > 0) { throw new ModelProgramUserException("Parameter generator " + attr.Name + " given by [Domain] attribute must be declared static."); } else { throw new ModelProgramUserException("Could not find parameter generator '" + attr.Name + "' specified by [Domain] attribute."); } } else { return(delegate() { object o = (paramField == null ? paramGenMethod.Invoke(null, null) : paramField.GetValue(null)); IEnumerable values = o as IEnumerable; if (null == values) { throw new InvalidOperationException("Parameter generator " + attr.Name + " of wrong type or returned null"); // to do: make into user error } Set <Term> result = Set <Term> .EmptySet; foreach (object value in values) { result = result.Add(AbstractValue.GetTerm((IComparable)value)); } return result; }); } } else { throw new ModelProgramUserException("[Domain] attribute missing required name"); } }
/// <summary> /// 验证是否满足特性的限定 /// </summary> /// <param name="method">执行的方法</param> /// <param name="letter">请求</param> /// <returns></returns> public static bool Verify(MemberInfo method, Letter letter) { List <RangeAttribute> ranges = WeishaAttr.GetAttrs <RangeAttribute>(method); if (ranges.Count < 1) { return(true); } //任意范围,有此特性则直接跳过 AnywhereAttribute any = WeishaAttr.GetAttr <AnywhereAttribute>(method); if (any != null && !any.Ignore) { return(true); } //局域网内访问 if (letter.Sever.IsIntranetIP) { IntranetAttribute intranet = WeishaAttr.GetAttr <IntranetAttribute>(method); if (any != null && !any.Ignore) { return(true); } } //本机访问 if (letter.Sever.IsLocalIP) { LocalhostAttribute local = WeishaAttr.GetAttr <LocalhostAttribute>(method); if (local != null && !local.Ignore) { return(true); } } //限制同域 DomainAttribute domainAttr = WeishaAttr.GetAttr <DomainAttribute>(method); if (domainAttr != null && !domainAttr.Ignore) { string host = letter.Sever.Domain.ToLower(); if (letter.HTTP_REFERER.ToLower().StartsWith("http://" + host)) { return(true); } if (letter.HTTP_REFERER.ToLower().StartsWith("https://" + host)) { return(true); } } //没有通过,则返回异常 string msg = string.Empty; for (int i = 0; i < ranges.Count; i++) { if (ranges[i] is LocalhostAttribute) { msg += "本机"; } if (ranges[i] is IntranetAttribute) { msg += "局域网"; } if (ranges[i] is DomainAttribute) { msg += "同域"; } if (i < ranges.Count - 1) { msg += ","; } } throw new Exception(string.Format("当前方法访问受限,仅限{0}访问", msg)); }