Exemplo n.º 1
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            BootStrapper.RootPath = this.RootPathProvider.GetRootPath();

            // create App_Data
            Directory.CreateDirectory(Path.Combine(BootStrapper.RootPath, "App_Data"));

            ModuleResource.ReadSystemsAndResources(BootStrapper.RootPath);

            this.Conventions.ViewLocationConventions.Clear();

            #region Localized View Conventions

            // Site's View Folder has most priority
            // Mobile View Overrides
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                string u = context.Context.Request.Headers.UserAgent.ToLowerInvariant();
                if (u.Contains("mobile/"))
                {
                    return("Site/Views/Mobile/" + viewName + "_" + context.Context.Items["Language"]);
                }

                return(string.Empty); // not mobile browser
            });

            // Desktop View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("Site/Views/Desktop/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // Generic View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("Site/Views/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // Theme view location (views/_theme) can override _theme of the Theme folder
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                var theme = context.Context.GetSiteSettings().Theme;
                if (theme == null)
                {
                    return(string.Empty);
                }

                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("Themes/" + theme + "/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // NancyBlack's View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("NancyBlack/Content/Views/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // then try Views in Systems (AdminSystem, ContentSystem etc...)
            foreach (var system in ModuleResource.Systems)
            {
                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    if (context.Context.Items.ContainsKey("Language") == false)
                    {
                        return(string.Empty);
                    }

                    return(string.Concat("NancyBlack/Modules/",
                                         viewName,
                                         "_",
                                         context.Context.Items["Language"]));
                });

                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    if (context.Context.Items.ContainsKey("Language") == false)
                    {
                        return(string.Empty);
                    }

                    return(string.Concat("NancyBlack/Modules/",
                                         system,
                                         "/Views/",
                                         viewName,
                                         "_",
                                         context.Context.Items["Language"]));
                });
            }

            #endregion

            #region View Conventions

            // Site's View Folder has most priority
            // Mobile View Overrides
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                string u = context.Context.Request.Headers.UserAgent.ToLowerInvariant();
                if (u.Contains("mobile/"))
                {
                    return("Site/Views/Mobile/" + viewName);
                }

                return(string.Empty); // not mobile browser
            });

            // Desktop View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return("Site/Views/Desktop/" + viewName);
            });

            // Generic View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return("Site/Views/" + viewName);
            });

            // Theme view location (views/_theme) can override _theme of the Theme folder
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                var theme = context.Context.GetSiteSettings().Theme;
                if (theme == null)
                {
                    return(string.Empty);
                }

                return("Themes/" + theme + "/" + viewName);
            });

            // NancyBlack's View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return("NancyBlack/Content/Views/" + viewName);
            });

            // then try Views in Systems (AdminSystem, ContentSystem etc...)
            foreach (var system in ModuleResource.Systems)
            {
                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    return(string.Concat("NancyBlack/Modules/",
                                         viewName));
                });
                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    return(string.Concat("NancyBlack/Modules/",
                                         system,
                                         "/Views/",
                                         viewName));
                });
            }

            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return(viewName); // fully qualify names
            });

            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return(viewName.Substring(1)); // fully qualify names, remove forward slash at first
            });



            #endregion

            var formsAuthConfiguration = new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/__membership/login",
                UserMapper  = container.Resolve <IUserMapper>(),
            };
            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);

            pipelines.BeforeRequest.AddItemToStartOfPipeline((ctx) =>
            {
                ctx.Items["SiteDatabase"] = NancyBlackDatabase.GetSiteDatabase(this.RootPathProvider.GetRootPath());
                ctx.Items["CurrentSite"]  = AdminModule.ReadSiteSettings();
                ctx.Items["SiteSettings"] = AdminModule.ReadSiteSettings();
                ctx.Items["RootPath"]     = BootStrapper.RootPath;
                if (ctx.CurrentUser == null)
                {
                    ctx.CurrentUser = NcbUser.Anonymous;
                    if (ctx.Request.Url.HostName == "localhost")
                    {
                        ctx.CurrentUser = NcbUser.LocalHostAdmin;
                    }
                }

                return(null);
            });

            foreach (var item in container.ResolveAll <IPipelineHook>())
            {
                item.Hook(pipelines);
            }
        }
Exemplo n.º 2
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            BootStrapper.RootPath = this.RootPathProvider.GetRootPath();

            // create App_Data
            Directory.CreateDirectory(Path.Combine(BootStrapper.RootPath, "App_Data"));

            ModuleResource.ReadSystemsAndResources(BootStrapper.RootPath);

            this.Conventions.ViewLocationConventions.Clear();

            #region Localized View Conventions

            // Site's View Folder has most priority
            // Mobile View Overrides
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                string u = context.Context.Request.Headers.UserAgent.ToLowerInvariant();
                if (u.Contains("mobile/"))
                {
                    return("Site/Views/Mobile/" + viewName + "_" + context.Context.Items["Language"]);
                }

                return(string.Empty); // not mobile browser
            });

            // Desktop View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("Site/Views/Desktop/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // Generic View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("Site/Views/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // Theme view location (views/_theme) can override _theme of the Theme folder
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                var theme = context.Context.GetSiteSettings().Theme;
                if (theme == null)
                {
                    return(string.Empty);
                }

                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("Themes/" + theme + "/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // NancyBlack's View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                if (context.Context.Items.ContainsKey("Language") == false)
                {
                    return(string.Empty);
                }

                return("NancyBlack/Content/Views/" + viewName + "_" + context.Context.Items["Language"]);
            });

            // then try Views in Systems (AdminSystem, ContentSystem etc...)
            foreach (var system in ModuleResource.Systems)
            {
                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    if (context.Context.Items.ContainsKey("Language") == false)
                    {
                        return(string.Empty);
                    }

                    return(string.Concat("NancyBlack/Modules/",
                                         viewName,
                                         "_",
                                         context.Context.Items["Language"]));
                });

                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    if (context.Context.Items.ContainsKey("Language") == false)
                    {
                        return(string.Empty);
                    }

                    return(string.Concat("NancyBlack/Modules/",
                                         system,
                                         "/Views/",
                                         viewName,
                                         "_",
                                         context.Context.Items["Language"]));
                });
            }

            #endregion

            #region Sub Website View Conventions

            // Generic View for SubWebsite Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                string subSiteName = (string)context.Context.Items[ContextItems.SubSite];
                if (!string.IsNullOrEmpty(subSiteName))
                {
                    return("Site/SubSites/" + subSiteName + "/Views/" + viewName);
                }

                return(string.Empty);
            });

            #endregion

            #region View Conventions

            // Site's View Folder has most priority
            // Mobile View Overrides
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                string u = context.Context.Request.Headers.UserAgent.ToLowerInvariant();
                if (u.Contains("mobile/"))
                {
                    return("Site/Views/Mobile/" + viewName);
                }

                return(string.Empty); // not mobile browser
            });

            // Desktop View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return("Site/Views/Desktop/" + viewName);
            });

            // Generic View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return("Site/Views/" + viewName);
            });

            // Theme view location (views/_theme) can override _theme of the Theme folder
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                var theme = context.Context.GetSiteSettings().Theme;
                if (theme == null)
                {
                    return(string.Empty);
                }

                return("Themes/" + theme + "/" + viewName);
            });

            // NancyBlack's View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return("NancyBlack/Content/Views/" + viewName);
            });

            // then try Views in Systems (AdminSystem, ContentSystem etc...)
            foreach (var system in ModuleResource.Systems)
            {
                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    return(string.Concat("NancyBlack/Modules/",
                                         viewName));
                });
                this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
                {
                    return(string.Concat("NancyBlack/Modules/",
                                         system,
                                         "/Views/",
                                         viewName));
                });
            }

            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return(viewName); // fully qualify names
            });

            this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
            {
                return(viewName.Substring(1)); // fully qualify names, remove forward slash at first
            });



            #endregion

            var formsAuthConfiguration = new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/__membership/login",
                UserMapper  = container.Resolve <IUserMapper>(),
            };
            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);

            pipelines.BeforeRequest.AddItemToStartOfPipeline((ctx) =>
            {
                // Get Subsite Name if in main site will get null
                string folder = Path.Combine(BootStrapper.RootPath, "Site", "SubSites");
                if (Directory.Exists(folder))
                {
                    var subSiteNames     = from subDirectories in Directory.GetDirectories(folder) select Path.GetFileName(subDirectories);
                    var matchSubSiteName = (from subSite in subSiteNames where ctx.Request.Url.HostName.Contains(subSite) select subSite).FirstOrDefault();

                    ctx.Items[ContextItems.SubSite] = matchSubSiteName;
                }
                else
                {
                    ctx.Items[ContextItems.SubSite] = null;
                }

                var db = NancyBlackDatabase.GetSiteDatabase(this.RootPathProvider.GetRootPath());
                GlobalVar.Default.Load(db);

                ctx.Items["SiteDatabase"] = db;
                ctx.Items["CurrentSite"]  = AdminModule.ReadSiteSettings();
                ctx.Items["SiteSettings"] = AdminModule.ReadSiteSettings();
                ctx.Items["RootPath"]     = BootStrapper.RootPath;

                if (ctx.Request.Cookies.ContainsKey("userid") == false)
                {
                    ctx.Request.Cookies.Add("userid", Guid.NewGuid().ToString());
                }

                return(null);
            });

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                if (ctx.Request.Cookies.ContainsKey("userid"))
                {
                    ctx.Response.Cookies.Add(
                        new NancyCookie("userid", ctx.Request.Cookies["userid"], DateTime.Now.AddYears(10)));
                }

                GlobalVar.Default.Persist(ctx.Items["SiteDatabase"] as NancyBlackDatabase);
            });

            foreach (var item in container.ResolveAll <IPipelineHook>())
            {
                item.Hook(pipelines);
            }
        }
Exemplo n.º 3
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            BootStrapper.RootPath = this.RootPathProvider.GetRootPath();

            // create App_Data
            Directory.CreateDirectory(Path.Combine(BootStrapper.RootPath, "App_Data"));

            ModuleResource.ReadSystemsAndResources(BootStrapper.RootPath);

            this.Conventions.ViewLocationConventions.Clear();

            #region View Conventions

            // Generic View Location, with subsite
            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                var language = vc.Context.GetString(ContextItems.Language);
                var subsite  = vc.Context.GetString(ContextItems.SubSite);

                if (subsite == null)
                {
                    return("Site/Views/" + viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
                }

                return("Site/SubSites/" + subsite + "/Views/" + viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
            });

            // Generic View Location, without subsite
            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                var language = vc.Context.GetString(ContextItems.Language);
                return("Site/Views/" + viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
            });

            // NancyBlack's View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                var language = vc.Context.GetString(ContextItems.Language);
                return("NancyBlack/Content/Views/" + viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
            });

            // then try Views in Systems (AdminSystem, ContentSystem etc...)
            foreach (var system in ModuleResource.Systems)
            {
                this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
                {
                    var language = vc.Context.GetString(ContextItems.Language);

                    return("NancyBlack/Modules/" + viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
                });

                this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
                {
                    var language = vc.Context.GetString(ContextItems.Language);

                    return(string.Concat("NancyBlack/Modules/",
                                         system,
                                         "/Views/",
                                         viewName,
                                         (string.IsNullOrEmpty(language) ? null : "_" + language)));
                });
            }

            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                var subsite  = vc.Context.GetString(ContextItems.SubSite);
                var language = vc.Context.GetString(ContextItems.Language);


                if (subsite == null)
                {
                    return(viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
                }

                return("Site/SubSites/" + subsite + "/" + viewName + (string.IsNullOrEmpty(language) ? null : "_" + language));
            });


            // Generic View Location, with subsite
            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                var subsite = vc.Context.GetString(ContextItems.SubSite);

                if (subsite == null)
                {
                    return("Site/Views/" + viewName);
                }

                return("SubSites/" + subsite + "/Views/" + viewName);
            });

            // Generic View Location, without subsite
            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                return("Site/Views/" + viewName);
            });

            // NancyBlack's View Location
            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                return("NancyBlack/Content/Views/" + viewName);
            });

            // then try Views in Systems (AdminSystem, ContentSystem etc...)
            foreach (var system in ModuleResource.Systems)
            {
                this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
                {
                    return("NancyBlack/Modules/" + viewName);
                });

                this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
                {
                    return(string.Concat("NancyBlack/Modules/",
                                         system,
                                         "/Views/",
                                         viewName));
                });
            }

            this.Conventions.ViewLocationConventions.Add((viewName, model, vc) =>
            {
                return(viewName);
            });


            #endregion

            var formsAuthConfiguration = new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/__membership/login",
                UserMapper  = container.Resolve <IUserMapper>(),
            };
            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                if (ctx.Response.StatusCode != Nancy.HttpStatusCode.OK)
                {
                    return;
                }

                bool addCookies = ctx.Response.ContentType.StartsWith("text/html") ||
                                  ctx.Response.ContentType.StartsWith("application/json");

                if (ctx.Items.ContainsKey("NoCookie") ||
                    ctx.Request.Url.Path.StartsWith("/table"))
                {
                    addCookies = false;
                }

                // for admins requesting admin page - always return fresh copy
                if (ctx.CurrentUser.HasClaim("admin") &&
                    (ctx.Request.Url.Path.StartsWith("/table") ||
                     ctx.Request.Url.Path.StartsWith("/Admin")))
                {
                    addCookies = true;
                    ctx.Response.WithHeader("Cache-Control", "no-cache");
                    ctx.Response.Cookies.Add(new NancyCookie("admin", "1", DateTime.Now.AddDays(1)));
                }
                else
                {
                    if (!addCookies)
                    {
                        ctx.Response.WithHeader("Cache-Control", "public, max-age=86400");
                        return;
                    }
                }

                if (ctx.Items.ContainsKey("userid"))
                {
                    ctx.Response.Cookies.Add(
                        new NancyCookie("userid", ctx.Items["userid"].ToString(), DateTime.Now.AddYears(10)));
                }

                BootStrapper.SetCookies(ctx);
            });

            foreach (var item in container.ResolveAll <IPipelineHook>())
            {
                item.Hook(pipelines);
            }

            foreach (var item in container.ResolveAll <IRequireGlobalInitialize>())
            {
                PrepareRequest.RegisterGlobalInitialize(item);
            }
        }