// This method contains some heuristics that will help determine the correct action name from a given MethodInfo
        // assuming the default sync / async invokers are in use. The logic's not foolproof, but it should be good enough
        // for most uses.
        private static string GetTargetActionName(MethodInfo methodInfo)
        {
            string methodName = methodInfo.Name;

            // do we know this not to be an action?
            if (methodInfo.IsDefined(typeof(NonActionAttribute), true /* inherit */))
            {
                throw new InvalidOperationException(methodName);
            }

            // has this been renamed?
            ActionNameAttribute nameAttr = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true /* inherit */).OfType <ActionNameAttribute>().FirstOrDefault();

            if (nameAttr != null)
            {
                return(nameAttr.Name);
            }

            // targeting an async action?
            if (methodName.EndsWith("Async", StringComparison.OrdinalIgnoreCase))
            {
                return(methodName.Substring(0, methodName.Length - "Async".Length));
            }
            if (methodName.EndsWith("Completed", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(methodName);
            }

            // fallback
            return(methodName);
        }
        public void NameProperty()
        {
            // Arrange
            ActionNameAttribute attr = new ActionNameAttribute("someName");

            // Act & Assert
            Assert.Equal("someName", attr.Name);
        }
Esempio n. 3
0
        public static string GetActionName(MethodInfo methodInfo, object[] actionAttributes)
        {
            ActionNameAttribute nameAttribute = OfType <ActionNameAttribute>(actionAttributes).FirstOrDefault();

            return(nameAttribute != null
                       ? nameAttribute.Name
                       : methodInfo.Name);
        }
Esempio n. 4
0
        // This method contains some heuristics that will help determine the correct action name from a given MethodInfo
        // assuming the default sync / async invokers are in use. The logic's not foolproof, but it should be good enough
        // for most uses.
        private static string GetTargetActionName(MethodInfo methodInfo)
        {
            string methodName = methodInfo.Name;

            // do we know this not to be an action?
            if (
                methodInfo.IsDefined(
                    typeof(NonActionAttribute),
                    true /* inherit */
                    )
                )
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              MvcResources.ExpressionHelper_CannotCallNonAction,
                              methodName
                              )
                          );
            }

            // has this been renamed?
            ActionNameAttribute nameAttr = methodInfo
                                           .GetCustomAttributes(
                typeof(ActionNameAttribute),
                true     /* inherit */
                )
                                           .OfType <ActionNameAttribute>()
                                           .FirstOrDefault();

            if (nameAttr != null)
            {
                return(nameAttr.Name);
            }

            // targeting an async action?
            if (methodInfo.DeclaringType.IsSubclassOf(typeof(AsyncController)))
            {
                if (methodName.EndsWith("Async", StringComparison.OrdinalIgnoreCase))
                {
                    return(methodName.Substring(0, methodName.Length - "Async".Length));
                }
                if (methodName.EndsWith("Completed", StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  MvcResources.ExpressionHelper_CannotCallCompletedMethod,
                                  methodName
                                  )
                              );
                }
            }

            // fallback
            return(methodName);
        }
Esempio n. 5
0
        protected override string GetName()
        {
            ActionNameAttribute nameAttr = GetCustomAttributes(typeof(ActionNameAttribute), inherit: true)
                                           .Cast <ActionNameAttribute>()
                                           .SingleOrDefault();

            return((nameAttr != null) ?
                   nameAttr.Name
            : null);
        }
        public void IsValidForRequestReturnsTrueIfGivenNameMatches()
        {
            // Arrange
            ActionNameAttribute attr = new ActionNameAttribute("Bar");

            // Act
            bool returned = attr.IsValidName(null, "bar", null);

            // Assert
            Assert.True(returned);
        }
        public void IsValidForRequestReturnsFalseIfGivenNameDoesNotMatch()
        {
            // Arrange
            ActionNameAttribute attr = new ActionNameAttribute("Bar");

            // Act
            bool returned = attr.IsValidName(null, "foo", null);

            // Assert
            Assert.False(returned);
        }
Esempio n. 8
0
        private static string GetTargetActionName(MethodInfo methodInfo)
        {
            string name = methodInfo.Name;

            if (methodInfo.IsDefined(typeof(NonActionAttribute), true))
            {
                throw new InvalidOperationException(String.Format("The method {0} is not a action.", methodInfo.Name));
            }
            ActionNameAttribute actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).OfType <ActionNameAttribute>().FirstOrDefault <ActionNameAttribute>();

            if (actionNameAttribute != null)
            {
                return(actionNameAttribute.Name);
            }
            return(name);
        }
Esempio n. 9
0
        private static string GetActionName(MethodInfo method, bool allowLegacyAsyncActions)
        {
            // Check for ActionName attribute
            object[] nameAttributes = method.GetCustomAttributes(typeof(ActionNameAttribute), inherit: true);
            if (nameAttributes.Length > 0)
            {
                ActionNameAttribute nameAttribute = nameAttributes[0] as ActionNameAttribute;
                if (nameAttribute != null)
                {
                    return(nameAttribute.Name);
                }
            }

            const string AsyncMethodSuffix = "Async";

            if (allowLegacyAsyncActions && method.Name.EndsWith(AsyncMethodSuffix, StringComparison.OrdinalIgnoreCase))
            {
                return(method.Name.Substring(0, method.Name.Length - AsyncMethodSuffix.Length));
            }

            return(method.Name);
        }
            public SchemeAction(MethodInfo methodInfo)
            {
                ActionNameAttribute actionNameAttribute = methodInfo.GetCustomAttribute <ActionNameAttribute>();

                if (actionNameAttribute != null)
                {
                    name = actionNameAttribute.Name;
                }
                else
                {
                    name = methodInfo.Name;
                }

                if (methodInfo.ReturnType != typeof(void))
                {
                    result = new SchemeActionResult(methodInfo.ReturnType);
                }

                foreach (ParameterInfo parameterInfo in methodInfo.GetParameters())
                {
                    parameters.Add(new SchemeActionParam(parameterInfo));
                }
            }
Esempio n. 11
0
        public static void RegisterActions(ApplicationDbContext context)
        {
            var q = from type in Assembly.GetExecutingAssembly().GetTypes()
                    where type.IsClass && type.Namespace != null && type.Namespace.Contains("Controller")
                    select type;
            List <string> activeActions = new List <string>();

            foreach (Type controller in q)
            {
                var actions = controller.GetMethods().ToList();
                foreach (MethodInfo mi in actions)
                {
                    AuthorizationService attribute = mi.GetCustomAttribute(typeof(AuthorizationService), false) as AuthorizationService;
                    if (attribute != null)
                    {
                        var actionName = mi.Name;
                        ActionNameAttribute customAction = mi.GetCustomAttribute(typeof(ActionNameAttribute), false) as ActionNameAttribute;
                        if (customAction != null)
                        {
                            actionName = customAction.Name;
                        }
                        string controllerName      = controller.Name.Substring(0, controller.Name.Length - 10);
                        bool   isHttpPost          = mi.GetCustomAttributes(typeof(HttpPostAttribute)).Count() > 0;
                        bool   isBackGroundProcess = mi.ReturnType != typeof(ActionResult) && mi.ReturnType != typeof(IActionResult);
                        var    action = context.ApplicationAction.Where(p => p.ActionName == actionName && p.ControllerName == controllerName).FirstOrDefault();
                        // && p.IsHttpPOST == isHttpPost)
                        var affected = 0;
                        if (action == null)
                        {
                            action = new ApplicationAction
                            {
                                ApplicationAction_Id = $"{controllerName} - {actionName}",
                                ActionName           = actionName,
                                ControllerName       = controllerName,
                                AccessNeeded         = attribute.Assignable,
                                IsHttpPOST           = isHttpPost,
                                Description          = attribute.ActionDescription
                            };
                            context.ApplicationAction.Add(action);
                        }
                        else
                        {
                            action.ApplicationAction_Id = $"{controllerName} - {actionName}";
                            action.ActionName           = actionName;
                            action.ControllerName       = controllerName;
                            action.AccessNeeded         = attribute.Assignable;
                            action.IsHttpPOST           = isHttpPost;
                            action.Description          = attribute.ActionDescription;
                            context.ApplicationAction.Update(action);
                        }
                        affected = context.SaveChanges();
                        if (affected > 0)
                        {
                            activeActions.Add(action.Id);
                        }
                    }
                }
            }

            if (activeActions.Any())
            {
                var actionsToRemove = context.ApplicationAction.Where(a => !activeActions.Contains(a.Id)).ToList();
                if (actionsToRemove.Count > 0)
                {
                    context.RemoveRange(actionsToRemove);
                    context.SaveChanges();
                }

                AJMPActionPermission aJMPActionPermission = new AJMPActionPermission(context);
                aJMPActionPermission.Setup();
            }
            else
            {
                if (context.ApplicationAction.Any())
                {
                    context.ApplicationAction.RemoveRange(context.ApplicationAction.ToList());
                    context.SaveChanges();
                }
            }
        }
Esempio n. 12
0
        public void DeleteConfirmedHasActionNameAttributeWithCorrectName()
        {
            ActionNameAttribute attr = (ActionNameAttribute)typeof(EventController).GetMethod(nameof(EventController.DeleteConfirmed), new Type[] { typeof(int) }).GetCustomAttribute(typeof(ActionNameAttribute));

            Assert.Equal(attr.Name, "Delete");
        }