public void GetCustomAttributesWithAttributeTypeCallsTypeGetCustomAttributes()
        {
            // Arrange
            object[]    expected = new object[0];
            Mock <Type> mockType = new Mock <Type>();

            mockType.Setup(t => t.GetCustomAttributes(typeof(ObsoleteAttribute), true)).Returns(expected);
            ReflectedControllerDescriptor cd = new ReflectedControllerDescriptor(mockType.Object);

            // Act
            object[] returned = cd.GetCustomAttributes(typeof(ObsoleteAttribute), true);

            // Assert
            Assert.Same(expected, returned);
        }
        public void GetCustomAttributesCallsTypeGetCustomAttributes()
        {
            // Arrange
            object[]    expected = new object[0];
            Mock <Type> mockType = new Mock <Type>();

            mockType.Expect(t => t.GetCustomAttributes(true)).Returns(expected);
            ReflectedControllerDescriptor cd = new ReflectedControllerDescriptor(mockType.Object);

            // Act
            object[] returned = cd.GetCustomAttributes(true);

            // Assert
            Assert.AreSame(expected, returned);
        }
Пример #3
0
        private DefaultContractResolver GetExecutingControllerSerializer()
        {
            var controllerDescriptor = new ReflectedControllerDescriptor(ControllerContext.Controller.GetType());
            var action           = RouteData.GetRequiredString("action");
            var actionDescriptor = controllerDescriptor.FindAction(ControllerContext, action);

            Attributes.SerializerAttribute attribute = (Attributes.SerializerAttribute)
                                                           (actionDescriptor.GetCustomAttributes(typeof(Attributes.SerializerAttribute), true).FirstOrDefault() ??
                                                           controllerDescriptor.GetCustomAttributes(typeof(Attributes.SerializerAttribute), true).FirstOrDefault());
            if (attribute != null)
            {
                return(CacheHelper.GetFromCacheOrDefault <DefaultContractResolver>(HttpContext, attribute.Serializer));
            }
            return(null);
        }
        public static HelperResult DocumentationLink <T>(this HtmlHelper <T> htmlHelper, string label)
        {
            DocumentationAttribute attribute = null;
            var viewContext = htmlHelper.ViewContext;

            var controllerDescriptor = new ReflectedControllerDescriptor(viewContext.Controller.GetType());

            var actionName       = viewContext.RouteData.Values["action"] as string;
            var actionDescriptor = controllerDescriptor
                                   .GetCanonicalActions()
                                   .Where(x => String.Equals(x.ActionName, actionName, StringComparison.OrdinalIgnoreCase))
                                   .FirstOrDefault();

            if (actionDescriptor != null)
            {
                attribute = actionDescriptor
                            .GetCustomAttributes(typeof(DocumentationAttribute), true)
                            .Cast <DocumentationAttribute>()
                            .FirstOrDefault();
            }

            if (attribute == null)
            {
                attribute = controllerDescriptor
                            .GetCustomAttributes(typeof(DocumentationAttribute), true)
                            .Cast <DocumentationAttribute>()
                            .FirstOrDefault();
            }

            var link = attribute == null
                ? BaseUrl
                : string.Format("{0}{1}", BaseUrl, attribute.DocumentPath);

            return(new HelperResult(writer =>
            {
                var builder = new TagBuilder("a");
                builder.MergeAttribute("href", link);
                builder.MergeAttribute("target", "_blank");
                writer.Write(builder.ToString(TagRenderMode.StartTag));
                writer.Write(label);
                writer.Write(builder.ToString(TagRenderMode.EndTag));
            }));
        }
Пример #5
0
        public static bool HasRightsToAction(this Type controllerType, string actionName)
        {
            var controllerDescriptor = new ReflectedControllerDescriptor(controllerType);

            if (controllerDescriptor == null)
            {
                throw new ArgumentException("Invalid controller type!");
            }

            var actionDescriptor = controllerDescriptor.GetCanonicalActions()
                                   .FirstOrDefault(item => item.ActionName == actionName);

            if (actionDescriptor?.GetCustomAttributes(typeof(CustomAuthorizeAttribute), false).FirstOrDefault() is
                CustomAuthorizeAttribute attribute && !attribute.HasRightsToAction(actionDescriptor))
            {
                return(false);
            }

            attribute = controllerDescriptor.GetCustomAttributes(typeof(CustomAuthorizeAttribute), false)
                        .FirstOrDefault() as CustomAuthorizeAttribute ??
                        new CustomAuthorizeAttribute();
            return(attribute.HasRightsToAction(actionDescriptor));
        }
Пример #6
0
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult viewResult = filterContext.Result as ViewResult;

            if (viewResult != null && viewResult.Model != null)
            {
                IAdminModel model = viewResult.Model as IAdminModel;
                if (model == null)
                {
                    throw new InvalidOperationException(string.Format("Model ({0}) does not implement IAdminModel",
                                                                      viewResult.GetType().Name));
                }

                model.Notifications = App.Instance.Notifications.Get(filterContext.HttpContext.User);

                // this was populated by the AddInControllerFactory
                IControllerMetadata metadata =
                    (IControllerMetadata)
                    filterContext.RequestContext.HttpContext.Items[AddInControllerFactory.MetadataKey];

                string actionText = string.Empty;

                // TODO FIX THIS SOMEHOW, this is pretty crappy
                var allControllerExports =
                    App.Instance.Container.GetExports(
                        new ContractBasedImportDefinition(
                            ContractNames.AdminController,
                            AttributedModelServices.GetTypeIdentity(typeof(IController)),
                            Enumerable.Empty <KeyValuePair <string, Type> >(),
                            ImportCardinality.ZeroOrMore,
                            false,
                            false,
                            CreationPolicy.NonShared));

                List <Navigation> menu = new List <Navigation>();
                foreach (Export export in allControllerExports)
                {
                    IControllerMetadata controllerMetadata =
                        AttributedModelServices.GetMetadataView <IControllerMetadata>(export.Metadata);
                    ReflectedControllerDescriptor descriptor = new ReflectedControllerDescriptor(export.Value.GetType());

                    var controllerAttr =
                        descriptor.GetCustomAttributes(typeof(AdminControllerAttribute), true).FirstOrDefault() as
                        AdminControllerAttribute;

                    if (controllerAttr == null)
                    {
                        continue;
                    }

                    UrlHelper         urlHelper        = new UrlHelper(filterContext.RequestContext);
                    Uri               defaultTargetUrl = null;
                    List <Navigation> children         = new List <Navigation>();
                    foreach (var actionDescriptor in descriptor.GetCanonicalActions())
                    {
                        var actionAttr =
                            actionDescriptor.GetCustomAttributes(typeof(AdminActionAttribute), true).FirstOrDefault() as
                            AdminActionAttribute;
                        if (actionAttr == null)
                        {
                            continue;
                        }

                        // TODO replace anon class with concrete type?
                        string targetUrl = urlHelper.Action(actionAttr.Name,
                                                            controllerAttr.Name,
                                                            new
                        {
                            packageId      = controllerMetadata.PackageId,
                            packageVersion = controllerMetadata.PackageVersion
                        });

                        Uri target = new Uri(targetUrl, UriKind.RelativeOrAbsolute);

                        if (defaultTargetUrl == null || actionAttr.IsDefault)
                        {
                            defaultTargetUrl = target;
                        }

                        bool isActive = filterContext.ActionDescriptor.ActionName == actionDescriptor.ActionName &&
                                        filterContext.ActionDescriptor.ControllerDescriptor.ControllerType ==
                                        descriptor.ControllerType;

                        if (isActive)
                        {
                            actionText = actionAttr.Text;
                        }

                        Navigation navigation = new Navigation(null,
                                                               actionAttr.Order,
                                                               actionAttr.Text,
                                                               target,
                                                               isActive,
                                                               Enumerable.Empty <Navigation>());

                        children.Add(navigation);
                    }

                    bool isAnyChildActive = children.Any(n => n.IsActive);

                    // if there's only one child, ignore it
                    if (children.Count == 1)
                    {
                        children.Clear();
                    }

                    menu.Add(new Navigation(controllerAttr.Group,
                                            controllerAttr.Order,
                                            controllerAttr.Text,
                                            defaultTargetUrl,
                                            isAnyChildActive,
                                            children));
                }

                model.Navigation = menu.OrderBy(n => n.Order).ToArray();

                model.PageTitle = "LostDoc Administration " + metadata.Text;

                if (!string.IsNullOrWhiteSpace(actionText))
                {
                    model.PageTitle += string.Format(" - {0}", actionText);
                }
            }
        }