mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
@@ -0,0 +1,14 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
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) {
|
||||
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,57 @@
|
||||
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;
|
||||
}
|
||||
|
||||
var installedPermissions = _roleService.GetInstalledPermissions().SelectMany(p => p.Value).ToList();
|
||||
|
||||
foreach (var roleElement in recipeContext.RecipeStep.Step.Elements()) {
|
||||
var roleName = roleElement.Attribute("Name").Value;
|
||||
|
||||
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)));
|
||||
}
|
||||
recipeContext.Executed = true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -56,6 +56,9 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="ImportExport\RolesCustomExportStep.cs" />
|
||||
<Compile Include="ImportExport\RolesExportEventHandler.cs" />
|
||||
<Compile Include="ImportExport\RolesRecipeHandler.cs" />
|
||||
<Compile Include="Migrations.cs" />
|
||||
<Compile Include="Drivers\UserRolesPartDriver.cs" />
|
||||
<Compile Include="DefaultRoleUpdater.cs" />
|
||||
|
Reference in New Issue
Block a user