private static IEnumerable<FilterAttribute> ExecutedAttributesOf(ActionDescriptor actionDescriptor)
        {
            var actionCustomAttributes = actionDescriptor.GetCustomAttributes(inherit: true).OfType<FilterAttribute>().OrderByDescending(attribute => attribute.Order);
            var controllerCustomAttributes = actionDescriptor.ControllerDescriptor.GetCustomAttributes(inherit: true).OfType<FilterAttribute>().OrderByDescending(attribute => attribute.Order);

            return actionCustomAttributes.Concat(controllerCustomAttributes);
        }
Esempio n. 2
0
        public IEnumerable<System.Web.Mvc.Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            //if ((controllerContext.Controller is ProductController || controllerContext.Controller is ProductsController) &&
            //    actionDescriptor.ActionName.Equals("ProductList", 
            //    StringComparison.InvariantCultureIgnoreCase))
            if ((actionDescriptor.ControllerDescriptor.ControllerName == "Product" || actionDescriptor.ControllerDescriptor.ControllerName == "Products") &&
                actionDescriptor.ActionName.Equals("ProductList", StringComparison.InvariantCultureIgnoreCase))
            {
                if (controllerContext.RouteData.Values["area"] != null &&
                    (controllerContext.RouteData.Values["area"].ToString() == "Admin" || controllerContext.RouteData.Values["area"].ToString() == "Vendor"))
                {
                    //return new List<System.Web.Mvc.Filter>() { new System.Web.Mvc.Filter(this, FilterScope.Action, 0) };
                }
            }

            if ((actionDescriptor.ControllerDescriptor.ControllerName == "Home") &&
                actionDescriptor.ActionName.Equals("Index",
                StringComparison.InvariantCultureIgnoreCase))
            {
                return new List<System.Web.Mvc.Filter>() { new System.Web.Mvc.Filter(this, FilterScope.Action, 0) };
            }

            var a = actionDescriptor.GetCustomAttributes(true);
            var b = actionDescriptor.GetFilterAttributes(true);
            var c = actionDescriptor.GetParameters();

            return new List<System.Web.Mvc.Filter>();
        }
 private static ThemedConditionalAttribute GetThemedAttribute(ActionDescriptor descriptor)
 {
     return
         descriptor.GetCustomAttributes(typeof(ThemedConditionalAttribute), true).Concat(
             descriptor.ControllerDescriptor.GetCustomAttributes(typeof(ThemedConditionalAttribute), true)).
             OfType<ThemedConditionalAttribute>().FirstOrDefault();
 }
 private static IEnumerable<FilterAttributeMetadata> LoadFilterAttributes(ActionDescriptor actionDescriptor)
 {
     return actionDescriptor.GetCustomAttributes(typeof(IFilterAttribute), true).Cast<IFilterAttribute>().
                             Select(x => new FilterAttributeMetadata(x, FilterScope.Action)).
                             Union(actionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(IFilterAttribute), true).
                             Cast<IFilterAttribute>().
                             Select(x => new FilterAttributeMetadata(x, FilterScope.Controller)));
 }
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            var anonymousAction = actionDescriptor.GetCustomAttributes(typeof (AllowAnonymousAttribute), false);

            if (anonymousAction.Length == 0)
            {
                yield return new Filter(DependencyResolver.Current.GetService<AuthorizationRequiredFilter>(), FilterScope.Controller, 1);
            }
            else
            {
                yield return new Filter(new FilterAttributeFilterProvider(), FilterScope.Controller, 1);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 创建缓存策略
        /// </summary>
        /// <param name="context">控制器上下文</param>
        /// <param name="action">Action 信息</param>
        /// <param name="parameters">Action 参数</param>
        /// <returns>缓存策略</returns>
        protected override CachePolicy CreateCachePolicy( ControllerContext context, ActionDescriptor action, IDictionary<string, object> parameters )
        {
            var httpMethod = context.HttpContext.Request.HttpMethod;
              if ( !httpMethod.EqualsIgnoreCase( "get" ) && !httpMethod.EqualsIgnoreCase( "header" ) )//如果不是GET或Header请求,都不予缓存。
            return null;

              //如果存在 Cacheable 筛选器,则不再提供默认策略。
              if ( action.GetCustomAttributes( typeof( CacheableAttribute ), true ).Any() || action.ControllerDescriptor.GetCustomAttributes( typeof( CacheableAttribute ), true ).Any() )
            return null;

              ControllerCachePolicyProvider provider = GetControllerProvider( MvcEnvironment.GetAreaName( context ), action.ControllerDescriptor.ControllerName );
              if ( provider != null )
              {
            var policy = provider.CreateCachePolicy( context, action, parameters );
            if ( policy != null )
              return policy;
              }

              return MvcEnvironment.CreateCachePolicy( context, action, parameters );
        }
 private IEnumerable<AdminAuthorizeAttribute> GetAdminAuthorizeAttributes(ActionDescriptor descriptor)
 {
     return descriptor.GetCustomAttributes(typeof(AdminAuthorizeAttribute), true)
         .Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof(AdminAuthorizeAttribute), true))
         .OfType<AdminAuthorizeAttribute>();
 }
Esempio n. 8
0
 /// <summary>
 /// Loads the configuration-based filters and
 /// returns them to the MVC runtime
 /// </summary>
 /// <param name="controllerCtx">
 /// The current controller
 /// </param>
 /// <param name="actionDesc">
 /// The current action
 /// </param>
 /// <returns>
 /// An enumeration of configured action filters
 /// </returns>
 public IEnumerable<System.Web.Mvc.Filter> GetFilters(
     ControllerContext controllerCtx,
     ActionDescriptor actionDesc)
 {
     String controller = actionDesc.ControllerDescriptor.ControllerName;
     String action = actionDesc.ActionName;
     return this
        .Cast<FilterElement>()
        // match the controller name or all controllers
        .Where(f =>
       String.IsNullOrEmpty(f.Controller) ||
       String.Compare(f.Controller, controller, false) == 0
        )
        // match the action name or all actions
        .Where(f =>
       String.IsNullOrEmpty(f.Action) ||
       String.Compare(f.Action, action, false) == 0
        )
        // action attributes override configured attributes
        .Where(f =>
       !actionDesc.GetCustomAttributes(f.RuntimeType, true).Any()
        )
        .Select(f => f.Filter);
 }
Esempio n. 9
0
 private static IEnumerable<FrontMenuAttribute> GetAdminAttributes(ActionDescriptor descriptor)
 {
     return descriptor.GetCustomAttributes(typeof(FrontMenuAttribute), true)
         .Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof(FrontMenuAttribute), true))
         .OfType<FrontMenuAttribute>();
 }
 protected override IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(ActionDescriptor actionDescriptor)
 {
     return actionDescriptor.GetCustomAttributes(typeof(IDirectRouteFactory), inherit: true).Cast<IDirectRouteFactory>().ToArray();
 }
 private IEnumerable<PermissionAuthorizationAttribute> GetPermissionAuthorizationAttributes(ActionDescriptor descriptor)
 {
     return descriptor.GetCustomAttributes(typeof(PermissionAuthorizationAttribute), true)
         .Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof(PermissionAuthorizationAttribute), true))
         .OfType<PermissionAuthorizationAttribute>();
 }
Esempio n. 12
0
		public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
		{
			DVSAuthorizeFilterAttribute controllerAttribute = (DVSAuthorizeFilterAttribute)controllerContext.Controller.GetType().GetCustomAttributes(true).FirstOrDefault(e => e.GetType() == typeof(DVSAuthorizeFilterAttribute));
			DVSAuthorizeFilterAttribute actionAttribute = (DVSAuthorizeFilterAttribute)actionDescriptor.GetCustomAttributes(true).FirstOrDefault(e => e.GetType() == typeof(DVSAuthorizeFilterAttribute));
			if (actionAttribute != null)
			{
				actionAttribute.AccessHelper = _kernel.Get<IUserAccessHelper>();
				return new[] { new Filter(actionAttribute, FilterScope.Action, 1) };
			}
			if (controllerAttribute != null)
			{
				controllerAttribute.AccessHelper = _kernel.Get<IUserAccessHelper>();
				return new[] { new Filter(controllerAttribute, FilterScope.Controller, 1) };
			}
			return Enumerable.Empty<Filter>();
		}
		internal static AuthorizationMode GetAuthorizationMode(ActionDescriptor actionDescriptor, System.Web.Routing.RequestContext requestContext, out string schemaId, out string actionId, out ICredentialValidator validator)
		{
			schemaId = null;
			actionId = null;
			validator = null;

			//查找位于Action方法的授权标记
			var attribute = (AuthorizationAttribute)actionDescriptor.GetCustomAttributes(typeof(Zongsoft.Security.Membership.AuthorizationAttribute), true).FirstOrDefault();

			if(attribute == null)
			{
				//查找位于Controller类的授权标记
				attribute = (AuthorizationAttribute)actionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(Zongsoft.Security.Membership.AuthorizationAttribute), true).FirstOrDefault();

				if(attribute == null)
					return AuthorizationMode.Disabled;

				validator = attribute.Validator;

				if(attribute.Mode == AuthorizationMode.Required)
				{
					schemaId = string.IsNullOrWhiteSpace(attribute.SchemaId) ? GetSchemaId(actionDescriptor.ControllerDescriptor.ControllerName, requestContext.RouteData.Values["area"] as string) : attribute.SchemaId;
					actionId = actionDescriptor.ActionName;
				}

				return attribute.Mode;
			}

			validator = attribute.Validator;

			if(attribute.Mode != AuthorizationMode.Required)
				return attribute.Mode;

			schemaId = attribute.SchemaId;
			actionId = string.IsNullOrWhiteSpace(attribute.ActionId) ? actionDescriptor.ActionName : attribute.ActionId;

			if(string.IsNullOrWhiteSpace(schemaId))
			{
				var controllerAttribute = (AuthorizationAttribute)Attribute.GetCustomAttribute(actionDescriptor.ControllerDescriptor.ControllerType, typeof(Zongsoft.Security.Membership.AuthorizationAttribute), true);

				if(controllerAttribute == null || string.IsNullOrWhiteSpace(controllerAttribute.SchemaId))
					schemaId = GetSchemaId(actionDescriptor.ControllerDescriptor.ControllerName, requestContext.RouteData.Values["area"] as string);
				else
					schemaId = controllerAttribute.SchemaId;
			}

			return attribute.Mode;
		}
		internal static AuthorizationAttribute GetAuthorizationAttribute(ActionDescriptor actionDescriptor)
		{
			//查找位于Action方法的授权标记
			var attribute = (AuthorizationAttribute)actionDescriptor.GetCustomAttributes(typeof(Zongsoft.Security.Membership.AuthorizationAttribute), true).FirstOrDefault();

			if(attribute == null)
			{
				//查找位于Controller类的授权标记
				attribute = (AuthorizationAttribute)actionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(Zongsoft.Security.Membership.AuthorizationAttribute), true).FirstOrDefault();
			}

			return attribute;
		}
 protected virtual IEnumerable<AuthorizeAttribute> GetAuthorizeAttributes(ActionDescriptor actionDescriptor, ControllerContext controllerContext)
 {
     return actionDescriptor.GetCustomAttributes(typeof(AuthorizeAttribute), true).OfType
                    <AuthorizeAttribute>().ToList()
                    .Union(
                        actionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(AuthorizeAttribute), true).OfType
                            <AuthorizeAttribute>().ToList());
 }
        private static string GetSamplePath(RequestContext requestContext, ActionDescriptor action) {
            var urlHelper = new UrlHelper(requestContext);

            var actionNameAttrib = action.GetCustomAttributes(inherit: true).OfType<ActionNameAttribute>().FirstOrDefault();

            // This is tricky because some of the action parameters may not be meant to come from the route.
            // e.g. they could come from a POST body.
            // In that case, they may end up as bogus query string params on the path, which is a bit buggy
            var routeValues = new RouteValueDictionary();
            foreach (ParameterDescriptor param in action.GetParameters()) {
                routeValues.Add(param.ParameterName, GetDefaultValue(param));
            }

            return urlHelper.Action(
                actionNameAttrib != null ? actionNameAttrib.Name : action.ActionName,
                action.ControllerDescriptor.ControllerName.ToLowerInvariant(),
                routeValues);
        }
        /// <summary>
        ///获取Action的Http标记
        /// </summary>
        /// <param name="descriptor">The descriptor.</param>
        /// <returns></returns>
        private string GetActionHttpAttr(ActionDescriptor descriptor)
        {
            var attrs = descriptor.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), false);
            object attrType = typeof(HttpGetAttribute);//默认使用HttpGet
            if (attrs.Length > 0)
                attrType = attrs[0];

            string fullTypeName = attrType.ToString();
            string typeName = fullTypeName.Substring(fullTypeName.LastIndexOf('.') + 1);
            var actionHttpAttr = typeName.Replace("Attribute", string.Empty);
            return actionHttpAttr;
        }