Ejemplo n.º 1
0
        public void BaseDefaultsModification_7()
        {
            MetaModel m   = MetaModel.Default;
            var       req = new FakeHttpWorkerRequest();
            var       ctx = new HttpContext(req);

            HttpContext.Current = ctx;

            RouteCollection routes = RouteTable.Routes;

            routes.Clear();

            var ddr = new DynamicDataRoute("{table}/{action}.aspx")
            {
                Table        = null,
                Model        = m,
                RouteHandler = new MyDynamicDataRouteHandler()
            };

            routes.Add(ddr);

            Assert.IsNotNull(ddr, "#A1");
            Assert.IsNull(ddr.Defaults, "#A1-1");
            var rd = new RouteData();
            var hc = new HttpContextWrapper(HttpContext.Current);

            ddr.GetVirtualPath(new RequestContext(hc, rd), null);
            Assert.IsNull(ddr.Defaults, "#B1");
        }
Ejemplo n.º 2
0
        private static void InitializeDynamicData(RouteCollection routes, string root, IConfiguration configuration)
        {
            try
            {
                DefaultModel.RegisterContext(
                    new EFCodeFirstDataModelProvider(
                        () => new EntitiesContext(configuration.SqlConnectionString, readOnly: false)), // DB Admins do not need to respect read-only mode.
                    configuration: new ContextConfiguration {
                    ScaffoldAllTables = true
                });
            }
            catch (SqlException e)
            {
                QuietlyLogException(e);
                return;
            }
            catch (DataException e)
            {
                QuietlyLogException(e);
                return;
            }

            // This route must come first to prevent some other route from the site to take over
            _route = new DynamicDataRoute(root + "/{table}/{action}")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model       = DefaultModel
            };
            routes.Insert(0, _route);

            routes.MapPageRoute(
                "dd_default",
                root,
                "~/Areas/Admin/DynamicData/Default.aspx");
        }
Ejemplo n.º 3
0
        public void BaseDefaultsModification_8()
        {
            MetaModel m   = MetaModel.Default;
            var       req = new FakeHttpWorkerRequest();
            var       ctx = new HttpContext(req);

            HttpContext.Current = ctx;

            RouteCollection routes = RouteTable.Routes;

            routes.Clear();

            var ddr = new DynamicDataRoute("{table}/{action}.aspx")
            {
                Table        = String.Empty,
                Model        = m,
                RouteHandler = new MyDynamicDataRouteHandler()
            };

            routes.Add(ddr);
            var rd = new RouteData();
            var hc = new HttpContextWrapper(HttpContext.Current);

            AssertExtensions.Throws <ArgumentException> (() => {
                ddr.GetVirtualPath(new RequestContext(hc, rd), null);
            }, "#A1");
        }
Ejemplo n.º 4
0
        public void GetRouteData()
        {
            var r = new DynamicDataRoute("{table}/{action}.aspx");

            // We need one which overloads CreateHandler
            r.RouteHandler = new MyDynamicDataRouteHandler();

            var wrapper = new MyHttpContextWrapper();
            var request = wrapper.Request as MyHttpRequestWrapper;

            request.SetProperty("AppRelativeCurrentExecutionFilePath", "~/NoSuchTable/List.aspx");
            request.SetProperty("PathInfo", String.Empty);

            // This must be non-null because DynamicData doesn't care to check whether the returned
            // value is null or not...
            request.SetProperty("QueryString", new NameValueCollection());

            // It appears .NET checks whether the indicated table exists - if not, GetRouteData will return
            // null (even though the Route class will find a match)
            RouteData rd = r.GetRouteData(wrapper);

            Assert.IsNull(rd, "#A1");

            request.SetProperty("AppRelativeCurrentExecutionFilePath", "~/BazTable/List.aspx");
            rd = r.GetRouteData(wrapper);
            Assert.IsNotNull(rd, "#B1");
        }
Ejemplo n.º 5
0
        public void GetTableFromRouteData()
        {
            var r  = new DynamicDataRoute("x");
            var rd = new RouteData();

            // rd must have "Table" value
            r.GetTableFromRouteData(rd);
        }
Ejemplo n.º 6
0
        public void GetTableFromRouteData3()
        {
            var r  = new DynamicDataRoute("x");
            var rd = new RouteData();

            rd.Values["Table"] = "FooTable";
            var t = r.GetTableFromRouteData(rd);
        }
Ejemplo n.º 7
0
        public void GetActionFromRouteData()
        {
            var r  = new DynamicDataRoute("x2");
            var rd = new RouteData();

            // rd must have "Action" value
            r.GetActionFromRouteData(rd);
        }
Ejemplo n.º 8
0
        public void RouteHandler()
        {
            var r = new DynamicDataRoute("{table}/{action}.aspx");

            Assert.IsNotNull(r.RouteHandler, "#A1");
            Assert.AreEqual(typeof(DynamicDataRouteHandler), r.RouteHandler.GetType(), "#A1-1");

            r.RouteHandler = null;
            Assert.IsNull(r.RouteHandler, "#A2");
        }
Ejemplo n.º 9
0
        public void GetTableFromRouteData2()
        {
            var r = new DynamicDataRoute("x");

            r.Model = new MetaModel();
            var rd = new RouteData();

            rd.Values["Table"] = "y";
            r.GetTableFromRouteData(rd);              // no such table
        }
Ejemplo n.º 10
0
        public void GetActionFromRouteData2()
        {
            var r  = new DynamicDataRoute("x");
            var rd = new RouteData();

            rd.Values["Action"] = "y";
            var a = r.GetActionFromRouteData(rd);

            Assert.AreEqual("y", a);
        }
Ejemplo n.º 11
0
        public void BaseDefaultsModification_10()
        {
            MetaModel m   = MetaModel.Default;
            var       req = new FakeHttpWorkerRequest();
            var       ctx = new HttpContext(req);

            HttpContext.Current = ctx;

            RouteCollection routes = RouteTable.Routes;

            routes.Clear();

            var ddr = new DynamicDataRoute("{table}/{action}.aspx")
            {
                Defaults = new RouteValueDictionary()
                {
                    { "Table", "FooWithDefaultsTable" }
                },
                Table        = "BazTable",
                Model        = m,
                RouteHandler = new MyDynamicDataRouteHandler()
            };

            routes.Add(ddr);

            Assert.IsNotNull(ddr, "#A1");
            Assert.IsNotNull(ddr.Defaults, "#A1-1");
            var rd = new RouteData();
            var hc = new HttpContextWrapper(HttpContext.Current);

            ddr.GetVirtualPath(new RequestContext(hc, rd), null);
            Assert.IsNotNull(ddr.Defaults, "#B1");
            Assert.AreEqual(1, ddr.Defaults.Count, "#B1-1");
            Assert.AreEqual("BazTable", ddr.Defaults["Table"], "#B1-2");

            ddr.Table = "AnotherTable";
            ddr.GetVirtualPath(new RequestContext(hc, rd), null);
            Assert.IsNotNull(ddr.Defaults, "#C1");
            Assert.AreEqual(1, ddr.Defaults.Count, "#C1-1");
            Assert.AreEqual("BazTable", ddr.Defaults["Table"], "#C1-2");
        }
Ejemplo n.º 12
0
        public void BaseDefaultsModification_9()
        {
            MetaModel m   = MetaModel.Default;
            var       req = new FakeHttpWorkerRequest();
            var       ctx = new HttpContext(req);

            HttpContext.Current = ctx;

            RouteCollection routes = RouteTable.Routes;

            routes.Clear();

            var ddr = new DynamicDataRoute("{table}/{action}.aspx")
            {
                Defaults = new RouteValueDictionary()
                {
                    { "Action", "InitialAction" }
                },
                Action       = PageAction.Details,
                Model        = m,
                RouteHandler = new MyDynamicDataRouteHandler()
            };

            routes.Add(ddr);

            Assert.IsNotNull(ddr, "#A1");
            Assert.IsNotNull(ddr.Defaults, "#A1-1");
            var rd = new RouteData();
            var hc = new HttpContextWrapper(HttpContext.Current);

            ddr.GetVirtualPath(new RequestContext(hc, rd), null);
            Assert.IsNotNull(ddr.Defaults, "#B1");
            Assert.AreEqual(1, ddr.Defaults.Count, "#B1-1");
            Assert.AreEqual(PageAction.Details, ddr.Defaults["Action"], "#B1-2");

            ddr.Action = "MyAction";
            ddr.GetVirtualPath(new RequestContext(hc, rd), null);
            Assert.IsNotNull(ddr.Defaults, "#C1");
            Assert.AreEqual(1, ddr.Defaults.Count, "#C1-1");
            Assert.AreEqual(PageAction.Details, ddr.Defaults["Action"], "#B1-2");
        }
Ejemplo n.º 13
0
        public void Constructor()
        {
            // other tests create MetaModel and set Default and this test does not always run first, so it does not make sense anymore.
            //Assert.IsNull (MetaModel.Default, "#1");
            bool isFirst = (MetaModel.Default == null);
            var  m       = new MetaModel();       // it automatically fills Default

            if (isFirst)
            {
                Assert.AreEqual(m, MetaModel.Default, "#2");
            }

            var r = new DynamicDataRoute("Dynamic1");

            Assert.AreEqual(MetaModel.Default, r.Model, "#1");
            Assert.IsNull(r.Action, "#2");
            Assert.IsNull(r.Table, "#3");
            Assert.IsNull(r.ViewName, "#4");
            Assert.IsNotNull(r.RouteHandler, "#5");
            Assert.IsNotNull(r.Model, "#6");
            Assert.IsNull(r.RouteHandler.Model, "#7");              // irrelevant to route's MetaModel
        }
 public override IHttpHandler CreateHandler(DynamicDataRoute route, MetaTable table, string action)
 {
     return(new Page() as IHttpHandler);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Determine access to tables based on ASP.NET authentication
        /// and security attribute as applied to the tables in the
        /// data model.
        /// </summary>
        /// <param name="route">The route used by
        /// ASP.NET Dynamic Data. This is in the format
        /// {table}/{action}.aspx and is defined in Global.asax</param>
        /// <param name="table">The metadata that describes a table
        /// for use by Dynamic Data pages.</param>
        /// <param name="action">The action allowed for the table from the
        /// route.</param>
        /// <returns></returns>
        public IHttpHandler CreateHandler2(
            DynamicDataRoute route, MetaTable table, string action)
        {
            // Store the ASP.NET authenticated roles
            string[] roles = Roles.GetAllRoles();

            // Obtain the roles that have administrative
            // access rights.
            Attribute[] adminRoles =
                Attribute.GetCustomAttributes(typeof(AdminRoles));

            // Obtain the roles that have limited
            // access rights.
            Attribute[] anonymousRoles =
                Attribute.GetCustomAttributes(typeof(AnonymousRoles));


            // Allow tables access based on the authenticated
            // roles and on the security attributes applied
            // to the tables.
            for (int i = 0; i < roles.Length; i++)
            {
                // Check if the user is an authenticated
                // administrator.
                foreach (SecurityAttribute admin in
                         adminRoles.OfType <SecurityAttribute>())
                {
                    if (
                        (Roles.IsUserInRole(admin.Role)) &&
                        (admin.Action == "All")
                        )
                    {
                        // Allow complete access.
                        return(base.CreateHandler(route, table, action));
                    }
                }
                // Instantiate the SecurityInformation
                // utility object.
                DynamicDataSecurity secInfo =
                    new DynamicDataSecurity();

                // Check if the user is an administrator
                // and is authenticated.
                if (secInfo.IsUserInAdmimistrativeRole())
                {
                    // Allow complete access.
                    return(base.CreateHandler(route, table, action));
                }

                // Check if the user is authenticated.
                // Allow those actions permitted by the
                // security attributes.
                if (Roles.IsUserInRole(roles[i]))
                {
                    foreach (SecurityAttribute attribute in
                             table.Attributes.OfType <SecurityAttribute>())
                    {
                        // Allow access the permissible actions.
                        if (attribute.Role == roles[i] &&
                            attribute.Action == action)
                        {
                            return(base.CreateHandler(route, table, action));
                        }
                    }
                }
            }
            // Search for roles that have limited access
            // to the database tables.
            // Allow access to those tables that
            // are marked with the roles having limited access.
            // Note this check is important to allow
            // anonymous access; otherwise you would not
            // have any table showing in the scaffolded list
            // in the default.aspx page.
            foreach (SecurityAttribute anonymous in
                     anonymousRoles.OfType <SecurityAttribute>())
            {
                foreach (SecurityAttribute entityRole in
                         table.Attributes.OfType <SecurityAttribute>())
                {
                    if (entityRole.Role == anonymous.Role)
                    {
                        // Allow limited access.
                        return(base.CreateHandler(route, table, anonymous.Action));
                    }
                }
            }

            // No role and no attribute exist; access is denied.
            return(null);
        }