2010-07-14 15:31:43 -07:00
|
|
|
|
using Orchard.ContentManagement;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
|
|
|
|
using Orchard.Core.Common.Models;
|
|
|
|
|
using Orchard.Core.Common.ViewModels;
|
2010-07-21 17:17:13 -07:00
|
|
|
|
using Orchard.Core.ContentsLocation.Models;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.Security;
|
|
|
|
|
using Orchard.Services;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Common.Drivers {
|
2010-07-22 12:52:16 -07:00
|
|
|
|
public class CommonPartDriver : ContentPartDriver<CommonPart> {
|
|
|
|
|
private const string TemplatePrefix = "CommonPart";
|
2010-06-09 15:18:12 -07:00
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
private readonly IAuthenticationService _authenticationService;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
private readonly IMembershipService _membershipService;
|
|
|
|
|
private readonly IClock _clock;
|
|
|
|
|
|
2010-07-22 12:52:16 -07:00
|
|
|
|
public CommonPartDriver(
|
2010-07-13 10:14:46 -07:00
|
|
|
|
IOrchardServices services,
|
2010-06-09 15:18:12 -07:00
|
|
|
|
IContentManager contentManager,
|
|
|
|
|
IAuthenticationService authenticationService,
|
|
|
|
|
IAuthorizationService authorizationService,
|
|
|
|
|
IMembershipService membershipService,
|
|
|
|
|
IClock clock) {
|
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
_authenticationService = authenticationService;
|
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
|
_membershipService = membershipService;
|
|
|
|
|
_clock = clock;
|
|
|
|
|
T = NullLocalizer.Instance;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
Services = services;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Localizer T { get; set; }
|
2010-07-13 10:14:46 -07:00
|
|
|
|
public IOrchardServices Services { get; set; }
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Display(CommonPart part, string displayType, dynamic shapeHelper) {
|
2010-10-13 04:51:15 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
ContentShape("Parts_Common_Metadata",
|
|
|
|
|
() => shapeHelper.Parts_Common_Metadata(ContentPart: part)),
|
|
|
|
|
ContentShape("Parts_Common_Metadata_Summary",
|
|
|
|
|
() => shapeHelper.Parts_Common_Metadata_Summary(ContentPart: part)),
|
|
|
|
|
ContentShape("Parts_Common_Metadata_SummaryAdmin",
|
|
|
|
|
() => shapeHelper.Parts_Common_Metadata_SummaryAdmin(ContentPart: part))
|
|
|
|
|
);
|
2010-07-08 15:47:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Editor(CommonPart part, dynamic shapeHelper) {
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
OwnerEditor(part, null),
|
2010-07-14 15:31:43 -07:00
|
|
|
|
ContainerEditor(part, null));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Editor(CommonPart instance, IUpdateModel updater, dynamic shapeHelper) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
// this event is hooked so the modified timestamp is changed when an edit-post occurs.
|
|
|
|
|
instance.ModifiedUtc = _clock.UtcNow;
|
|
|
|
|
instance.VersionModifiedUtc = _clock.UtcNow;
|
|
|
|
|
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
OwnerEditor(instance, updater),
|
2010-07-14 15:31:43 -07:00
|
|
|
|
ContainerEditor(instance, updater));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-22 12:52:16 -07:00
|
|
|
|
DriverResult OwnerEditor(CommonPart part, IUpdateModel updater) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
|
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.ChangeOwner, currentUser, part)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var model = new OwnerEditorViewModel();
|
|
|
|
|
if (part.Owner != null)
|
|
|
|
|
model.Owner = part.Owner.UserName;
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
var priorOwner = model.Owner;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
updater.TryUpdateModel(model, TemplatePrefix, null, null);
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
|
|
|
|
if (model.Owner != null && model.Owner != priorOwner) {
|
|
|
|
|
var newOwner = _membershipService.GetUser(model.Owner);
|
|
|
|
|
if (newOwner == null) {
|
2010-07-22 12:52:16 -07:00
|
|
|
|
updater.AddModelError("CommonPart.Owner", T("Invalid user name"));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
part.Owner = newOwner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-21 21:32:13 -07:00
|
|
|
|
return ContentPartTemplate(model, "Parts/Common.Owner", TemplatePrefix).Location(part.GetLocation("Editor"));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-22 12:52:16 -07:00
|
|
|
|
DriverResult ContainerEditor(CommonPart part, IUpdateModel updater) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
|
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.ChangeOwner, currentUser, part)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var model = new ContainerEditorViewModel();
|
|
|
|
|
if (part.Container != null)
|
|
|
|
|
model.ContainerId = part.Container.ContentItem.Id;
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
var priorContainerId = model.ContainerId;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
updater.TryUpdateModel(model, TemplatePrefix, null, null);
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
|
|
|
|
if (model.ContainerId != null && model.ContainerId != priorContainerId) {
|
|
|
|
|
var newContainer = _contentManager.Get((int)model.ContainerId, VersionOptions.Latest);
|
|
|
|
|
if (newContainer == null) {
|
2010-07-22 12:52:16 -07:00
|
|
|
|
updater.AddModelError("CommonPart.ContainerId", T("Invalid container"));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
part.Container = newContainer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-21 17:17:13 -07:00
|
|
|
|
|
2010-07-21 21:32:13 -07:00
|
|
|
|
return ContentPartTemplate(model, "Parts/Common.Container", TemplatePrefix).Location(part.GetLocation("Editor"));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|