Adding import/export for user roles

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-06-08 15:51:17 -07:00
parent c2464a42c7
commit 781e5a692f
2 changed files with 35 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement.Drivers;
using Orchard.Data;
@@ -88,5 +89,35 @@ namespace Orchard.Roles.Drivers {
private static UserRolesViewModel BuildEditorViewModel(UserRolesPart userRolesPart) {
return new UserRolesViewModel { User = userRolesPart.As<IUser>(), UserRoles = userRolesPart };
}
protected override void Importing(UserRolesPart part, ContentManagement.Handlers.ImportContentContext context) {
var roles = context.Attribute(part.PartDefinition.Name, "Roles");
if(string.IsNullOrEmpty(roles)) {
return;
}
var userRoles = roles.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
// create new roles
foreach (var role in userRoles) {
var roleRecord = _roleService.GetRoleByName(role);
// create the role if it doesn't already exist
if (roleRecord == null) {
_roleService.CreateRole(role);
}
}
var currentUserRoleRecords = _userRolesRepository.Fetch(x => x.UserId == part.ContentItem.Id).ToList();
var currentRoleRecords = currentUserRoleRecords.Select(x => x.Role).ToList();
var targetRoleRecords = userRoles.Select(x => _roleService.GetRoleByName(x)).ToList();
foreach (var addingRole in targetRoleRecords.Where(x => !currentRoleRecords.Contains(x))) {
_userRolesRepository.Create(new UserRolesPartRecord { UserId = part.ContentItem.Id, Role = addingRole });
}
}
protected override void Exporting(UserRolesPart part, ContentManagement.Handlers.ExportContentContext context) {
context.Element(part.PartDefinition.Name).SetAttributeValue("Roles", string.Join(",", part.Roles));
}
}
}

View File

@@ -18,6 +18,7 @@
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation />
<TargetFrameworkProfile />
<UseIISExpress>false</UseIISExpress>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -47,6 +48,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="AdminMenu.cs" />