/* * /// <summary> * /// check authentication attributes for the class * /// </summary> * protected virtual void MapClassAuth() * { * object[] attributes = GetType().GetCustomAttributes(true); * foreach (object attribute in attributes) * { * if (attribute.GetType() == typeof (AuthenticatorAttribute)) * AddAuthAttribute(ClassMethodName, attribute); * if (attribute.GetType() == typeof (AuthenticationRequiredAttribute)) * AddCheckAuthAttribute(ClassMethodName, attribute); * } * } */ /// <summary> /// This method goes through all methods in the controller and /// adds them to a dictionary. They are later used to invoke /// the correct method depending on the Uri. /// </summary> /// <exception cref="InvalidOperationException">Authentication validator have already been specified.</exception> private void MapMethods() { lock (_methods) { // already mapped. if (_methods.Count > 0) { return; } object[] controllerNameAttrs = GetType().GetCustomAttributes(typeof(ControllerNameAttribute), false); if (controllerNameAttrs.Length > 0) { ControllerName = ((ControllerNameAttribute)controllerNameAttrs[0]).Name; } else { ControllerName = GetType().Name; if (ControllerName.Contains("Controller")) { ControllerName = ControllerName.Replace("Controller", ""); } ControllerName = ControllerName.ToLower(); } MethodInfo[] methods = GetType().GetMethods(BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance); foreach (MethodInfo info in methods) { ParameterInfo[] parameters = info.GetParameters(); // find regular render methods if (parameters.Length == 0 && info.ReturnType == typeof(string)) { string name = info.Name.ToLower(); if (name.Length > 3 && (name.Substring(0, 4) == "get_" || name.Substring(0, 4) == "set_")) { continue; } if (name == "tostring") { continue; } // Add authenticators object[] authAttributes = info.GetCustomAttributes(true); foreach (object attribute in authAttributes) { if (attribute.GetType() == typeof(AuthRequiredAttribute)) { _authMethods.Add(info.Name.ToLower(), ((AuthRequiredAttribute)attribute).Level); } } _methods.Add(info.Name.ToLower(), info); } // find raw handlers object[] attributes = info.GetCustomAttributes(typeof(RawHandlerAttribute), true); if (attributes.Length >= 1 && info.ReturnType == typeof(void) && parameters.Length == 0) { // Add authenticators object[] authAttributes = info.GetCustomAttributes(true); foreach (object attribute in authAttributes) { if (attribute.GetType() == typeof(AuthRequiredAttribute)) { _authMethods.Add(info.Name.ToLower(), ((AuthRequiredAttribute)attribute).Level); } } _binaryMethods.Add(info.Name.ToLower(), info); } } //foreach methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); foreach (MethodInfo info in methods) { ParameterInfo[] parameters = info.GetParameters(); // find before filters. if (parameters.Length != 0 || info.ReturnType != typeof(bool)) { continue; } object[] authAttributes = info.GetCustomAttributes(true); foreach (object attribute in authAttributes) { if (attribute.GetType() == typeof(AuthenticationValidatorAttribute)) { if (_authValidator != null) { throw new InvalidOperationException("Authentication validator have already been specified."); } _authValidator = info; } else if (attribute.GetType() == typeof(BeforeFilterAttribute)) { BeforeFilterAttribute attr = (BeforeFilterAttribute)attribute; LinkedListNode <MethodInfo> node = new LinkedListNode <MethodInfo>(info); switch (attr.Position) { case FilterPosition.First: _beforeFilters.AddFirst(node); break; case FilterPosition.Last: _beforeFilters.AddLast(node); break; default: if (_lastMiddleFilter == null) { _beforeFilters.AddLast(node); } else { _beforeFilters.AddAfter(_lastMiddleFilter, node); } _lastMiddleFilter = node; break; } } } } // Map index method. MethodInfo mi = GetType().GetMethod("Index", BindingFlags.Public | BindingFlags.Instance); if (mi != null && mi.ReturnType == typeof(string) && mi.GetParameters().Length == 0) { DefaultMethod = "Index"; } } }