Commits

Inside Zhou  committed c625b37

为Asp.net Mvc增加将action标记为一个权限的Attribute。

  • Participants
  • Parent commits 71deeac

Comments (0)

Files changed (5)

File Inside.Util.csproj

     <Compile Include="Web\HttpRequestBaseExtension.cs" />
     <Compile Include="Web\MIME.cs" />
     <Compile Include="Web\MvcControllerBase.cs" />
+    <Compile Include="Web\PriviledgeDescriptionAttribute.cs" />
     <Compile Include="Web\UriExtension.cs" />
     <Compile Include="Web\ViewAsFileResult.cs" />
     <Compile Include="Web\WSHelper.cs" />

File MiscExtension.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 
 namespace Inside.Util {
 
 			return result;
 		}
+
+		/// <summary>
+		/// 获取第一个类型为T的Attribute,若无则返回null。
+		/// </summary>
+		public static T GetAttribute<T>(this MethodInfo src) where T : Attribute {
+			return (T)src.GetCustomAttributes(false).FirstOrDefault((attr) => { return attr is T; });
+		}
 	}
 }

File Web/AuthorizeExAttribute.cs

 			}
 		}
 	}
-}
+}

File Web/MvcControllerBase.cs

 using System.Diagnostics;
 using System.Linq;
 using System.Text.RegularExpressions;
+using System.Reflection;
+using System.Web.Compilation;
 using System.Web;
 using System.Web.Security;
 using System.Web.Mvc;
 
 namespace Inside.Util.Web {
 	public abstract class MvcControllerBase : Controller {
-		protected bool Debugging { get; private set; }
+		public static List<Type> Controllers {
+			get {
+				var types = new List<Type>();
+
+				foreach(Assembly assembly in BuildManager.GetReferencedAssemblies()) {
+					foreach(var t in assembly.GetTypes()) {
+						if(t != null
+							&& t.IsPublic
+							&& t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
+							&& !t.IsAbstract
+							&& typeof(IController).IsAssignableFrom(t)) {
+
+							types.Add(t);
+						}
+					}
+				}
+
+				return types;
+			}
+		}
+
+		public static List<MethodInfo> Actions {
+			get {
+				var methods = new List<MethodInfo>();
+				foreach(var c in Controllers) {
+					methods.AddRange(c.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod).Where((m) => {
+						return m.ReturnType == typeof(ActionResult) || m.ReturnType.IsInstanceOfType(typeof(ActionResult));
+					}));
+				}
+
+				return methods;
+			}
+		}
+
+		public bool Debugging { get; private set; }
 
 		dynamic _useragent;
-		protected dynamic UserAgent {
+		public dynamic UserAgent {
 			get {
 				if(null == this._useragent) {
 					this._useragent = new DyObj();

File Web/PriviledgeDescriptionAttribute.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Inside.Util.Web {
+	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
+	public class PriviledgeDescriptionAttribute : Attribute {
+		public string ID { get; private set; }
+		public string Name { get; private set; }
+		public string Description { get; set; }
+
+		int _state = 0;
+		public int State {
+			get {
+				return this._state;
+			}
+			set {
+				this._state = value;
+			}
+		}
+
+		public PriviledgeDescriptionAttribute(string id, string name) {
+			if(string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name)) {
+				throw new ArgumentNullException();
+			}
+
+			this.ID = id;
+			this.Name = name;
+		}
+	}
+}