- Migrating Roles to the new shape API.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-10-15 15:55:47 -07:00
parent dd01e51869
commit e8f6150f44
3 changed files with 28 additions and 24 deletions

View File

@@ -18,6 +18,7 @@ namespace Orchard.Roles.Drivers {
private readonly INotifier _notifier; private readonly INotifier _notifier;
private readonly IAuthenticationService _authenticationService; private readonly IAuthenticationService _authenticationService;
private readonly IAuthorizationService _authorizationService; private readonly IAuthorizationService _authorizationService;
private const string TemplateName = "Parts/Roles.UserRoles";
public UserRolesPartDriver( public UserRolesPartDriver(
IRepository<UserRolesPartRecord> userRolesRepository, IRepository<UserRolesPartRecord> userRolesRepository,
@@ -46,20 +47,19 @@ namespace Orchard.Roles.Drivers {
if (!_authorizationService.TryCheckAccess(Permissions.ApplyRoles, _authenticationService.GetAuthenticatedUser(), userRolesPart)) if (!_authorizationService.TryCheckAccess(Permissions.ApplyRoles, _authenticationService.GetAuthenticatedUser(), userRolesPart))
return null; return null;
var roles = return ContentShape("Parts_Roles_UserRoles_Edit",
_roleService.GetRoles().Select( () => {
x => new UserRoleEntry { var roles =_roleService.GetRoles().Select(x => new UserRoleEntry {
RoleId = x.Id, RoleId = x.Id,
Name = x.Name, Name = x.Name,
Granted = userRolesPart.Roles.Contains(x.Name) Granted = userRolesPart.Roles.Contains(x.Name)});
}); var model = new UserRolesViewModel {
User = userRolesPart.As<IUser>(),
var model = new UserRolesViewModel { UserRoles = userRolesPart,
User = userRolesPart.As<IUser>(), Roles = roles.ToList(),
UserRoles = userRolesPart, };
Roles = roles.ToList(), return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix);
}; });
return ContentPartTemplate(model, "Parts/Roles.UserRoles");
} }
protected override DriverResult Editor(UserRolesPart userRolesPart, IUpdateModel updater, dynamic shapeHelper) { protected override DriverResult Editor(UserRolesPart userRolesPart, IUpdateModel updater, dynamic shapeHelper) {
@@ -67,29 +67,26 @@ namespace Orchard.Roles.Drivers {
if (!_authorizationService.TryCheckAccess(Permissions.ApplyRoles, _authenticationService.GetAuthenticatedUser(), userRolesPart)) if (!_authorizationService.TryCheckAccess(Permissions.ApplyRoles, _authenticationService.GetAuthenticatedUser(), userRolesPart))
return null; return null;
var model = new UserRolesViewModel { var model = BuildEditorViewModel(userRolesPart);
User = userRolesPart.As<IUser>(),
UserRoles = userRolesPart,
};
if (updater.TryUpdateModel(model, Prefix, null, null)) { if (updater.TryUpdateModel(model, Prefix, null, null)) {
var currentUserRoleRecords = _userRolesRepository.Fetch(x => x.UserId == model.User.Id); var currentUserRoleRecords = _userRolesRepository.Fetch(x => x.UserId == model.User.Id);
var currentRoleRecords = currentUserRoleRecords.Select(x => x.Role); var currentRoleRecords = currentUserRoleRecords.Select(x => x.Role);
var targetRoleRecords = model.Roles.Where(x => x.Granted).Select(x => _roleService.GetRole(x.RoleId)); var targetRoleRecords = model.Roles.Where(x => x.Granted).Select(x => _roleService.GetRole(x.RoleId));
foreach (var addingRole in targetRoleRecords.Where(x => !currentRoleRecords.Contains(x))) { foreach (var addingRole in targetRoleRecords.Where(x => !currentRoleRecords.Contains(x))) {
_notifier.Warning(T("Adding role {0} to user {1}", addingRole.Name, userRolesPart.As<IUser>().UserName)); _notifier.Warning(T("Adding role {0} to user {1}", addingRole.Name, userRolesPart.As<IUser>().UserName));
_userRolesRepository.Create(new UserRolesPartRecord { UserId = model.User.Id, Role = addingRole }); _userRolesRepository.Create(new UserRolesPartRecord { UserId = model.User.Id, Role = addingRole });
} }
foreach (var removingRole in currentUserRoleRecords.Where(x => !targetRoleRecords.Contains(x.Role))) { foreach (var removingRole in currentUserRoleRecords.Where(x => !targetRoleRecords.Contains(x.Role))) {
_notifier.Warning(T("Removing role {0} from user {1}", removingRole.Role.Name, userRolesPart.As<IUser>().UserName)); _notifier.Warning(T("Removing role {0} from user {1}", removingRole.Role.Name, userRolesPart.As<IUser>().UserName));
_userRolesRepository.Delete(removingRole); _userRolesRepository.Delete(removingRole);
} }
} }
return ContentPartTemplate(model, "Parts/Roles.UserRoles"); return ContentShape("Parts_Roles_UserRoles_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
private static UserRolesViewModel BuildEditorViewModel(UserRolesPart userRolesPart) {
return new UserRolesViewModel { User = userRolesPart.As<IUser>(), UserRoles = userRolesPart };
} }
} }
} }

View File

@@ -39,6 +39,7 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.ComponentModel.DataAnnotations"> <Reference Include="System.ComponentModel.DataAnnotations">
@@ -108,6 +109,9 @@
<Name>Orchard.Core</Name> <Name>Orchard.Core</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="Placement.info" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,3 @@
<Placement>
<Place Parts_Roles_UserRoles_Edit="Primary:10"/>
</Placement>