Exemplo n.º 1
0
 private void CreateSitePages(Tenant tenant, List <PageTemplate> pageTemplates)
 {
     if (pageTemplates.Count != 0)
     {
         var processed = new List <Site>();
         foreach (Alias alias in _aliases.GetAliases().Where(item => item.TenantId == tenant.TenantId))
         {
             if (!processed.Exists(item => item.SiteId == alias.SiteId))
             {
                 using (var scope = _serviceScopeFactory.CreateScope())
                 {
                     var siteState = scope.ServiceProvider.GetRequiredService <SiteState>();
                     siteState.Alias = alias;
                     var sites = scope.ServiceProvider.GetRequiredService <ISiteRepository>();
                     var site  = sites.GetSite(alias.SiteId);
                     if (site != null)
                     {
                         sites.CreatePages(site, pageTemplates);
                     }
                     processed.Add(site);
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        public TenantResolver(IHttpContextAccessor Accessor, IAliasRepository Aliases, ITenantRepository Tenants, SiteState SiteState)
        {
            int    aliasid   = -1;
            string aliasname = "";

            // get alias identifier based on request context
            if (Accessor.HttpContext != null)
            {
                // check if an alias is passed as a querystring parameter ( for cross tenant access )
                if (Accessor.HttpContext.Request.Query.ContainsKey("aliasid"))
                {
                    aliasid = int.Parse(Accessor.HttpContext.Request.Query["aliasid"]);
                }
                else // get the alias from the request url
                {
                    aliasname = Accessor.HttpContext.Request.Host.Value;
                    string   path     = Accessor.HttpContext.Request.Path.Value;
                    string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
                    {
                        aliasname += "/" + segments[0];
                    }
                    if (aliasname.EndsWith("/"))
                    {
                        aliasname = aliasname.Substring(0, aliasname.Length - 1);
                    }
                }
            }
            else  // background processes can pass in an alias using the SiteState service
            {
                if (SiteState != null)
                {
                    aliasid = SiteState.Alias.AliasId;
                }
            }

            // get the alias and tenant
            if (aliasid != -1 || aliasname != "")
            {
                IEnumerable <Alias>  aliases = Aliases.GetAliases(); // cached
                IEnumerable <Tenant> tenants = Tenants.GetTenants(); // cached

                if (aliasid != -1)
                {
                    alias = aliases.Where(item => item.AliasId == aliasid).FirstOrDefault();
                }
                else
                {
                    alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
                }
                if (alias != null)
                {
                    tenant = tenants.Where(item => item.TenantId == alias.TenantId).FirstOrDefault();
                }
            }
        }
Exemplo n.º 3
0
        public IEnumerable <Alias> Get()
        {
            var aliases = _aliases.GetAliases();

            if (!User.IsInRole(RoleNames.Host))
            {
                aliases = aliases.Where(item => item.SiteId == _alias.SiteId && item.TenantId == _alias.TenantId);
            }
            return(aliases);
        }
Exemplo n.º 4
0
        public TenantResolver(IHttpContextAccessor accessor, IAliasRepository aliasRepository, ITenantRepository tenantRepository, SiteState siteState)
        {
            int    aliasId   = -1;
            string aliasName = "";

            // get alias identifier based on request context
            if (accessor.HttpContext != null)
            {
                // check if an alias is passed as a querystring parameter ( for cross tenant access )
                if (accessor.HttpContext.Request.Query.ContainsKey("aliasid"))
                {
                    aliasId = int.Parse(accessor.HttpContext.Request.Query["aliasid"]);
                }
                else // get the alias from the request url
                {
                    aliasName = accessor.HttpContext.Request.Host.Value;
                    string   path     = accessor.HttpContext.Request.Path.Value;
                    string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
                    {
                        aliasName += "/" + segments[0];
                    }

                    if (aliasName.EndsWith("/"))
                    {
                        aliasName = aliasName.Substring(0, aliasName.Length - 1);
                    }
                }
            }
            else // background processes can pass in an alias using the SiteState service
            {
                aliasId = siteState?.Alias?.AliasId ?? -1;
            }

            // get the alias and tenant
            IEnumerable <Alias> aliases = aliasRepository.GetAliases().ToList(); // cached

            if (aliasId != -1)
            {
                _alias = aliases.FirstOrDefault(item => item.AliasId == aliasId);
            }
            else
            {
                _alias = aliases.FirstOrDefault(item => item.Name == aliasName
                                                //if here is only one alias and other methods fail, take it (case of startup install)
                                                || aliases.Count() == 1);
            }

            if (_alias != null)
            {
                IEnumerable <Tenant> tenants = tenantRepository.GetTenants(); // cached
                _tenant = tenants.FirstOrDefault(item => item.TenantId == _alias.TenantId);
            }
        }
Exemplo n.º 5
0
 public Alias GetAlias()
 {
     try
     {
         IEnumerable <Alias> aliases = _aliasrepository.GetAliases(); // cached
         return(aliases.Where(item => item.Name == aliasname).FirstOrDefault());
     }
     catch
     {
         throw;
     }
 }
 public Tenant GetTenant()
 {
     try
     {
         IEnumerable <Alias> aliases = _aliasrepository.GetAliases();   // cached
         Alias alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
         IEnumerable <Tenant> tenants = _tenantrepository.GetTenants(); // cached
         return(tenants.Where(item => item.TenantId == alias.TenantId).FirstOrDefault());
     }
     catch
     {
         throw;
     }
 }
Exemplo n.º 7
0
        public Alias GetAlias()
        {
            Alias alias = null;

            if (_siteState != null && _siteState.Alias != null)
            {
                alias = _siteState.Alias;
            }
            else
            {
                // if there is http context
                if (_httpContextAccessor.HttpContext != null)
                {
                    // legacy support for client api requests which would include the alias as a path prefix ( ie. {alias}/api/[controller] )
                    int      aliasId;
                    string[] segments = _httpContextAccessor.HttpContext.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    if (segments.Length > 1 && (segments[1] == "api" || segments[1] == "pages") && int.TryParse(segments[0], out aliasId))
                    {
                        alias = _aliasRepository.GetAliases().ToList().FirstOrDefault(item => item.AliasId == aliasId);
                    }

                    // resolve alias based on host name and path
                    if (alias == null)
                    {
                        string name = _httpContextAccessor.HttpContext.Request.Host.Value + _httpContextAccessor.HttpContext.Request.Path;
                        alias = _aliasRepository.GetAlias(name);
                    }

                    // if there is a match save it
                    if (alias != null)
                    {
                        _siteState.Alias = alias;
                    }
                }
            }

            return(alias);
        }
Exemplo n.º 8
0
        private void ResolveTenant()
        {
            if (_siteState != null && _siteState.Alias != null)
            {
                // background processes can pass in an alias using the SiteState service
                _alias = _siteState.Alias;
            }
            else
            {
                int aliasId = -1;

                // get aliasid identifier based on request
                if (_accessor.HttpContext != null)
                {
                    string[] segments = _accessor.HttpContext.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    if (segments.Length > 1 && (segments[1] == "api" || segments[1] == "pages") && segments[0] != "~")
                    {
                        aliasId = int.Parse(segments[0]);
                    }
                }

                // get the alias
                IEnumerable <Alias> aliases = _aliasRepository.GetAliases().ToList(); // cached
                if (aliasId != -1)
                {
                    _alias = aliases.FirstOrDefault(item => item.AliasId == aliasId);
                }
            }

            if (_alias != null)
            {
                // get the tenant
                IEnumerable <Tenant> tenants = _tenantRepository.GetTenants(); // cached
                _tenant = tenants.FirstOrDefault(item => item.TenantId == _alias.TenantId);
            }
        }
Exemplo n.º 9
0
 public IEnumerable <Alias> Get()
 {
     return(_aliases.GetAliases());
 }
Exemplo n.º 10
0
        public Alias GetAlias()
        {
            IEnumerable <Alias> aliases = Aliases.GetAliases(); // cached

            return(aliases.Where(item => item.Name == aliasname).FirstOrDefault());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Display help information for all available commands, or invoke the help argument for a specifically supplied command.
        /// </summary>
        /// <param name="commands">The list of available commands.</param>
        /// <param name="args">Any arguments passed in.</param>
        /// <returns>A CommandResult option containing properties relevant to how data should be processed by the UI.</returns>
        private CommandResult DisplayHelp(IEnumerable <ICommand> commands, string[] args, CommandResult commandResult)
        {
            var options = new OptionSet();

            options.Add(
                "?|help",
                "Show help information.",
                x =>
            {
                HelpUtility.WriteHelpInformation(
                    commandResult,
                    "HELP",
                    "[Option]",
                    "Displays help information.",
                    options
                    );
            }
                );
            try
            {
                if (args != null)
                {
                    var parsedArgs = options.Parse(args);
                    if (parsedArgs.Count == args.Length)
                    {
                        var commandName = parsedArgs.First();
                        var command     = commands.SingleOrDefault(x => x.Name.Is(commandName));
                        if (command != null)
                        {
                            command.Invoke(new string[] { "-help" });
                        }
                        else
                        {
                            commandResult.WriteLine("'{0}' is not a recognized command.", commandName);
                        }
                    }
                }
                else
                {
                    commandResult.WriteLine("The following commands are available:");
                    commandResult.WriteLine();
                    foreach (ICommand command in commands.OrderBy(x => x.Name))
                    {
                        if (command.ShowHelp)
                        {
                            commandResult.WriteLine(DisplayMode.DontType, "{0}{1}", command.Name.PadRight(15), command.Description);
                        }
                    }
                    commandResult.WriteLine();
                    commandResult.WriteLine("Type \"COMMAND -?\" for details on individual commands.");
                    if (_currentUser != null)
                    {
                        commandResult.WriteLine();
                        commandResult.WriteLine(DisplayMode.Dim | DisplayMode.DontType, new string('-', AppSettings.DividerLength));
                        commandResult.WriteLine();
                        var aliases = _aliasRepository.GetAliases(_currentUser.Username);
                        if (aliases.Count() > 0)
                        {
                            commandResult.WriteLine("You have the following aliases defined:");
                            commandResult.WriteLine();
                            foreach (var alias in aliases)
                            {
                                commandResult.WriteLine(DisplayMode.DontType, "{0}'{1}'", alias.Shortcut.ToUpper().PadRight(15, ' '), alias.Command);
                            }
                        }
                        else
                        {
                            commandResult.WriteLine("You have no aliases defined.");
                        }
                    }
                }
            }
            catch (OptionException ex)
            {
                commandResult.WriteLine(ex.Message);
            }

            return(commandResult);
        }
Exemplo n.º 12
0
        public IActionResult OnGet()
        {
            AntiForgeryToken = _antiforgery.GetAndStoreTokens(HttpContext).RequestToken;
            RemoteIPAddress  = HttpContext.Connection.RemoteIpAddress?.ToString() ?? "";

            if (_configuration.GetSection("Runtime").Exists())
            {
                Runtime = _configuration.GetSection("Runtime").Value;
            }

            if (_configuration.GetSection("RenderMode").Exists())
            {
                RenderMode = (RenderMode)Enum.Parse(typeof(RenderMode), _configuration.GetSection("RenderMode").Value, true);
            }

            // if framework is installed
            if (_configuration.IsInstalled())
            {
                var alias = _tenantManager.GetAlias();
                if (alias != null)
                {
                    var url = WebUtility.UrlDecode(HttpContext.Request.GetEncodedUrl());

                    // redirect non-default alias unless you are trying to access site settings
                    if (!alias.IsDefault && !url.Contains("admin/site"))
                    {
                        var aliases = _aliases.GetAliases().Where(item => item.TenantId == alias.TenantId && item.SiteId == alias.SiteId);
                        if (aliases.Where(item => item.IsDefault).FirstOrDefault() != null)
                        {
                            return(RedirectPermanent(url.Replace(alias.Name, aliases.Where(item => item.IsDefault).FirstOrDefault().Name)));
                        }
                        else // no default specified - use first alias
                        {
                            if (alias.Name.Trim() != aliases.First().Name.Trim())
                            {
                                return(RedirectPermanent(url.Replace(alias.Name, aliases.First().Name)));
                            }
                        }
                    }

                    var site = _sites.GetSite(alias.SiteId);
                    if (site != null && !site.IsDeleted)
                    {
                        Route route = new Route(url, alias.Path);

                        if (!string.IsNullOrEmpty(site.Runtime))
                        {
                            Runtime = site.Runtime;
                        }
                        if (!string.IsNullOrEmpty(site.RenderMode))
                        {
                            RenderMode = (RenderMode)Enum.Parse(typeof(RenderMode), site.RenderMode, true);
                        }
                        if (site.FaviconFileId != null)
                        {
                            FavIcon = Utilities.ContentUrl(alias, site.FaviconFileId.Value);
                        }
                        if (site.PwaIsEnabled && site.PwaAppIconFileId != null && site.PwaSplashIconFileId != null)
                        {
                            PWAScript = CreatePWAScript(alias, site, route);
                        }
                        Title     = site.Name;
                        ThemeType = site.DefaultThemeType;

                        if (site.VisitorTracking)
                        {
                            TrackVisitor(site.SiteId);
                        }

                        var page = _pages.GetPage(route.PagePath, site.SiteId);
                        if (page != null)
                        {
                            // set page title
                            if (!string.IsNullOrEmpty(page.Title))
                            {
                                Title = page.Title;
                            }
                            else
                            {
                                Title = Title + " - " + page.Name;
                            }
                            Meta = page.Meta;

                            // include theme resources
                            if (!string.IsNullOrEmpty(page.ThemeType))
                            {
                                ThemeType = page.ThemeType;
                            }
                        }
                        else // page not found
                        {
                            // look for url mapping
                            var urlMapping = _urlMappings.GetUrlMapping(site.SiteId, route.PagePath);
                            if (urlMapping != null && !string.IsNullOrEmpty(urlMapping.MappedUrl))
                            {
                                url = (urlMapping.MappedUrl.StartsWith("http")) ? urlMapping.MappedUrl : route.SiteUrl + "/" + urlMapping.MappedUrl;
                                return(RedirectPermanent(url));
                            }
                        }

                        // include global resources
                        var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
                        foreach (Assembly assembly in assemblies)
                        {
                            ProcessHostResources(assembly);
                            ProcessModuleControls(assembly);
                            ProcessThemeControls(assembly);
                        }

                        // set culture if not specified
                        string culture = HttpContext.Request.Cookies[CookieRequestCultureProvider.DefaultCookieName];
                        if (culture == null)
                        {
                            // get default language for site
                            var languages = _languages.GetLanguages(alias.SiteId);
                            if (languages.Any())
                            {
                                // use default language if specified otherwise use first language in collection
                                culture = (languages.Where(l => l.IsDefault).SingleOrDefault() ?? languages.First()).Code;
                            }
                            else
                            {
                                culture = _localizationManager.GetDefaultCulture();
                            }
                            SetLocalizationCookie(culture);
                        }

                        // set language for page
                        if (!string.IsNullOrEmpty(culture))
                        {
                            // localization cookie value in form of c=en|uic=en
                            Language = culture.Split('|')[0];
                            Language = Language.Replace("c=", "");
                        }
                    }
                    else
                    {
                        Message = "Site Is Either Disabled Or Not Configured Correctly";
                    }
                }
                else
                {
                    Message = "Site Not Configured Correctly - No Matching Alias Exists For Host Name";
                }
            }
            return(Page());
        }
Exemplo n.º 13
0
        public IActionResult OnGet()
        {
            AntiForgeryToken = _antiforgery.GetAndStoreTokens(HttpContext).RequestToken;
            RemoteIPAddress  = HttpContext.Connection.RemoteIpAddress?.ToString() ?? "";

            if (_configuration.GetSection("Runtime").Exists())
            {
                Runtime = _configuration.GetSection("Runtime").Value;
            }

            if (_configuration.GetSection("RenderMode").Exists())
            {
                RenderMode = (RenderMode)Enum.Parse(typeof(RenderMode), _configuration.GetSection("RenderMode").Value, true);
            }

            // if framework is installed
            if (!string.IsNullOrEmpty(_configuration.GetConnectionString("DefaultConnection")))
            {
                var alias = _tenantManager.GetAlias();
                if (alias != null)
                {
                    var url = WebUtility.UrlDecode(HttpContext.Request.GetEncodedUrl());

                    // redirect non-default alias
                    if (!alias.IsDefault)
                    {
                        var aliases = _aliases.GetAliases().Where(item => item.TenantId == alias.TenantId && item.SiteId == alias.SiteId);
                        if (aliases.Where(item => item.IsDefault).FirstOrDefault() != null)
                        {
                            return(RedirectPermanent(url.Replace(alias.Name, aliases.Where(item => item.IsDefault).FirstOrDefault().Name)));
                        }
                        else // no default specified - use first alias
                        {
                            if (alias.Name.Trim() != aliases.First().Name.Trim())
                            {
                                return(RedirectPermanent(url.Replace(alias.Name, aliases.First().Name)));
                            }
                        }
                    }

                    var site = _sites.GetSite(alias.SiteId);
                    if (site != null)
                    {
                        Route route = new Route(url, alias.Path);

                        if (!string.IsNullOrEmpty(site.Runtime))
                        {
                            Runtime = site.Runtime;
                        }
                        if (!string.IsNullOrEmpty(site.RenderMode))
                        {
                            RenderMode = (RenderMode)Enum.Parse(typeof(RenderMode), site.RenderMode, true);
                        }
                        if (site.FaviconFileId != null)
                        {
                            FavIcon = Utilities.ContentUrl(alias, site.FaviconFileId.Value);
                        }
                        if (site.PwaIsEnabled && site.PwaAppIconFileId != null && site.PwaSplashIconFileId != null)
                        {
                            PWAScript = CreatePWAScript(alias, site, route);
                        }
                        Title     = site.Name;
                        ThemeType = site.DefaultThemeType;

                        if (site.VisitorTracking)
                        {
                            TrackVisitor(site.SiteId);
                        }

                        var page = _pages.GetPage(route.PagePath, site.SiteId);
                        if (page != null)
                        {
                            // set page title
                            if (!string.IsNullOrEmpty(page.Title))
                            {
                                Title = page.Title;
                            }
                            else
                            {
                                Title = Title + " - " + page.Name;
                            }

                            // include theme resources
                            if (!string.IsNullOrEmpty(page.ThemeType))
                            {
                                ThemeType = page.ThemeType;
                            }
                        }
                        else // page not found
                        {
                            // look for url mapping
                            var urlMapping = _urlMappings.GetUrlMapping(site.SiteId, route.PagePath);
                            if (urlMapping != null && !string.IsNullOrEmpty(urlMapping.MappedUrl))
                            {
                                url = (urlMapping.MappedUrl.StartsWith("http")) ? urlMapping.MappedUrl : route.SiteUrl + "/" + urlMapping.MappedUrl;
                                return(RedirectPermanent(url));
                            }
                        }
                    }

                    // include global resources
                    var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
                    foreach (Assembly assembly in assemblies)
                    {
                        ProcessHostResources(assembly);
                        ProcessModuleControls(assembly);
                        ProcessThemeControls(assembly);
                    }

                    // set culture if not specified
                    if (HttpContext.Request.Cookies[CookieRequestCultureProvider.DefaultCookieName] == null)
                    {
                        // set default language for site if the culture is not supported
                        var languages = _languages.GetLanguages(alias.SiteId);
                        if (languages.Any() && languages.All(l => l.Code != CultureInfo.CurrentUICulture.Name))
                        {
                            var defaultLanguage = languages.Where(l => l.IsDefault).SingleOrDefault() ?? languages.First();
                            SetLocalizationCookie(defaultLanguage.Code);
                        }
                        else
                        {
                            SetLocalizationCookie(_localizationManager.GetDefaultCulture());
                        }
                    }
                }
            }
            return(Page());
        }