Example #1
0
        /// <summary>
        /// Authenticates the request by reading the FormsAuthentication cookie and setting the
        /// context and thread principle object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void AuthenticateRequest(object sender, EventArgs e)
        {
            var app  = (HttpApplication)sender;
            var http = new HttpContextWrapper(app.Context);

            //we need to determine if the path being requested is an umbraco path, if not don't do anything
            var settings = UmbracoSettings.GetSettings();

            var backOfficeRoutePath = string.Concat(settings.UmbracoPaths.BackOfficePath, "/");
            var installerRoutePath  = string.Concat("Install", "/");

            var routeUrl  = "";
            var routeData = RouteTable.Routes.GetRouteData(http);

            if (routeData != null)
            {
                var route = routeData.Route as Route;
                if (route != null)
                {
                    routeUrl = route.Url;
                }
            }

            if (routeUrl.StartsWith(installerRoutePath, StringComparison.InvariantCultureIgnoreCase) ||
                routeUrl.StartsWith(backOfficeRoutePath, StringComparison.InvariantCultureIgnoreCase))
            {
                if (app.Context.User == null)
                {
                    if (app.User != null)
                    {
                        //set the principal object
                        app.Context.User        = app.User;
                        Thread.CurrentPrincipal = app.User;
                    }
                    else
                    {
                        var ticket = http.GetUmbracoAuthTicket();
                        if (ticket != null && !ticket.Expired && http.RenewUmbracoAuthTicket())
                        {
                            //create the Umbraco user identity
                            var identity = new UmbracoBackOfficeIdentity(ticket);

                            //set the principal object
                            var principal = new GenericPrincipal(identity, identity.Roles);
                            app.Context.User        = principal;
                            Thread.CurrentPrincipal = principal;
                        }
                    }
                }
            }
        }
        public static string ResolveLayoutPath(string path, File file)
        {
            if (path.StartsWith("~") || path.StartsWith("/"))
            {
                path = HttpContext.Current.Server.MapPath(path);
            }
            else if (file != null)
            {
                path = file.RootedPath.Substring(0, file.RootedPath.LastIndexOf("\\")) + "\\" + path;
            }
            else
            {
                path = HttpContext.Current.Server.MapPath(UmbracoSettings.GetSettings().UmbracoFolders.TemplateFolder) + "\\" + path;
            }

            return(path);
        }
        public static void Initialize()
        {
            using (new WriteLockDisposable(Locker))
            {
                using (DisposableTimer.TraceDuration <PluginManager>("Start Initialise", "End Initialise"))
                {
                    var settings = UmbracoSettings.GetSettings();

                    // TODO: Add verbose exception handling / raising here since this is happening on app startup and could
                    // prevent app from starting altogether
                    var pluginFolder = new DirectoryInfo(HostingEnvironment.MapPath(settings.PluginConfig.PluginsPath));
                    _shadowCopyFolder = new DirectoryInfo(HostingEnvironment.MapPath(settings.PluginConfig.ShadowCopyPath));

                    var referencedPlugins = new List <PluginDefinition>();

                    var pluginFiles = Enumerable.Empty <FileInfo>();

                    try
                    {
                        LogHelper.TraceIfEnabled <PluginManager>("Creating shadow copy folder and querying for dlls");

                        //ensure folders are created
                        Directory.CreateDirectory(pluginFolder.FullName);
                        Directory.CreateDirectory(_shadowCopyFolder.FullName);
                        Directory.CreateDirectory(Path.Combine(pluginFolder.FullName, PackagesFolderName));

                        //get list of all DLLs in bin
                        var binFiles = _shadowCopyFolder.GetFiles("*.dll", SearchOption.AllDirectories);

                        //get list of all DLLs in plugins (not in bin!)
                        //this will match the plugin folder pattern
                        pluginFiles = pluginFolder.GetFiles("*.dll", SearchOption.AllDirectories)
                                      //just make sure we're not registering shadow copied plugins
                                      .Where(x => !binFiles.Select(q => q.FullName).Contains(x.FullName))
                                      .Where(x => x.Directory.Parent != null && (IsPackagePluginBinFolder(x.Directory)))
                                      .ToList();

                        //clear out shadow copied plugins
                        foreach (var f in binFiles)
                        {
                            LogHelper.TraceIfEnabled <PluginManager>("Deleting {0}", () => f.Name);

                            File.Delete(f.FullName);
                        }
                    }
                    catch (Exception ex)
                    {
                        var fail = new ApplicationException("Could not initialise plugin folder", ex);
                        LogHelper.Error <PluginManager>(fail.Message, fail);
                        //throw fail;
                    }

                    try
                    {
                        LogHelper.TraceIfEnabled <PluginManager>("Shadow copying assemblies");

                        //shadow copy files
                        referencedPlugins
                        .AddRange(pluginFiles.Select(plug =>
                                                     new PluginDefinition(plug,
                                                                          GetPackageFolderFromPluginDll(plug),
                                                                          PerformFileDeploy(plug),
                                                                          plug.Directory.Parent.Name == "Core")));
                    }
                    catch (Exception ex)
                    {
                        var fail = new ApplicationException("Could not initialise plugin folder", ex);
                        LogHelper.Error <PluginManager>(fail.Message, fail);
                        //throw fail;
                    }

                    ReferencedPlugins = referencedPlugins;
                }
            }
        }
 public IDictionary <string, string> GetData()
 {
     return(UmbracoSettings.GetSettings().Applications.ToDictionary(x => x.Alias, y => y.Name));
 }
 /// <summary>
 /// Default constructor that uses the standard UmbracoComponentRegistrar as the IComponentRegistrar
 /// </summary>
 /// <param name="httpApp"></param>
 public UmbracoContainerBuilder(TWebApp httpApp)
 {
     _httpApp            = httpApp;
     _settings           = UmbracoSettings.GetSettings();
     _componentRegistrar = new UmbracoComponentRegistrar();
 }
        /// <summary>Builds the dependency demands required by this implementation. </summary>
        /// <param name="builder">The <see cref="IContainerBuilder"/> .</param>
        /// <param name="builderContext"></param>
        public void Build(IContainerBuilder builder, IBuilderContext builderContext)
        {
            //raise the building event
            OnContainerBuilding(new ContainerBuilderEventArgs(builder));

            //register all of the abstract web types
            builder.AddDependencyDemandBuilder(new WebTypesDemandBuilder(_httpApp));

            var typeFinder = new TypeFinder();

            builder.ForInstanceOfType(typeFinder)
            .ScopedAs.Singleton();

            //register the umbraco settings
            builder.ForFactory(x => UmbracoSettings.GetSettings())
            .KnownAsSelf()
            .ScopedAs.Singleton();     //only have one instance ever

            //register our MVC types
            builder.AddDependencyDemandBuilder(new MvcTypesDemandBuilder(typeFinder));

            // Register the IRoutableRequestContext
            builder.For <HttpRequestScopedCache>().KnownAs <AbstractScopedCache>().ScopedAs.Singleton();
            builder.For <HttpRuntimeApplicationCache>().KnownAs <AbstractApplicationCache>().ScopedAs.Singleton();
            builder.For <HttpRequestScopedFinalizer>().KnownAs <AbstractFinalizer>().ScopedAs.Singleton();
            builder.For <DefaultFrameworkContext>().KnownAs <IFrameworkContext>().ScopedAs.Singleton();
            builder.For <UmbracoApplicationContext>().KnownAs <IUmbracoApplicationContext>().ScopedAs.Singleton();
            builder.For <RoutableRequestContext>().KnownAs <IRoutableRequestContext>().ScopedAs.HttpRequest();
            builder.For <DefaultBackOfficeRequestContext>().KnownAs <IBackOfficeRequestContext>().ScopedAs.HttpRequest();

            // TODO: Ensure this isn't created manually anywhere but tests, only via a factory: builder.ForType<IUmbracoRenderModel, UmbracoRenderContext>().Register().ScopedPerHttpRequest();
            builder.For <DefaultRenderModelFactory>().KnownAs <IRenderModelFactory>().
            ScopedAs.Singleton();

            // Register Hive provider
            //builder.AddDependencyDemandBuilder(new HiveDemandBuilder());
            builder.AddDependencyDemandBuilder(new Hive.DemandBuilders.HiveDemandBuilder());

            // Register Persistence provider loader
            //builder.AddDependencyDemandBuilder(new Framework.Persistence.DependencyManagement.DemandBuilders.LoadFromPersistenceConfig());
            builder.AddDependencyDemandBuilder(new Hive.DemandBuilders.LoadFromPersistenceConfig());

            // Register Cms bootstrapper
            // TODO: Split UmbracoContainerBuilder between Cms and Frontend variants / needs
            builder.For <CmsBootstrapper>().KnownAsSelf();
            // Register Frontend bootstrapper
            builder.ForFactory(
                x =>
                new RenderBootstrapper(
                    x.Resolve <IUmbracoApplicationContext>(),
                    x.Resolve <IRouteHandler>(RenderRouteHandler.SingletonServiceName),
                    x.Resolve <IRenderModelFactory>())
                )
            .KnownAsSelf();

            //register all component areas, loop through all found package folders
            //TODO: All other places querying for packages use the NuGet IO FileManager stuff, not the standard .Net IO classes
            var pluginFolder = new DirectoryInfo(_httpApp.Server.MapPath(_settings.PluginConfig.PluginsPath));

            foreach (var package in pluginFolder.GetDirectories(PluginManager.PackagesFolderName)
                     .SelectMany(x => x.GetDirectories()
                                 .Where(PluginManager.IsPackagePluginFolder)))
            {
                //register an area for this package
                builder.For <PackageAreaRegistration>()
                .KnownAsSelf()
                .WithNamedParam("packageFolder", package);
            }

            //register the RoutingEngine
            builder
            .For <DefaultRoutingEngine>()
            .KnownAs <IRoutingEngine>()
            .ScopedAs.HttpRequest();

            //register the package context
            builder
            .ForFactory(x => new DefaultPackageContext(x.Resolve <UmbracoSettings>(), HostingEnvironment.MapPath))
            //.For<DefaultPackageContext>()
            .KnownAs <IPackageContext>()
            .ScopedAs.Singleton();

            //register the PropertyEditorFactory
            builder.For <PropertyEditorFactory>()
            .KnownAs <IPropertyEditorFactory>()
            .ScopedAs.Singleton();

            //register the ParameterEditorFactory
            builder.For <ParameterEditorFactory>()
            .KnownAs <IParameterEditorFactory>()
            .ScopedAs.Singleton();

            //register the SecurityService
            builder.For <SecurityService>()
            .KnownAs <ISecurityService>();

            //register the CmsAttributeTypeRegistry
            builder.For <CmsAttributeTypeRegistry>()
            .KnownAs <IAttributeTypeRegistry>()
            .ScopedAs.Singleton();

            //component registration
            _componentRegistrar.RegisterTasks(builder, typeFinder);
            _componentRegistrar.RegisterTreeControllers(builder, typeFinder);
            _componentRegistrar.RegisterPropertyEditors(builder, typeFinder);
            _componentRegistrar.RegisterParameterEditors(builder, typeFinder);
            _componentRegistrar.RegisterEditorControllers(builder, typeFinder);
            _componentRegistrar.RegisterMenuItems(builder, typeFinder);
            _componentRegistrar.RegisterSurfaceControllers(builder, typeFinder);
            _componentRegistrar.RegisterDashboardFilters(builder, typeFinder);
            _componentRegistrar.RegisterDashboardMatchRules(builder, typeFinder);
            _componentRegistrar.RegisterPermissions(builder, typeFinder);
            _componentRegistrar.RegisterMacroEngines(builder, typeFinder);

            //register the registrations
            builder.For <ComponentRegistrations>().KnownAsSelf();

            //register task manager
            builder.For <ApplicationTaskManager>().KnownAsSelf().ScopedAs.Singleton();

            //register our model mappings and resolvers
            builder.AddDependencyDemandBuilder(new ModelMappingsDemandBuilder());

            //TODO: More stuff should happen with the TextManager here (e.g. db access and whatnot)
            //The user may later override settings, most importantly the LocalizationConfig.CurrentTextManager delegate to implement different environments
            //The text manager is assumed to be set up by the framework
            var textManager = LocalizationConfig.TextManager;

            LocalizationWebConfig.ApplyDefaults <TWebApp>(textManager, overridesPath: "~/App_Data/Umbraco/LocalizationEntries.xml");
            LocalizationWebConfig.SetupMvcDefaults(setupMetadata: false);

            //The name of the assembly that contains common texts
            textManager.FallbackNamespaces.Add("Umbraco.Cms.Web");

            OnContainerBuildingComplete(new ContainerBuilderEventArgs(builder));
        }