Skip to content

Commit

Permalink
Tenant Workflows: Disable and Enable Tasks (#4765)
Browse files Browse the repository at this point in the history
  • Loading branch information
agriffard authored Mar 19, 2020
1 parent d5ff090 commit 88faa82
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Localization;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;
Expand All @@ -13,12 +14,9 @@ namespace OrchardCore.Tenants.Workflows.Activities
{
public class CreateTenantTask : TenantTask
{
private readonly IWorkflowExpressionEvaluator _expressionEvaluator;

public CreateTenantTask(IShellSettingsManager shellSettingsManager, IShellHost shellHost, IWorkflowExpressionEvaluator expressionEvaluator, IWorkflowScriptEvaluator scriptEvaluator, IStringLocalizer<CreateTenantTask> localizer)
: base(shellSettingsManager, shellHost, scriptEvaluator, localizer)
: base(shellSettingsManager, shellHost, expressionEvaluator, scriptEvaluator, localizer)
{
_expressionEvaluator = expressionEvaluator;
}

public override string Name => nameof(CreateTenantTask);
Expand Down Expand Up @@ -77,43 +75,75 @@ public WorkflowExpression<string> RecipeName

public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Done"]);
return Outcomes(S["Done"], S["Failed"]);
}

public async override Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
var tenantNameTask = _expressionEvaluator.EvaluateAsync(TenantName, workflowContext);
var tenantDescriptionTask = _expressionEvaluator.EvaluateAsync(Description, workflowContext);
var requestUrlPrefixTask = _expressionEvaluator.EvaluateAsync(RequestUrlPrefix, workflowContext);
var requestUrlHostTask = _expressionEvaluator.EvaluateAsync(RequestUrlHost, workflowContext);
var databaseProviderTask = _expressionEvaluator.EvaluateAsync(DatabaseProvider, workflowContext);
var connectionStringTask = _expressionEvaluator.EvaluateAsync(ConnectionString, workflowContext);
var tablePrefixTask = _expressionEvaluator.EvaluateAsync(TablePrefix, workflowContext);
var recipeNameTask = _expressionEvaluator.EvaluateAsync(RecipeName, workflowContext);
if (ShellScope.Context.Settings.Name != ShellHelper.DefaultShellName)
{
return Outcomes("Failed");
}

var tenantName = (await ExpressionEvaluator.EvaluateAsync(TenantName, workflowContext))?.Trim();

if (string.IsNullOrEmpty(tenantName))
{
return Outcomes("Failed");
}

if (ShellHost.TryGetSettings(tenantName, out var shellSettings))
{
return Outcomes("Failed");
}

var requestUrlPrefix = (await ExpressionEvaluator.EvaluateAsync(RequestUrlPrefix, workflowContext))?.Trim();
var requestUrlHost = (await ExpressionEvaluator.EvaluateAsync(RequestUrlHost, workflowContext))?.Trim();
var databaseProvider = (await ExpressionEvaluator.EvaluateAsync(DatabaseProvider, workflowContext))?.Trim();
var connectionString = (await ExpressionEvaluator.EvaluateAsync(ConnectionString, workflowContext))?.Trim();
var tablePrefix = (await ExpressionEvaluator.EvaluateAsync(TablePrefix, workflowContext))?.Trim();
var recipeName = (await ExpressionEvaluator.EvaluateAsync(RecipeName, workflowContext))?.Trim();

// Creates a default shell settings based on the configuration.
shellSettings = ShellSettingsManager.CreateDefaultSettings();

shellSettings.Name = tenantName;

if (!string.IsNullOrEmpty(requestUrlHost))
{
shellSettings.RequestUrlHost = requestUrlHost;
}

if (!string.IsNullOrEmpty(requestUrlPrefix))
{
shellSettings.RequestUrlPrefix = requestUrlPrefix;
}

shellSettings.State = TenantState.Uninitialized;

if (!string.IsNullOrEmpty(connectionString))
{
shellSettings["ConnectionString"] = connectionString;
}

await Task.WhenAll(tenantNameTask, tenantDescriptionTask, requestUrlPrefixTask, requestUrlHostTask, databaseProviderTask, connectionStringTask, tablePrefixTask, recipeNameTask);
if (!string.IsNullOrEmpty(tablePrefix))
{
shellSettings["TablePrefix"] = tablePrefix;
}

var shellSettings = new ShellSettings();
if (!string.IsNullOrEmpty(databaseProvider))
{
shellSettings["DatabaseProvider"] = databaseProvider;
}

if (!string.IsNullOrWhiteSpace(tenantNameTask.Result))
if (!string.IsNullOrEmpty(recipeName))
{
shellSettings = new ShellSettings
{
Name = tenantNameTask.Result?.Trim(),
RequestUrlPrefix = requestUrlPrefixTask.Result?.Trim(),
RequestUrlHost = requestUrlHostTask.Result?.Trim(),
State = TenantState.Uninitialized
};
shellSettings["Description"] = tenantDescriptionTask.Result?.Trim();
shellSettings["ConnectionString"] = connectionStringTask.Result?.Trim();
shellSettings["TablePrefix"] = tablePrefixTask.Result?.Trim();
shellSettings["DatabaseProvider"] = databaseProviderTask.Result?.Trim();
shellSettings["Secret"] = Guid.NewGuid().ToString();
shellSettings["RecipeName"] = recipeNameTask.Result.Trim();
shellSettings["RecipeName"] = recipeName;
}

await ShellSettingsManager.SaveSettingsAsync(shellSettings);
var shellContext = await ShellHost.GetOrCreateShellContextAsync(shellSettings);
shellSettings["Secret"] = Guid.NewGuid().ToString();

await ShellHost.UpdateShellSettingsAsync(shellSettings);

workflowContext.LastResult = shellSettings;
workflowContext.CorrelationId = shellSettings.Name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;
using OrchardCore.Workflows.Services;

namespace OrchardCore.Tenants.Workflows.Activities
{
public class DisableTenantTask : TenantTask
{
public DisableTenantTask(IShellSettingsManager shellSettingsManager, IShellHost shellHost, IWorkflowScriptEvaluator scriptEvaluator, IStringLocalizer<DisableTenantTask> localizer)
: base(shellSettingsManager, shellHost, scriptEvaluator, localizer)
public DisableTenantTask(IShellSettingsManager shellSettingsManager, IShellHost shellHost, IWorkflowExpressionEvaluator expressionEvaluator, IWorkflowScriptEvaluator scriptEvaluator, IStringLocalizer<DisableTenantTask> localizer)
: base(shellSettingsManager, shellHost, expressionEvaluator, scriptEvaluator, localizer)
{
}

public override string Name => nameof(DisableTenantTask);

public override LocalizedString Category => S["Tenant"];
Expand All @@ -22,16 +25,37 @@ public DisableTenantTask(IShellSettingsManager shellSettingsManager, IShellHost

public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Disabled"]);
return Outcomes(S["Disabled"], S["Failed"]);
}

//public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
//{
// //var shellSettings = await GetTenantAsync(workflowContext);
// //shellSettings.State = TenantState.Disabled;
// //await ShellHost.UpdateShellSettingsAsync(shellSettings);
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
if (ShellScope.Context.Settings.Name != ShellHelper.DefaultShellName)
{
return Outcomes("Failed");
}

var tenantName = (await ExpressionEvaluator.EvaluateAsync(TenantName, workflowContext))?.Trim();

if (tenantName == ShellHelper.DefaultShellName)
{
return Outcomes("Failed");
}

// return Outcomes("Disable");
//}
if (!ShellHost.TryGetSettings(tenantName?.Trim(), out var shellSettings))
{
return Outcomes("Failed");
}

if (shellSettings.State != TenantState.Running)
{
return Outcomes("Failed");
}

shellSettings.State = TenantState.Disabled;
await ShellHost.UpdateShellSettingsAsync(shellSettings);

return Outcomes("Disabled");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;
using OrchardCore.Workflows.Services;

namespace OrchardCore.Tenants.Workflows.Activities
{
public class EnableTenantTask : TenantTask
{
public EnableTenantTask(IShellSettingsManager shellSettingsManager, IShellHost shellHost, IWorkflowScriptEvaluator scriptEvaluator, IStringLocalizer<EnableTenantTask> localizer)
: base(shellSettingsManager, shellHost, scriptEvaluator, localizer)
public EnableTenantTask(IShellSettingsManager shellSettingsManager, IShellHost shellHost, IWorkflowExpressionEvaluator expressionEvaluator, IWorkflowScriptEvaluator scriptEvaluator, IStringLocalizer<EnableTenantTask> localizer)
: base(shellSettingsManager, shellHost, expressionEvaluator, scriptEvaluator, localizer)
{
}

Expand All @@ -22,7 +26,32 @@ public EnableTenantTask(IShellSettingsManager shellSettingsManager, IShellHost s

public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Enabled"]);
return Outcomes(S["Enabled"], S["Failed"]);
}

public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
if (ShellScope.Context.Settings.Name != ShellHelper.DefaultShellName)
{
return Outcomes("Failed");
}

var tenantName = (await ExpressionEvaluator.EvaluateAsync(TenantName, workflowContext))?.Trim();

if (!ShellHost.TryGetSettings(tenantName, out var shellSettings))
{
return Outcomes("Failed");
}

if (shellSettings.State != TenantState.Disabled)
{
return Outcomes("Failed");
}

shellSettings.State = TenantState.Running;
await ShellHost.UpdateShellSettingsAsync(shellSettings);

return Outcomes("Enabled");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Modules;
using OrchardCore.Setup.Services;
using OrchardCore.Workflows.Abstractions.Models;
Expand All @@ -22,7 +22,7 @@ public class SetupTenantTask : TenantTask
private readonly IWorkflowExpressionEvaluator _expressionEvaluator;

public SetupTenantTask(IShellSettingsManager shellSettingsManager, IShellHost shellHost, ISetupService setupService, IClock clock, IWorkflowExpressionEvaluator expressionEvaluator, IWorkflowScriptEvaluator scriptEvaluator, IUpdateModelAccessor updateModelAccessor, IStringLocalizer<SetupTenantTask> localizer)
: base(shellSettingsManager, shellHost, scriptEvaluator, localizer)
: base(shellSettingsManager, shellHost, expressionEvaluator, scriptEvaluator, localizer)
{
SetupService = setupService;
_clock = clock;
Expand Down Expand Up @@ -93,55 +93,80 @@ public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContex

public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
var tenantNameTask = _expressionEvaluator.EvaluateAsync(TenantName, workflowContext);
var siteNameTask = _expressionEvaluator.EvaluateAsync(SiteName, workflowContext);
var adminUsernameTask = _expressionEvaluator.EvaluateAsync(AdminUsername, workflowContext);
var adminEmailTask = _expressionEvaluator.EvaluateAsync(AdminEmail, workflowContext);
var adminPasswordTask = _expressionEvaluator.EvaluateAsync(AdminPassword, workflowContext);
var databaseProviderTask = _expressionEvaluator.EvaluateAsync(DatabaseProvider, workflowContext);
var databaseConnectionStringTask = _expressionEvaluator.EvaluateAsync(DatabaseConnectionString, workflowContext);
var databaseTablePrefixTask = _expressionEvaluator.EvaluateAsync(DatabaseTablePrefix, workflowContext);
var recipeNameTask = _expressionEvaluator.EvaluateAsync(RecipeName, workflowContext);

await Task.WhenAll(tenantNameTask, siteNameTask, adminUsernameTask, adminEmailTask, adminPasswordTask, databaseProviderTask, databaseConnectionStringTask, databaseTablePrefixTask, recipeNameTask);

if (!ShellHost.TryGetSettings(tenantNameTask.Result?.Trim(), out var shellSettings))
if (ShellScope.Context.Settings.Name != ShellHelper.DefaultShellName)
{
if (!string.IsNullOrWhiteSpace(tenantNameTask.Result))
{
shellSettings = new ShellSettings
{
Name = tenantNameTask.Result?.Trim(),
State = TenantState.Uninitialized
};
shellSettings["ConnectionString"] = databaseConnectionStringTask.Result?.Trim();
shellSettings["TablePrefix"] = databaseTablePrefixTask.Result?.Trim();
shellSettings["DatabaseProvider"] = databaseProviderTask.Result?.Trim();
shellSettings["Secret"] = Guid.NewGuid().ToString();
shellSettings["RecipeName"] = recipeNameTask.Result.Trim();
}
return Outcomes("Failed");
}

var tenantName = (await ExpressionEvaluator.EvaluateAsync(TenantName, workflowContext))?.Trim();

if (string.IsNullOrWhiteSpace(tenantName))
{
return Outcomes("Failed");
}

if (!ShellHost.TryGetSettings(tenantName, out var shellSettings))
{
return Outcomes("Failed");
}

if (shellSettings.State == TenantState.Running)
{
return Outcomes("Failed");
}

await ShellSettingsManager.SaveSettingsAsync(shellSettings);
var shellContext = await ShellHost.GetOrCreateShellContextAsync(shellSettings);
if (shellSettings.State != TenantState.Uninitialized)
{
return Outcomes("Failed");
}

var siteName = (await _expressionEvaluator.EvaluateAsync(SiteName, workflowContext))?.Trim();
var adminUsername = (await _expressionEvaluator.EvaluateAsync(AdminUsername, workflowContext))?.Trim();
var adminEmail = (await _expressionEvaluator.EvaluateAsync(AdminEmail, workflowContext))?.Trim();
var adminPassword = (await _expressionEvaluator.EvaluateAsync(AdminPassword, workflowContext))?.Trim();

var databaseProvider = (await _expressionEvaluator.EvaluateAsync(DatabaseProvider, workflowContext))?.Trim();
var databaseConnectionString = (await _expressionEvaluator.EvaluateAsync(DatabaseConnectionString, workflowContext))?.Trim();
var databaseTablePrefix = (await _expressionEvaluator.EvaluateAsync(DatabaseTablePrefix, workflowContext))?.Trim();
var recipeName = (await _expressionEvaluator.EvaluateAsync(RecipeName, workflowContext))?.Trim();

if (string.IsNullOrEmpty(databaseProvider))
{
databaseProvider = shellSettings["DatabaseProvider"];
}

if (string.IsNullOrEmpty(databaseConnectionString))
{
databaseConnectionString = shellSettings["ConnectionString"];
}

if (string.IsNullOrEmpty(databaseTablePrefix))
{
databaseTablePrefix = shellSettings["TablePrefix"];
}

if (string.IsNullOrEmpty(recipeName))
{
recipeName = shellSettings["RecipeName"];
}

var recipes = await SetupService.GetSetupRecipesAsync();
var recipe = recipes.FirstOrDefault(x => x.Name == shellSettings["RecipeName"]);
var recipe = recipes.FirstOrDefault(r => r.Name == recipeName);

var setupContext = new SetupContext
{
ShellSettings = shellSettings,
SiteName = siteNameTask.Result?.Trim(),
SiteName = siteName,
EnabledFeatures = null,
AdminUsername = adminUsernameTask.Result?.Trim(),
AdminEmail = adminEmailTask.Result?.Trim(),
AdminPassword = adminPasswordTask.Result?.Trim(),
AdminUsername = adminUsername,
AdminEmail = adminEmail,
AdminPassword = adminPassword,
Errors = new Dictionary<string, string>(),
Recipe = recipe,
SiteTimeZone = _clock.GetSystemTimeZone().TimeZoneId,
DatabaseProvider = databaseProviderTask.Result?.Trim(),
DatabaseConnectionString = databaseConnectionStringTask.Result?.Trim(),
DatabaseTablePrefix = databaseTablePrefixTask.Result?.Trim(),
DatabaseProvider = databaseProvider,
DatabaseConnectionString = databaseConnectionString,
DatabaseTablePrefix = databaseTablePrefix
};

var executionId = await SetupService.SetupAsync(setupContext);
Expand Down
Loading

0 comments on commit 88faa82

Please sign in to comment.