mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactored Roles import/export step.
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Orchard.Events;
|
|
||||||
|
|
||||||
namespace Orchard.Roles.ImportExport {
|
|
||||||
public interface ICustomExportStep : IEventHandler {
|
|
||||||
void Register(IList<string> steps);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class RolesCustomExportStep : ICustomExportStep {
|
|
||||||
public void Register(IList<string> steps) {
|
|
||||||
steps.Add("Roles");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Xml.Linq;
|
|
||||||
using Orchard.Data;
|
|
||||||
using Orchard.Events;
|
|
||||||
using Orchard.Roles.Models;
|
|
||||||
|
|
||||||
namespace Orchard.Roles.ImportExport {
|
|
||||||
public interface IExportEventHandler : IEventHandler {
|
|
||||||
void Exporting(dynamic context);
|
|
||||||
void Exported(dynamic context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class RolesExportEventHandler : IExportEventHandler {
|
|
||||||
private readonly IRepository<RoleRecord> _roleRecordepository;
|
|
||||||
|
|
||||||
public RolesExportEventHandler(IRepository<RoleRecord> roleRecordRepository) {
|
|
||||||
_roleRecordepository = roleRecordRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Exporting(dynamic context) {}
|
|
||||||
|
|
||||||
public void Exported(dynamic context) {
|
|
||||||
|
|
||||||
if (!((IEnumerable<string>) context.ExportOptions.CustomSteps).Contains("Roles")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var roles = _roleRecordepository.Table.ToList();
|
|
||||||
|
|
||||||
if (!roles.Any()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var root = new XElement("Roles");
|
|
||||||
context.Document.Element("Orchard").Add(root);
|
|
||||||
|
|
||||||
foreach (var role in roles.OrderBy(x => x.Name)) {
|
|
||||||
root.Add(new XElement("Role",
|
|
||||||
new XAttribute("Name", role.Name),
|
|
||||||
new XAttribute("Permissions", string.Join(",", role.RolesPermissions.Select(rolePermission => rolePermission.Permission.Name)))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Orchard.Data;
|
|
||||||
using Orchard.Localization;
|
|
||||||
using Orchard.Logging;
|
|
||||||
using Orchard.Recipes.Models;
|
|
||||||
using Orchard.Recipes.Services;
|
|
||||||
using Orchard.Roles.Models;
|
|
||||||
using Orchard.Roles.Services;
|
|
||||||
|
|
||||||
namespace Orchard.Roles.ImportExport {
|
|
||||||
public class RolesRecipeHandler : IRecipeHandler {
|
|
||||||
private readonly IRoleService _roleService;
|
|
||||||
private readonly IRepository<RoleRecord> _roleRecordRepository;
|
|
||||||
private readonly IRepository<PermissionRecord> _permissionRepository;
|
|
||||||
|
|
||||||
public RolesRecipeHandler(IRoleService roleService,
|
|
||||||
IRepository<RoleRecord> roleRecordRepository,
|
|
||||||
IRepository<PermissionRecord> permissionRepository) {
|
|
||||||
_roleService = roleService;
|
|
||||||
_roleRecordRepository = roleRecordRepository;
|
|
||||||
_permissionRepository = permissionRepository;
|
|
||||||
Logger = NullLogger.Instance;
|
|
||||||
T = NullLocalizer.Instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Localizer T { get; set; }
|
|
||||||
public ILogger Logger { get; set; }
|
|
||||||
|
|
||||||
public void ExecuteRecipeStep(RecipeContext recipeContext) {
|
|
||||||
if (!String.Equals(recipeContext.RecipeStep.Name, "Roles", StringComparison.OrdinalIgnoreCase)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.Information("Executing recipe step '{0}'; ExecutionId={1}", recipeContext.RecipeStep.Name, recipeContext.ExecutionId);
|
|
||||||
|
|
||||||
var installedPermissions = _roleService.GetInstalledPermissions().SelectMany(p => p.Value).ToList();
|
|
||||||
|
|
||||||
foreach (var roleElement in recipeContext.RecipeStep.Step.Elements()) {
|
|
||||||
var roleName = roleElement.Attribute("Name").Value;
|
|
||||||
|
|
||||||
Logger.Information("Processing role '{0}'.", roleName);
|
|
||||||
|
|
||||||
try {
|
|
||||||
var role = _roleService.GetRoleByName(roleName);
|
|
||||||
if (role == null) {
|
|
||||||
_roleService.CreateRole(roleName);
|
|
||||||
role = _roleService.GetRoleByName(roleName);
|
|
||||||
}
|
|
||||||
|
|
||||||
var permissions = roleElement.Attribute("Permissions").Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
// only import permissions for currenlty installed modules
|
|
||||||
var permissionsValid = permissions.Where(permission => installedPermissions.Any(x => x.Name == permission)).ToList();
|
|
||||||
|
|
||||||
// union to keep existing permissions
|
|
||||||
_roleService.UpdateRole(role.Id, role.Name, permissionsValid.Union(role.RolesPermissions.Select(p => p.Permission.Name)));
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
Logger.Error(ex, "Error while processing role '{0}'.", roleName);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
recipeContext.Executed = true;
|
|
||||||
Logger.Information("Finished executing recipe step '{0}'.", recipeContext.RecipeStep.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
<IISExpressAnonymousAuthentication />
|
<IISExpressAnonymousAuthentication />
|
||||||
<IISExpressWindowsAuthentication />
|
<IISExpressWindowsAuthentication />
|
||||||
<IISExpressUseClassicPipelineMode />
|
<IISExpressUseClassicPipelineMode />
|
||||||
|
<UseGlobalApplicationHostFile />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@@ -85,9 +86,8 @@
|
|||||||
<Compile Include="Events\UserRoleContext.cs" />
|
<Compile Include="Events\UserRoleContext.cs" />
|
||||||
<Compile Include="Forms\SelectRolesForms.cs" />
|
<Compile Include="Forms\SelectRolesForms.cs" />
|
||||||
<Compile Include="Forms\UserTaskForms.cs" />
|
<Compile Include="Forms\UserTaskForms.cs" />
|
||||||
<Compile Include="ImportExport\RolesCustomExportStep.cs" />
|
<Compile Include="Recipes\Builders\RolesStep.cs" />
|
||||||
<Compile Include="ImportExport\RolesExportEventHandler.cs" />
|
<Compile Include="Recipes\Executors\RolesStep.cs" />
|
||||||
<Compile Include="ImportExport\RolesRecipeHandler.cs" />
|
|
||||||
<Compile Include="Migrations.cs" />
|
<Compile Include="Migrations.cs" />
|
||||||
<Compile Include="Drivers\UserRolesPartDriver.cs" />
|
<Compile Include="Drivers\UserRolesPartDriver.cs" />
|
||||||
<Compile Include="DefaultRoleUpdater.cs" />
|
<Compile Include="DefaultRoleUpdater.cs" />
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using Orchard.Data;
|
||||||
|
using Orchard.Localization;
|
||||||
|
using Orchard.Recipes.Services;
|
||||||
|
using Orchard.Roles.Models;
|
||||||
|
|
||||||
|
namespace Orchard.Roles.Recipes.Builders {
|
||||||
|
public class RolesStep : RecipeBuilderStep {
|
||||||
|
private readonly IRepository<RoleRecord> _roleRecordepository;
|
||||||
|
|
||||||
|
public RolesStep(IRepository<RoleRecord> roleRecordRepository) {
|
||||||
|
_roleRecordepository = roleRecordRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Name {
|
||||||
|
get { return "Roles"; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override LocalizedString DisplayName {
|
||||||
|
get { return T("Roles"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override LocalizedString Description {
|
||||||
|
get { return T("Exports the user roles."); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Build(BuildContext context) {
|
||||||
|
var roles = _roleRecordepository.Table.ToList();
|
||||||
|
|
||||||
|
if (!roles.Any())
|
||||||
|
return;
|
||||||
|
|
||||||
|
var root = new XElement("Roles");
|
||||||
|
context.RecipeDocument.Element("Orchard").Add(root);
|
||||||
|
|
||||||
|
foreach (var role in roles.OrderBy(x => x.Name)) {
|
||||||
|
root.Add(
|
||||||
|
new XElement("Role",
|
||||||
|
new XAttribute("Name", role.Name),
|
||||||
|
new XAttribute("Permissions", string.Join(",", role.RolesPermissions.Select(rolePermission => rolePermission.Permission.Name)))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Orchard.Logging;
|
||||||
|
using Orchard.Recipes.Models;
|
||||||
|
using Orchard.Recipes.Services;
|
||||||
|
using Orchard.Roles.Services;
|
||||||
|
|
||||||
|
namespace Orchard.Roles.Recipes.Executors {
|
||||||
|
public class RolesStep : RecipeExecutionStep {
|
||||||
|
private readonly IRoleService _roleService;
|
||||||
|
|
||||||
|
public RolesStep(IRoleService roleService) {
|
||||||
|
_roleService = roleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Name {
|
||||||
|
get { return "Roles"; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Execute(RecipeExecutionContext context) {
|
||||||
|
var installedPermissions = _roleService.GetInstalledPermissions().SelectMany(p => p.Value).ToList();
|
||||||
|
|
||||||
|
foreach (var roleElement in context.RecipeStep.Step.Elements()) {
|
||||||
|
var roleName = roleElement.Attribute("Name").Value;
|
||||||
|
|
||||||
|
Logger.Information("Processing role '{0}'.", roleName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
var role = _roleService.GetRoleByName(roleName);
|
||||||
|
if (role == null) {
|
||||||
|
_roleService.CreateRole(roleName);
|
||||||
|
role = _roleService.GetRoleByName(roleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
var permissions = roleElement.Attribute("Permissions").Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
// Only import permissions for currenlty installed modules.
|
||||||
|
var permissionsValid = permissions.Where(permission => installedPermissions.Any(x => x.Name == permission)).ToList();
|
||||||
|
|
||||||
|
// Union to keep existing permissions.
|
||||||
|
_roleService.UpdateRole(role.Id, role.Name, permissionsValid.Union(role.RolesPermissions.Select(p => p.Permission.Name)));
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
Logger.Error(ex, "Error while processing role '{0}'.", roleName);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user