Adding a rough-cut owner editor

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4043456
This commit is contained in:
loudej
2009-12-08 06:13:08 +00:00
parent ab0fbe20e1
commit 5aae644c6d
15 changed files with 231 additions and 10 deletions

View File

@@ -29,12 +29,12 @@ namespace Orchard.Core.Common.Models {
set { Record.ModifiedUtc = value;}
}
IUser ICommonAspect.Owner {
public IUser Owner {
get { return _owner.Value; }
set { _owner.Value = value; }
}
IContent ICommonAspect.Container {
public IContent Container {
get { return _container.Value; }
set { _container.Value = value; }
}

View File

@@ -1 +1 @@
Name: Content
Name: Common

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Security.Permissions;
namespace Orchard.Core.Common {
public class Permissions : IPermissionProvider {
public static Permission ChangeOwner = new Permission { Name = "ChangeOwner", Description = "Change the owner of content items" };
public string PackageName {
get { return "Common"; }
}
public IEnumerable<Permission> GetPermissions() {
return new[] { ChangeOwner };
}
}
}

View File

@@ -1,9 +1,13 @@
using System;
using Orchard.Core.Common.Models;
using Orchard.Core.Common.Records;
using Orchard.Core.Common.ViewModels;
using Orchard.Data;
using Orchard.Localization;
using Orchard.Models;
using Orchard.Models.Aspects;
using Orchard.Models.Driver;
using Orchard.Models.ViewModels;
using Orchard.Security;
using Orchard.Services;
@@ -11,24 +15,37 @@ namespace Orchard.Core.Common.Providers {
public class CommonAspectProvider : ContentProvider {
private readonly IClock _clock;
private readonly IAuthenticationService _authenticationService;
private readonly IAuthorizationService _authorizationService;
private readonly IMembershipService _membershipService;
private readonly IContentManager _contentManager;
public CommonAspectProvider(
IRepository<CommonRecord> repository,
IClock clock,
IAuthenticationService authenticationService,
IAuthorizationService authorizationService,
IMembershipService membershipService,
IContentManager contentManager) {
_clock = clock;
_authenticationService = authenticationService;
_authorizationService = authorizationService;
_membershipService = membershipService;
_contentManager = contentManager;
T = NullLocalizer.Instance;
OnActivated<CommonAspect>(PropertySetHandlers);
OnCreating<CommonAspect>(DefaultTimestampsAndOwner);
OnLoaded<CommonAspect>(LazyLoadHandlers);
Filters.Add(new StorageFilter<CommonRecord>(repository));
//OnGetDisplayViewModel<CommonAspect>();
OnGetEditorViewModel<CommonAspect>(GetEditor);
OnUpdateEditorViewModel<CommonAspect>(UpdateEditor);
}
public Localizer T { get; set; }
void DefaultTimestampsAndOwner(CreateContentContext context, CommonAspect instance) {
// assign default create/modified dates
if (instance.Record.CreatedUtc == null) {
@@ -75,15 +92,39 @@ namespace Orchard.Core.Common.Providers {
}
protected override void UpdateEditorViewModel(UpdateEditorViewModelContext context) {
var part = context.ContentItem.As<CommonAspect>();
if (part == null)
private void GetEditor(GetEditorViewModelContext context, CommonAspect instance) {
var currentUser = _authenticationService.GetAuthenticatedUser();
if (!_authorizationService.CheckAccess(currentUser, Permissions.ChangeOwner)) {
return;
// this event is hooked so the modified timestamp is changed when an edit-post occurs.
// kind of a loose rule of thumb. may not be sufficient
part.Record.ModifiedUtc = _clock.UtcNow;
}
var viewModel = new OwnerEditorViewModel { Owner = instance.Owner.UserName };
context.AddEditor(new TemplateViewModel(viewModel, "CommonAspect") );
}
private void UpdateEditor(UpdateEditorViewModelContext context, CommonAspect instance) {
// this event is hooked so the modified timestamp is changed when an edit-post occurs.
// kind of a loose rule of thumb. may not be sufficient
instance.Record.ModifiedUtc = _clock.UtcNow;
var currentUser = _authenticationService.GetAuthenticatedUser();
if (!_authorizationService.CheckAccess(currentUser, Permissions.ChangeOwner)) {
return;
}
var viewModel = new OwnerEditorViewModel { Owner = instance.Owner.UserName };
context.Updater.TryUpdateModel(viewModel, "CommonAspect", null, null);
if (viewModel.Owner != instance.Owner.UserName) {
var newOwner = _membershipService.GetUser(viewModel.Owner);
if (newOwner == null) {
context.Updater.AddModelError("CommonAspect.Owner", T("Invalid user name"));
}
else {
instance.Owner = newOwner;
}
}
context.AddEditor(new TemplateViewModel(viewModel, "CommonAspect"));
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Orchard.Core.Common.ViewModels {
public class OwnerEditorViewModel {
[Required]
public string Owner { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<OwnerEditorViewModel>" %>
<%@ Import Namespace="Orchard.Core.Common.ViewModels" %>
<%@ Import Namespace="Orchard.Core.Common.Models" %>
<%@ Import Namespace="Orchard.Core.Settings.ViewModels" %>
<%@ Import Namespace="Orchard.Utility" %>
<fieldset>
<legend>System</legend>
<%=Html.LabelFor(m=>m.Owner) %>
<%=Html.EditorFor(m=>m.Owner) %>
<%=Html.ValidationMessageFor(m=>m.Owner) %>
</fieldset>

View File

@@ -61,6 +61,7 @@
<Reference Include="System.Web.Mobile" />
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Permissions.cs" />
<Compile Include="Common\Utilities\LazyField.cs" />
<Compile Include="Common\Providers\CommonAspectProvider.cs" />
<Compile Include="Common\Models\CommonAspect.cs" />
@@ -73,6 +74,7 @@
<Compile Include="Common\Records\RoutableRecord.cs" />
<Compile Include="Common\ViewModels\BodyDisplayViewModel.cs" />
<Compile Include="Common\ViewModels\BodyEditorViewModel.cs" />
<Compile Include="Common\ViewModels\OwnerEditorViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings\AdminMenu.cs" />
<Compile Include="Settings\Controllers\AdminController.cs" />
@@ -118,6 +120,7 @@
<Content Include="XmlRpc\Views\Web.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Common\Views\Models\EditorTemplates\OwnerEditorViewModel.ascx" />
<Content Include="Common\Views\Web.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@@ -49,6 +49,10 @@ namespace Orchard.Core.Settings.Controllers {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
#endregion
}
}

View File

@@ -150,5 +150,9 @@ namespace Orchard.Blogs.Controllers {
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
}
}

View File

@@ -192,5 +192,8 @@ namespace Orchard.Blogs.Controllers {
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
}
}

View File

@@ -106,5 +106,8 @@ namespace Orchard.Sandbox.Controllers {
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
}
}

View File

@@ -101,6 +101,9 @@ namespace Orchard.Users.Controllers {
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
}
}