Example #1
0
        private async Task ExecuteStepAsync(RecipeExecutionContext recipeStep)
        {
            IServiceScope    scope;
            ShellContext     shellContext;
            IServiceProvider serviceProvider;

            if (recipeStep.RecipeDescriptor.RequireNewScope)
            {
                (scope, shellContext) = await _shellHost.GetScopeAndContextAsync(_shellSettings);

                serviceProvider = scope.ServiceProvider;
            }
            else
            {
                (scope, shellContext) = (null, null);
                serviceProvider       = _httpContextAccessor.HttpContext.RequestServices;
            }

            using (scope)
            {
                if (recipeStep.RecipeDescriptor.RequireNewScope && !shellContext.IsActivated)
                {
                    using (var activatingScope = shellContext.CreateScope())
                    {
                        var tenantEvents = activatingScope.ServiceProvider.GetServices <IModularTenantEvents>();

                        foreach (var tenantEvent in tenantEvents)
                        {
                            await tenantEvent.ActivatingAsync();
                        }

                        foreach (var tenantEvent in tenantEvents.Reverse())
                        {
                            await tenantEvent.ActivatedAsync();
                        }
                    }

                    shellContext.IsActivated = true;
                }

                var recipeStepHandlers = serviceProvider.GetServices <IRecipeStepHandler>();
                var scriptingManager   = serviceProvider.GetRequiredService <IScriptingManager>();
                scriptingManager.GlobalMethodProviders.Add(_environmentMethodProvider);

                // Substitutes the script elements by their actual values
                EvaluateScriptNodes(recipeStep, scriptingManager);

                foreach (var recipeStepHandler in recipeStepHandlers)
                {
                    if (Logger.IsEnabled(LogLevel.Information))
                    {
                        Logger.LogInformation("Executing recipe step '{RecipeName}'.", recipeStep.Name);
                    }

                    await _recipeEventHandlers.InvokeAsync(e => e.RecipeStepExecutingAsync(recipeStep), Logger);

                    await recipeStepHandler.ExecuteAsync(recipeStep);

                    await _recipeEventHandlers.InvokeAsync(e => e.RecipeStepExecutedAsync(recipeStep), Logger);

                    if (Logger.IsEnabled(LogLevel.Information))
                    {
                        Logger.LogInformation("Finished executing recipe step '{RecipeName}'.", recipeStep.Name);
                    }
                }
            }

            // E.g if we run migrations defined in a recipe.
            if (!recipeStep.RecipeDescriptor.RequireNewScope)
            {
                return;
            }

            // The recipe execution might have invalidated the shell by enabling new features,
            // so the deferred tasks need to run on an updated shell context if necessary.
            using (var localScope = await _shellHost.GetScopeAsync(_shellSettings))
            {
                var deferredTaskEngine = localScope.ServiceProvider.GetService <IDeferredTaskEngine>();

                // The recipe might have added some deferred tasks to process
                if (deferredTaskEngine != null && deferredTaskEngine.HasPendingTasks)
                {
                    var taskContext = new DeferredTaskContext(localScope.ServiceProvider);
                    await deferredTaskEngine.ExecuteTasksAsync(taskContext);
                }
            }
        }
Example #2
0
        private async Task ExecuteStepAsync(RecipeExecutionContext recipeStep)
        {
            var shellContext = _orchardHost.GetOrCreateShellContext(_shellSettings);

            using (var scope = shellContext.EnterServiceScope())
            {
                if (!shellContext.IsActivated)
                {
                    var tenantEvents = scope.ServiceProvider
                                       .GetServices <IModularTenantEvents>();

                    foreach (var tenantEvent in tenantEvents)
                    {
                        tenantEvent.ActivatingAsync().Wait();
                    }

                    shellContext.IsActivated = true;

                    foreach (var tenantEvent in tenantEvents)
                    {
                        tenantEvent.ActivatedAsync().Wait();
                    }
                }

                var recipeStepHandlers = scope.ServiceProvider.GetServices <IRecipeStepHandler>();
                var scriptingManager   = scope.ServiceProvider.GetRequiredService <IScriptingManager>();
                scriptingManager.GlobalMethodProviders.Add(_environmentMethodProvider);

                // Substitutes the script elements by their actual values
                EvaluateScriptNodes(recipeStep, scriptingManager);

                foreach (var recipeStepHandler in recipeStepHandlers)
                {
                    if (Logger.IsEnabled(LogLevel.Information))
                    {
                        Logger.LogInformation("Executing recipe step '{0}'.", recipeStep.Name);
                    }

                    await _recipeEventHandlers.InvokeAsync(e => e.RecipeStepExecutingAsync(recipeStep), Logger);

                    await recipeStepHandler.ExecuteAsync(recipeStep);

                    await _recipeEventHandlers.InvokeAsync(e => e.RecipeStepExecutedAsync(recipeStep), Logger);

                    if (Logger.IsEnabled(LogLevel.Information))
                    {
                        Logger.LogInformation("Finished executing recipe step '{0}'.", recipeStep.Name);
                    }
                }
            }

            // The recipe execution might have invalidated the shell by enabling new features,
            // so the deferred tasks need to run on an updated shell context if necessary.
            shellContext = _orchardHost.GetOrCreateShellContext(_shellSettings);
            using (var scope = shellContext.EnterServiceScope())
            {
                var deferredTaskEngine = scope.ServiceProvider.GetService <IDeferredTaskEngine>();

                // The recipe might have added some deferred tasks to process
                if (deferredTaskEngine != null && deferredTaskEngine.HasPendingTasks)
                {
                    var taskContext = new DeferredTaskContext(scope.ServiceProvider);
                    await deferredTaskEngine.ExecuteTasksAsync(taskContext);
                }
            }
        }
        //todo: need to add validation, at least to detect when import same thing twice!
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!string.Equals(context.Name, StepName, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            try
            {
                _logger.LogInformation("Running {StepName} for {RecipeName} recipe.", StepName, context.RecipeDescriptor.Name);

                var step = context.Step.ToObject <CypherToContentStepModel>();

                foreach (CypherToContentModel?cypherToContent in step !.Queries ?? Enumerable.Empty <CypherToContentModel?>())
                {
                    if (cypherToContent == null)
                    {
                        continue;
                    }

                    _logger.LogInformation("{StepName} step", StepName);

                    var getContentItemsAsJsonQuery = _serviceProvider.GetRequiredService <IGetContentItemsAsJsonQuery>();

                    getContentItemsAsJsonQuery.QueryStatement = cypherToContent.Query;

                    _logger.LogInformation("Executing query to retrieve content for items:\r\n{Query}",
                                           cypherToContent.Query);

                    // for now, populate from the published graph
                    // we _may_ want to introduce support for creating draft items from the draft replica set at some point
                    List <string> contentItemsJsonPreTokenization = await _graphCluster.Run(GraphReplicaSetNames.Published, getContentItemsAsJsonQuery);

                    IEnumerable <string> contentItemsJson = contentItemsJsonPreTokenization.Select(ReplaceCSharpHelpers);

                    var contentItemJObjects = contentItemsJson
                                              .Select(JsonConvert.DeserializeObject <List <JObject> >)
                                              .SelectMany(cijo => cijo);

                    Stopwatch stopwatch = Stopwatch.StartNew();

                    var preparedContentItems = contentItemJObjects
                                               .Select(PrepareContentItem)
                                               .Where(i => i != null)
                                               .Select(i => i !);

                    foreach (ContentItem preparedContentItem in preparedContentItems)
                    {
                        if (cypherToContent.SyncBackRequired)
                        {
                            await CreateContentItem(preparedContentItem);
                        }
                        else
                        {
                            _session.Save(preparedContentItem);
                        }
                    }

                    //todo: log this, but ensure no double enumeration
                    // _logger.LogInformation($"Created {contentItemJObjects.Count()} content items in {stopwatch.Elapsed}");
                    _logger.LogInformation("Created content items in {TimeTaken}.", stopwatch.Elapsed);
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "CypherToContentStep execute exception.");
                throw;
            }
        }
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!string.Equals(context.Name, "OpenIdApplication", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step.ToObject <CreateOpenIdApplicationViewModel>();

            var descriptor = new OpenIdApplicationDescriptor
            {
                ClientId     = model.ClientId,
                ClientSecret = model.ClientSecret,
                ConsentType  = model.ConsentType,
                DisplayName  = model.DisplayName,
                Type         = model.Type
            };

            if (model.AllowAuthorizationCodeFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode);
            }
            if (model.AllowClientCredentialsFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.ClientCredentials);
            }
            if (model.AllowImplicitFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.Implicit);
            }
            if (model.AllowPasswordFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.Password);
            }
            if (model.AllowRefreshTokenFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.RefreshToken);
            }
            if (model.AllowAuthorizationCodeFlow || model.AllowImplicitFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.Endpoints.Authorization);
            }
            if (model.AllowAuthorizationCodeFlow || model.AllowClientCredentialsFlow ||
                model.AllowPasswordFlow || model.AllowRefreshTokenFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.Endpoints.Token);
            }

            descriptor.PostLogoutRedirectUris.UnionWith(
                from uri in model.PostLogoutRedirectUris?.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty <string>()
                select new Uri(uri, UriKind.Absolute));

            descriptor.PostLogoutRedirectUris.UnionWith(
                from uri in model.RedirectUris?.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty <string>()
                select new Uri(uri, UriKind.Absolute));

            descriptor.Roles.UnionWith(model.RoleEntries
                                       .Where(role => role.Selected)
                                       .Select(role => role.Name));

            await _applicationManager.CreateAsync(descriptor);
        }
 public abstract void Execute(RecipeExecutionContext context);
        private void BatchedInvoke(RecipeExecutionContext context, string batchLabel, Action <string, string, XElement, ImportContentSession, IDictionary <string, XElement> > contentItemAction)
        {
            var importContentSession = new ImportContentSession(_orchardServices.ContentManager);

            // Populate local dictionary with elements and their ids.
            var elementDictionary = CreateElementDictionary(context.RecipeStep.Step);

            // Populate import session with all identities to be imported.
            foreach (var identity in elementDictionary.Keys)
            {
                importContentSession.Set(identity, elementDictionary[identity].Name.LocalName);
            }

            // Determine if the import is to be batched in multiple transactions.
            var batchSize  = GetBatchSizeForDataStep(context.RecipeStep.Step);
            var startIndex = 0;
            var itemIndex  = 0;

            Logger.Debug("Using batch size {0} for '{1}'.", batchSize, batchLabel);

            try {
                while (startIndex < elementDictionary.Count)
                {
                    Logger.Debug("Batch '{0}' execution starting at index {1}.", batchLabel, startIndex);
                    importContentSession.InitializeBatch(startIndex, batchSize);

                    // The session determines which items are included in the current batch
                    // so that dependencies can be managed within the same transaction.
                    var nextIdentity = importContentSession.GetNextInBatch();
                    while (nextIdentity != null)
                    {
                        var itemId            = "";
                        var nextIdentityValue = nextIdentity.ToString();
                        if (elementDictionary[nextIdentityValue].HasAttributes)
                        {
                            itemId = elementDictionary[nextIdentityValue].FirstAttribute.Value;
                        }
                        Logger.Information("Handling content item '{0}' (item {1}/{2} of '{3}').", itemId, itemIndex + 1, elementDictionary.Count, batchLabel);
                        try {
                            contentItemAction(itemId, nextIdentityValue, elementDictionary[nextIdentityValue], importContentSession, elementDictionary);
                        }
                        catch (Exception ex) {
                            Logger.Error(ex, "Error while handling content item '{0}' (item {1}/{2} of '{3}').", itemId, itemIndex + 1, elementDictionary.Count, batchLabel);
                            throw;
                        }
                        itemIndex++;
                        nextIdentity = importContentSession.GetNextInBatch();
                    }

                    startIndex += batchSize;

                    // Create a new transaction for each batch.
                    if (startIndex < elementDictionary.Count)
                    {
                        _transactionManager.RequireNew();
                    }

                    Logger.Debug("Finished batch '{0}' starting at index {1}.", batchLabel, startIndex);
                }
            }
            catch (Exception) {
                // Ensure a failed batch is rolled back.
                _transactionManager.Cancel();
                throw;
            }
        }
Example #7
0
        // <Theme packageId="theme1" repository="somethemerepo" version="1.1" enable="true" current="true" />
        // Install themes from feed.
        public override void Execute(RecipeExecutionContext context)
        {
            bool   enable = false, current = false;
            string packageId = null, version = null, repository = null;

            foreach (var attribute in context.RecipeStep.Step.Attributes())
            {
                if (String.Equals(attribute.Name.LocalName, "enable", StringComparison.OrdinalIgnoreCase))
                {
                    enable = Boolean.Parse(attribute.Value);
                }
                else if (String.Equals(attribute.Name.LocalName, "current", StringComparison.OrdinalIgnoreCase))
                {
                    current = Boolean.Parse(attribute.Value);
                }
                else if (String.Equals(attribute.Name.LocalName, "packageId", StringComparison.OrdinalIgnoreCase))
                {
                    packageId = attribute.Value;
                }
                else if (String.Equals(attribute.Name.LocalName, "version", StringComparison.OrdinalIgnoreCase))
                {
                    version = attribute.Value;
                }
                else if (String.Equals(attribute.Name.LocalName, "repository", StringComparison.OrdinalIgnoreCase))
                {
                    repository = attribute.Value;
                }
                else
                {
                    Logger.Warning("Unrecognized attribute '{0}' encountered; skipping.", attribute.Name.LocalName);
                }
            }

            if (packageId == null)
            {
                throw new InvalidOperationException("The PackageId attribute is required on a Theme declaration in a recipe file.");
            }

            // Download and install theme from the orchard feed or a custom feed if repository is specified.
            var            enforceVersion = version != null;
            var            installed      = false;
            PackagingEntry packagingEntry = null;

            var packagingSource = _packagingSourceManager.GetSources().FirstOrDefault();

            if (repository != null)
            {
                packagingSource = new PackagingSource {
                    FeedTitle = repository, FeedUrl = repository
                };
            }

            if (enforceVersion)
            {
                packagingEntry = _packagingSourceManager.GetExtensionList(false, packagingSource,
                                                                          packages => packages.Where(package =>
                                                                                                     package.PackageType.Equals(DefaultExtensionTypes.Theme) &&
                                                                                                     package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) &&
                                                                                                     package.Version.Equals(version, StringComparison.OrdinalIgnoreCase))).FirstOrDefault();
            }
            else
            {
                packagingEntry = _packagingSourceManager.GetExtensionList(false, packagingSource,
                                                                          packages => packages.Where(package =>
                                                                                                     package.PackageType.Equals(DefaultExtensionTypes.Theme) &&
                                                                                                     package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) &&
                                                                                                     package.IsLatestVersion)).FirstOrDefault();
            }

            if (packagingEntry != null)
            {
                if (!ThemeAlreadyInstalled(packagingEntry.PackageId))
                {
                    Logger.Information("Installing theme package '{0}'.", packagingEntry.PackageId);
                    _packageManager.Install(packagingEntry.PackageId, packagingEntry.Version, packagingSource.FeedUrl, HostingEnvironment.MapPath("~/"));
                }
                if (current)
                {
                    Logger.Information("Enabling theme '{0}'.", packagingEntry.Title);
                    _themeService.EnableThemeFeatures(packagingEntry.Title);
                    Logger.Information("Setting theme '{0}' as the site theme.", packagingEntry.Title);
                    _siteThemeService.SetSiteTheme(packagingEntry.Title);
                }
                else if (enable)
                {
                    Logger.Information("Enabling theme '{0}'.", packagingEntry.Title);
                    _themeService.EnableThemeFeatures(packagingEntry.Title);
                }

                installed = true;
            }

            if (!installed)
            {
                throw new InvalidOperationException(String.Format("Theme '{0}' was not found in the specified location.", packageId));
            }
        }
Example #8
0
        public override async Task ExecuteAsync(RecipeExecutionContext recipeContext)
        {
            var model = recipeContext.RecipeStep.Step;
            var site  = await _siteService.GetSiteSettingsAsync();

            if (model["BaseUrl"] != null)
            {
                site.BaseUrl = model["BaseUrl"].ToString();
            }

            if (model["Calendar"] != null)
            {
                site.Calendar = model["Calendar"].ToString();
            }

            if (model["Culture"] != null)
            {
                site.Culture = model["Culture"].ToString();
            }

            if (model["MaxPagedCount"] != null)
            {
                site.MaxPagedCount = model.Value <int>("MaxPagedCount");
            }

            if (model["MaxPageSize"] != null)
            {
                site.MaxPageSize = model.Value <int>("MaxPageSize");
            }

            if (model["PageSize"] != null)
            {
                site.PageSize = model.Value <int>("PageSize");
            }

            if (model["ResourceDebugMode"] != null)
            {
                site.ResourceDebugMode = model.Value <ResourceDebugMode>("ResourceDebugMode");
            }

            if (model["SiteName"] != null)
            {
                site.SiteName = model["SiteName"].ToString();
            }

            if (model["SiteSalt"] != null)
            {
                site.SiteSalt = model["SiteSalt"].ToString();
            }

            if (model["SuperUser"] != null)
            {
                site.SuperUser = model["SuperUser"].ToString();
            }

            if (model["TimeZone"] != null)
            {
                site.TimeZone = model["TimeZone"].ToString();
            }

            if (model["UseCdn"] != null)
            {
                site.UseCdn = model.Value <bool>("UseCdn");
            }

            if (model["HomeRoute"] != null)
            {
                site.HomeRoute = model["HomeRoute"].ToObject <RouteValueDictionary>();
            }

            await _siteService.UpdateSiteSettingsAsync(site);
        }
Example #9
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!string.Equals(context.Name, "OpenIdApplication", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step.ToObject <OpenIdApplicationStepModel>();
            var app   = await _applicationManager.FindByClientIdAsync(model.ClientId);

            var descriptor = new OpenIdApplicationDescriptor();
            var isNew      = true;

            if (app != null)
            {
                isNew = false;
                await _applicationManager.PopulateAsync(app, descriptor);
            }

            descriptor.ClientId     = model.ClientId;
            descriptor.ClientSecret = model.ClientSecret;
            descriptor.ConsentType  = model.ConsentType;
            descriptor.DisplayName  = model.DisplayName;
            descriptor.Type         = model.Type;

            if (model.AllowAuthorizationCodeFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode);
            }
            if (model.AllowClientCredentialsFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.ClientCredentials);
            }
            if (model.AllowImplicitFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.Implicit);
            }
            if (model.AllowPasswordFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.Password);
            }
            if (model.AllowRefreshTokenFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.RefreshToken);
            }
            if (model.AllowAuthorizationCodeFlow || model.AllowImplicitFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.Endpoints.Authorization);
            }
            if (model.AllowLogoutEndpoint)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.Endpoints.Logout);
            }
            if (model.AllowAuthorizationCodeFlow || model.AllowClientCredentialsFlow ||
                model.AllowPasswordFlow || model.AllowRefreshTokenFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.Endpoints.Token);
            }
            if (model.AllowAuthorizationCodeFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.Code);
            }
            if (model.AllowImplicitFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.IdToken);

                if (string.Equals(model.Type, OpenIddictConstants.ClientTypes.Public, StringComparison.OrdinalIgnoreCase))
                {
                    descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.IdTokenToken);
                    descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.Token);
                }
            }
            if (model.AllowHybridFlow)
            {
                descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.CodeIdToken);

                if (string.Equals(model.Type, OpenIddictConstants.ClientTypes.Public, StringComparison.OrdinalIgnoreCase))
                {
                    descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.CodeIdTokenToken);
                    descriptor.Permissions.Add(OpenIddictConstants.Permissions.ResponseTypes.CodeToken);
                }
            }
            if (!string.IsNullOrWhiteSpace(model.PostLogoutRedirectUris))
            {
                descriptor.PostLogoutRedirectUris.UnionWith(
                    model.PostLogoutRedirectUris
                    .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                    .Select(u => new Uri(u, UriKind.Absolute)));
            }
            if (!string.IsNullOrWhiteSpace(model.RedirectUris))
            {
                descriptor.RedirectUris.UnionWith(
                    model.RedirectUris
                    .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                    .Select(u => new Uri(u, UriKind.Absolute)));
            }
            if (model.RoleEntries != null)
            {
                descriptor.Roles.UnionWith(
                    model.RoleEntries
                    .Select(role => role.Name));
            }
            if (model.ScopeEntries != null)
            {
                descriptor.Permissions.UnionWith(
                    model.ScopeEntries
                    .Select(scope => OpenIddictConstants.Permissions.Prefixes.Scope + scope.Name));
            }
            if (isNew)
            {
                await _applicationManager.CreateAsync(descriptor);
            }
            else
            {
                await _applicationManager.UpdateAsync(app, descriptor);
            }
        }
Example #10
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!String.Equals(context.Name, "Layers", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step.ToObject <LayersStepModel>();

            var allLayers = await _layerService.LoadLayersAsync();

            var unknownTypes = new List <string>();
            var factories    = _factories.ToDictionary(x => x.Name);

            foreach (var layerStep in model.Layers)
            {
                var layer = allLayers.Layers.FirstOrDefault(x => String.Equals(x.Name, layerStep.Name, StringComparison.OrdinalIgnoreCase));

                if (layer == null)
                {
                    layer = new Layer();
                    allLayers.Layers.Add(layer);
                }

                // Backwards compatability check.
                if (layer.LayerRule == null)
                {
                    layer.LayerRule = new Rule();
                    _conditionIdGenerator.GenerateUniqueId(layer.LayerRule);
                }

                // Replace any property that is set in the recipe step
                if (!String.IsNullOrEmpty(layerStep.Name))
                {
                    layer.Name = layerStep.Name;
                }
                else
                {
                    throw new ArgumentNullException($"{nameof(layer.Name)} is required");
                }

                if (layerStep.LayerRule != null)
                {
                    if (!String.IsNullOrEmpty(layerStep.LayerRule.ConditionId))
                    {
                        layer.LayerRule.ConditionId = layerStep.LayerRule.ConditionId;
                    }

                    // The conditions list is cleared, because we cannot logically merge conditions.
                    layer.LayerRule.Conditions.Clear();
                    foreach (var jCondition in layerStep.LayerRule.Conditions)
                    {
                        var name = jCondition["Name"].ToString();
                        if (factories.TryGetValue(name, out var factory))
                        {
                            var factoryCondition = (Condition)jCondition.ToObject(factory.Create().GetType(), JsonSerializer);

                            layer.LayerRule.Conditions.Add(factoryCondition);
                        }
                        else
                        {
                            unknownTypes.Add(name);
                        }
                    }
                }

#pragma warning disable 0618
                // Migrate any old rule in a recipe to the new rule format.
                // Do not import the old rule.
                if (!String.IsNullOrEmpty(layerStep.Rule))
                {
                    _ruleMigrator.Migrate(layerStep.Rule, layer.LayerRule);
                }
#pragma warning restore 0618

                if (!String.IsNullOrEmpty(layerStep.Description))
                {
                    layer.Description = layerStep.Description;
                }
            }

            if (unknownTypes.Count != 0)
            {
                var prefix = "No changes have been made. The following types of conditions cannot be added:";
                var suffix = "Please ensure that the related features are enabled to add these types of conditions.";

                throw new InvalidOperationException($"{prefix} {String.Join(", ", unknownTypes)}. {suffix}");
            }

            await _layerService.UpdateAsync(allLayers);
        }
        public async Task OpenIdApplicationCanBeUpdated()
        {
            // Arrange
            var recipeName = "app-recipe3";
            var clientId   = "a1";
            var expected   = new OpenIdApplicationDescriptor
            {
                ClientId    = clientId,
                DisplayName = "Expected Name"
            };

            expected.RedirectUris.UnionWith(new[] { new Uri("https://localhost/redirect") });

            var actual = new OpenIdApplicationDescriptor
            {
                ClientId    = clientId,
                DisplayName = "Actual Name"
            };

            actual.RedirectUris.UnionWith(new[] { new Uri("https://localhost/x") });
            actual.Roles.UnionWith(new[] { "x" });
            actual.Permissions.UnionWith(new[] { $"{Permissions.Prefixes.Scope}x" });

            var actualDb = new OpenIdApplication
            {
                ClientId     = actual.ClientId,
                DisplayName  = actual.DisplayName,
                RedirectUris = actual.RedirectUris.Select(u => u.AbsoluteUri).ToImmutableArray(),
                Roles        = actual.Roles.ToImmutableArray(),
                Permissions  = actual.Permissions.ToImmutableArray()
            };

            var appManagerMock = new Mock <IOpenIdApplicationManager>(MockBehavior.Strict);

            appManagerMock.Setup(m =>
                                 m.FindByClientIdAsync(
                                     It.IsAny <string>(),
                                     It.IsAny <CancellationToken>()))
            .Returns(
                new ValueTask <object>(actualDb));

            appManagerMock.Setup(m =>
                                 m.PopulateAsync(
                                     It.IsAny <object>(),
                                     It.IsAny <OpenIddictApplicationDescriptor>(),
                                     It.IsAny <CancellationToken>()))
            .Returns(
                new ValueTask());

            appManagerMock.Setup(m =>
                                 m.UpdateAsync(
                                     It.IsAny <object>(),
                                     It.IsAny <OpenIdApplicationDescriptor>(),
                                     It.IsAny <CancellationToken>()))
            .Callback <object, OpenIddictApplicationDescriptor, CancellationToken>((app, desc, c) =>
                                                                                   actual = (OpenIdApplicationDescriptor)desc)
            .Returns(
                new ValueTask());

            var step    = new OpenIdApplicationStep(appManagerMock.Object);
            var recipe  = JObject.Parse(GetRecipeFileContent(recipeName));
            var context = new RecipeExecutionContext
            {
                Name = recipe.Property("steps").Value.First.Value <string>("name"),
                Step = (JObject)recipe.Property("steps").Value.First,
            };

            // Act
            await step.ExecuteAsync(context);

            // Assert
            appManagerMock.Verify(m =>
                                  m.FindByClientIdAsync(
                                      It.Is <string>(ci => ci == expected.ClientId),
                                      It.IsAny <CancellationToken>()));

            appManagerMock.Verify(m =>
                                  m.UpdateAsync(
                                      It.IsAny <object>(),
                                      It.IsAny <OpenIdApplicationDescriptor>(),
                                      It.IsAny <CancellationToken>()));

            Assert.Equal(expected.ClientId, actual.ClientId);
            Assert.Equal(expected.ClientSecret, actual.ClientSecret);
            Assert.Equal(expected.ConsentType, actual.ConsentType);
            Assert.Equal(expected.DisplayName, actual.DisplayName);
            Assert.Equal(expected.Type, actual.Type);
            Assert.Equal(expected.Permissions, actual.Permissions);
            Assert.Equal(expected.PostLogoutRedirectUris, actual.PostLogoutRedirectUris);
            Assert.Equal(expected.RedirectUris, actual.RedirectUris);
            Assert.Equal(expected.Roles, actual.Roles);
        }
Example #12
0
        // <Module packageId="module1" [repository="somerepo"] version="1.1" />
        // Install modules from feed.
        public override void Execute(RecipeExecutionContext context)
        {
            string packageId = null, version = null, repository = null;

            foreach (var attribute in context.RecipeStep.Step.Attributes())
            {
                if (String.Equals(attribute.Name.LocalName, "packageId", StringComparison.OrdinalIgnoreCase))
                {
                    packageId = attribute.Value;
                }
                else if (String.Equals(attribute.Name.LocalName, "version", StringComparison.OrdinalIgnoreCase))
                {
                    version = attribute.Value;
                }
                else if (String.Equals(attribute.Name.LocalName, "repository", StringComparison.OrdinalIgnoreCase))
                {
                    repository = attribute.Value;
                }
                else
                {
                    throw new InvalidOperationException(String.Format("Unrecognized attribute {0} encountered in step Module.", attribute.Name.LocalName));
                }
            }

            if (packageId == null)
            {
                throw new InvalidOperationException("PackageId is required in a module declaration in a recipe file.");
            }

            // download and install module from the orchard feed or a custom feed if repository is specified.
            var            enforceVersion = version != null;
            var            installed      = false;
            PackagingEntry packagingEntry = null;

            var packagingSource = _packagingSourceManager.GetSources().FirstOrDefault();

            if (repository != null)
            {
                packagingSource = new PackagingSource {
                    FeedTitle = repository, FeedUrl = repository
                };
            }

            if (enforceVersion)
            {
                packagingEntry = _packagingSourceManager.GetExtensionList(false, packagingSource,
                                                                          packages => packages.Where(package =>
                                                                                                     package.PackageType.Equals(DefaultExtensionTypes.Module) &&
                                                                                                     package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) &&
                                                                                                     package.Version.Equals(version, StringComparison.OrdinalIgnoreCase))).FirstOrDefault();
            }
            else
            {
                packagingEntry = _packagingSourceManager.GetExtensionList(false, packagingSource,
                                                                          packages => packages.Where(package =>
                                                                                                     package.PackageType.Equals(DefaultExtensionTypes.Module) &&
                                                                                                     package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) &&
                                                                                                     package.IsLatestVersion)).FirstOrDefault();
            }

            if (packagingEntry != null)
            {
                if (!ModuleAlreadyInstalled(packagingEntry.PackageId))
                {
                    Logger.Information("Installing module {0}.", packagingEntry.Title);
                    _packageManager.Install(packagingEntry.PackageId, packagingEntry.Version, packagingSource.FeedUrl, HostingEnvironment.MapPath("~/"));
                }
                installed = true;
            }

            if (!installed)
            {
                throw new InvalidOperationException(String.Format("Module {0} was not found in the specified location.", packageId));
            }
        }
Example #13
0
        public async Task OpenIdScopeCanBeUpdated()
        {
            // Arrange

            // Match expected with scope-recipe.json
            var scopeName = "test_scope";
            var expected  = CreateScopeDescriptor(
                scopeName, "A", "res1", "res2", "res3");
            var actual = CreateScopeDescriptor(
                scopeName, "B", "res");
            var dbActual = new OpenIdScope
            {
                Name      = actual.Name,
                Resources = actual.Resources.ToImmutableArray()
            };
            var scopeManagerMock = new Mock <IOpenIdScopeManager>(MockBehavior.Strict);

            scopeManagerMock.Setup(m =>
                                   m.FindByNameAsync(
                                       It.IsAny <string>(),
                                       It.IsAny <CancellationToken>()))
            .Returns(
                new ValueTask <object>(dbActual));

            scopeManagerMock.Setup(m =>
                                   m.PopulateAsync(
                                       It.IsAny <object>(),
                                       It.IsAny <OpenIdScopeDescriptor>(),
                                       It.IsAny <CancellationToken>()))
            .Returns(
                new ValueTask());

            scopeManagerMock.Setup(m =>
                                   m.UpdateAsync(
                                       It.IsAny <object>(),
                                       It.IsAny <OpenIdScopeDescriptor>(),
                                       It.IsAny <CancellationToken>()))
            .Callback <object, OpenIddictScopeDescriptor, CancellationToken>((s, desc, c) =>
                                                                             actual = (OpenIdScopeDescriptor)desc)
            .Returns(
                new ValueTask());

            var step    = new OpenIdScopeStep(scopeManagerMock.Object);
            var recipe  = JObject.Parse(GetRecipeFileContent("scope-recipe"));
            var context = new RecipeExecutionContext
            {
                Name = recipe.Property("steps").Value.First.Value <string>("name"),
                Step = (JObject)recipe.Property("steps").Value.First,
            };

            // Act
            await step.ExecuteAsync(context);

            // Assert
            scopeManagerMock.Verify(m =>
                                    m.FindByNameAsync(
                                        It.Is <string>(v => v == expected.Name),
                                        It.IsAny <CancellationToken>()));

            scopeManagerMock.Verify(m =>
                                    m.PopulateAsync(
                                        It.IsAny <object>(),
                                        It.IsAny <OpenIdScopeDescriptor>(),
                                        It.IsAny <CancellationToken>()));

            scopeManagerMock.Verify(m =>
                                    m.UpdateAsync(
                                        It.IsAny <object>(),
                                        It.IsAny <OpenIdScopeDescriptor>(),
                                        It.IsAny <CancellationToken>()));

            Assert.Equal(expected.Name, actual.Name);
            Assert.Equal(expected.DisplayName, actual.DisplayName);
            Assert.Equal(expected.Description, actual.Description);
            Assert.Equal(expected.Resources.ToArray(), actual.Resources.ToArray());
        }
Example #14
0
        public async Task <string> ExecuteAsync(string executionId, RecipeDescriptor recipeDescriptor, object environment)
        {
            await _recipeEventHandlers.InvokeAsync(x => x.RecipeExecutingAsync(executionId, recipeDescriptor), Logger);

            try
            {
                _environmentMethodProvider = new ParametersMethodProvider(environment);

                var result = new RecipeResult {
                    ExecutionId = executionId
                };

                await _recipeStore.CreateAsync(result);

                using (StreamReader file = File.OpenText(recipeDescriptor.RecipeFileInfo.PhysicalPath))
                {
                    using (var reader = new JsonTextReader(file))
                    {
                        // Go to Steps, then iterate.
                        while (reader.Read())
                        {
                            if (reader.Path == "variables")
                            {
                                reader.Read();

                                var variables = JObject.Load(reader);
                                _variablesMethodProvider = new VariablesMethodProvider(variables);
                            }

                            if (reader.Path == "steps" && reader.TokenType == JsonToken.StartArray)
                            {
                                while (reader.Read() && reader.Depth > 1)
                                {
                                    if (reader.Depth == 2)
                                    {
                                        var child = JObject.Load(reader);

                                        var recipeStep = new RecipeExecutionContext
                                        {
                                            Name        = child.Value <string>("name"),
                                            Step        = child,
                                            ExecutionId = executionId,
                                            Environment = environment
                                        };

                                        var stepResult = new RecipeStepResult {
                                            StepName = recipeStep.Name
                                        };
                                        result.Steps.Add(stepResult);
                                        await _recipeStore.UpdateAsync(result);

                                        ExceptionDispatchInfo capturedException = null;
                                        try
                                        {
                                            await ExecuteStepAsync(recipeStep);

                                            stepResult.IsSuccessful = true;
                                        }
                                        catch (Exception e)
                                        {
                                            stepResult.IsSuccessful = false;
                                            stepResult.ErrorMessage = e.ToString();

                                            // Because we can't do some async processing the in catch or finally
                                            // blocks, we store the exception to throw it later.

                                            capturedException = ExceptionDispatchInfo.Capture(e);
                                        }

                                        stepResult.IsCompleted = true;
                                        await _recipeStore.UpdateAsync(result);

                                        if (stepResult.IsSuccessful == false)
                                        {
                                            capturedException.Throw();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                await _recipeEventHandlers.InvokeAsync(x => x.RecipeExecutedAsync(executionId, recipeDescriptor), Logger);

                return(executionId);
            }
            catch (Exception)
            {
                await _recipeEventHandlers.InvokeAsync(x => x.ExecutionFailedAsync(executionId, recipeDescriptor), Logger);

                throw;
            }
        }
Example #15
0
 public override void Execute(RecipeExecutionContext context)
 {
     _sweepGenerator.Activate();
 }
Example #16
0
 public abstract Task ExecuteAsync(RecipeExecutionContext context);
Example #17
0
 public override void Execute(RecipeExecutionContext context)
 {
     _shellSettings.State = TenantState.Running;
     _shellSettingsManager.SaveSettings(_shellSettings);
 }
Example #18
0
        public async Task <string> ExecuteAsync(string executionId, RecipeDescriptor recipeDescriptor, object environment, CancellationToken cancellationToken)
        {
            await _recipeEventHandlers.InvokeAsync((handler, executionId, recipeDescriptor) => handler.RecipeExecutingAsync(executionId, recipeDescriptor), executionId, recipeDescriptor, _logger);

            try
            {
                var methodProviders = new List <IGlobalMethodProvider>();
                _methodProviders.Add(executionId, methodProviders);

                methodProviders.Add(new ParametersMethodProvider(environment));
                methodProviders.Add(new ConfigurationMethodProvider(_shellSettings.ShellConfiguration));

                var result = new RecipeResult {
                    ExecutionId = executionId
                };

                using (var stream = recipeDescriptor.RecipeFileInfo.CreateReadStream())
                {
                    using var file   = new StreamReader(stream);
                    using var reader = new JsonTextReader(file);

                    // Go to Steps, then iterate.
                    while (await reader.ReadAsync())
                    {
                        if (reader.Path == "variables")
                        {
                            await reader.ReadAsync();

                            var variables = await JObject.LoadAsync(reader);

                            methodProviders.Add(new VariablesMethodProvider(variables));
                        }

                        if (reader.Path == "steps" && reader.TokenType == JsonToken.StartArray)
                        {
                            while (await reader.ReadAsync() && reader.Depth > 1)
                            {
                                if (reader.Depth == 2)
                                {
                                    var child = await JObject.LoadAsync(reader);

                                    var recipeStep = new RecipeExecutionContext
                                    {
                                        Name             = child.Value <string>("name"),
                                        Step             = child,
                                        ExecutionId      = executionId,
                                        Environment      = environment,
                                        RecipeDescriptor = recipeDescriptor
                                    };

                                    if (cancellationToken.IsCancellationRequested)
                                    {
                                        _logger.LogError("Recipe interrupted by cancellation token.");
                                        return(null);
                                    }

                                    var stepResult = new RecipeStepResult {
                                        StepName = recipeStep.Name
                                    };
                                    result.Steps.Add(stepResult);

                                    ExceptionDispatchInfo capturedException = null;
                                    try
                                    {
                                        await ExecuteStepAsync(recipeStep);

                                        stepResult.IsSuccessful = true;
                                    }
                                    catch (Exception e)
                                    {
                                        stepResult.IsSuccessful = false;
                                        stepResult.ErrorMessage = e.ToString();

                                        // Because we can't do some async processing the in catch or finally
                                        // blocks, we store the exception to throw it later.

                                        capturedException = ExceptionDispatchInfo.Capture(e);
                                    }

                                    stepResult.IsCompleted = true;

                                    if (stepResult.IsSuccessful == false)
                                    {
                                        capturedException.Throw();
                                    }

                                    if (recipeStep.InnerRecipes != null)
                                    {
                                        foreach (var descriptor in recipeStep.InnerRecipes)
                                        {
                                            var innerExecutionId = Guid.NewGuid().ToString();
                                            await ExecuteAsync(innerExecutionId, descriptor, environment, cancellationToken);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                await _recipeEventHandlers.InvokeAsync((handler, executionId, recipeDescriptor) => handler.RecipeExecutedAsync(executionId, recipeDescriptor), executionId, recipeDescriptor, _logger);

                return(executionId);
            }
            catch (Exception)
            {
                await _recipeEventHandlers.InvokeAsync((handler, executionId, recipeDescriptor) => handler.ExecutionFailedAsync(executionId, recipeDescriptor), executionId, recipeDescriptor, _logger);

                throw;
            }
            finally
            {
                _methodProviders.Remove(executionId);
            }
        }
Example #19
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!String.Equals(context.Name, "Settings", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step;
            var site  = await _siteService.GetSiteSettingsAsync();

            foreach (JProperty property in model.Properties())
            {
                switch (property.Name)
                {
                case "BaseUrl":
                    site.BaseUrl = property.Value.ToString();
                    break;

                case "Calendar":
                    site.Calendar = property.Value.ToString();
                    break;

                case "Culture":
                    site.Culture = property.Value.ToString();
                    break;

                case "MaxPagedCount":
                    site.MaxPagedCount = property.Value <int>();
                    break;

                case "MaxPageSize":
                    site.MaxPageSize = property.Value <int>();
                    break;

                case "PageSize":
                    site.PageSize = property.Value <int>();
                    break;

                case "ResourceDebugMode":
                    site.ResourceDebugMode = property.Value <ResourceDebugMode>();
                    break;

                case "SiteName":
                    site.SiteName = property.ToString();
                    break;

                case "SiteSalt":
                    site.SiteSalt = property.ToString();
                    break;

                case "SuperUser":
                    site.SuperUser = property.ToString();
                    break;

                case "TimeZone":
                    site.TimeZoneId = property.ToString();
                    break;

                case "UseCdn":
                    site.UseCdn = property.Value <bool>();
                    break;

                case "HomeRoute":
                    site.HomeRoute = property.Value.ToObject <RouteValueDictionary>();
                    break;

                default:
                    site.Properties.Add(property);
                    break;
                }
            }

            await _siteService.UpdateSiteSettingsAsync(site);
        }
 public Task RecipeStepExecutingAsync(RecipeExecutionContext context) => Task.CompletedTask;
Example #21
0
 public Task Handle(RecipeExecutionContext context, CancellationToken cancellationToken = default)
 {
     throw new System.NotImplementedException();
 }
 public override void Execute(RecipeExecutionContext context)
 {
     IsExecuted = true;
 }
 public Task ExecuteAsync(RecipeExecutionContext context)
 {
     //throw new NotImplementedException();
     return(Task.CompletedTask);
 }