From df483c3a8e1fc9ead290d670d7f321674342c933 Mon Sep 17 00:00:00 2001 From: Bing Huan Chio Date: Wed, 22 Apr 2015 12:22:00 -0700 Subject: [PATCH] Adding modified by to common part --- .../Core/Common/Handlers/CommonPartHandler.cs | 36 +++++++++++-------- src/Orchard.Web/Core/Common/Migrations.cs | 16 +++++---- .../Core/Common/Models/CommonPart.cs | 9 +++++ .../Common/Models/CommonPartVersionRecord.cs | 1 + 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs b/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs index e0e0c525c..cfbc5deba 100644 --- a/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs +++ b/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs @@ -75,7 +75,7 @@ namespace Orchard.Core.Common.Handlers { protected bool ContentTypeWithACommonPart(string typeName) { //Note: What about content type handlers which activate "CommonPart" in code? var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(typeName); - + if (contentTypeDefinition != null) return contentTypeDefinition.Parts.Any(part => part.PartDefinition.Name == "CommonPart"); @@ -97,6 +97,7 @@ namespace Orchard.Core.Common.Handlers { part.ModifiedUtc = utcNow; part.VersionCreatedUtc = utcNow; part.VersionModifiedUtc = utcNow; + part.VersionModifiedBy = GetUserName(); } private void AssignUpdateDates(UpdateEditorContext context, CommonPart part) { @@ -104,6 +105,7 @@ namespace Orchard.Core.Common.Handlers { part.ModifiedUtc = utcNow; part.VersionModifiedUtc = utcNow; + part.VersionModifiedBy = GetUserName(); } private void AssignRemovingDates(RemoveContentContext context, CommonPart part) { @@ -111,15 +113,17 @@ namespace Orchard.Core.Common.Handlers { part.ModifiedUtc = utcNow; part.VersionModifiedUtc = utcNow; + part.VersionModifiedBy = GetUserName(); } protected void AssignVersioningDates(VersionContentContext context, CommonPart existing, CommonPart building) { var utcNow = _clock.UtcNow; - // assign the created date + // assign the created date building.VersionCreatedUtc = utcNow; // assign modified date for the new version building.VersionModifiedUtc = utcNow; + building.VersionModifiedBy = GetUserName(); // publish date should be null until publish method called building.VersionPublishedUtc = null; @@ -134,7 +138,7 @@ namespace Orchard.Core.Common.Handlers { protected void AssignPublishingDates(PublishContentContext context, CommonPart part) { var utcNow = _clock.UtcNow; - + part.PublishedUtc = utcNow; part.VersionPublishedUtc = utcNow; } @@ -142,33 +146,37 @@ namespace Orchard.Core.Common.Handlers { protected void LazyLoadHandlers(CommonPart part) { // add handlers that will load content for id's just-in-time part.OwnerField.Loader(() => _contentManager.Get(part.Record.OwnerId)); - part.ContainerField.Loader(() => part.Record.Container == null ? null : _contentManager.Get(part.Record.Container.Id)); + part.ContainerField.Loader(() => part.Record.Container == null ? null : _contentManager.Get(part.Record.Container.Id)); } protected static void PropertySetHandlers(ActivatedContentContext context, CommonPart part) { // add handlers that will update records when part properties are set part.OwnerField.Setter(user => { - part.Record.OwnerId = user == null - ? 0 - : user.ContentItem.Id; - return user; - }); + part.Record.OwnerId = user == null + ? 0 + : user.ContentItem.Id; + return user; + }); // Force call to setter if we had already set a value if (part.OwnerField.Value != null) part.OwnerField.Value = part.OwnerField.Value; part.ContainerField.Setter(container => { - part.Record.Container = container == null - ? null - : container.ContentItem.Record; - return container; - }); + part.Record.Container = container == null + ? null + : container.ContentItem.Record; + return container; + }); // Force call to setter if we had already set a value if (part.ContainerField.Value != null) part.ContainerField.Value = part.ContainerField.Value; } + private string GetUserName() { + var user = _authenticationService.GetAuthenticatedUser(); + return (user == null) ? string.Empty : user.UserName; + } } } diff --git a/src/Orchard.Web/Core/Common/Migrations.cs b/src/Orchard.Web/Core/Common/Migrations.cs index b564055ca..c386aeded 100644 --- a/src/Orchard.Web/Core/Common/Migrations.cs +++ b/src/Orchard.Web/Core/Common/Migrations.cs @@ -15,14 +15,14 @@ namespace Orchard.Core.Common { } public int Create() { - SchemaBuilder.CreateTable("BodyPartRecord", + SchemaBuilder.CreateTable("BodyPartRecord", table => table .ContentPartVersionRecord() .Column("Text", column => column.Unlimited()) .Column("Format") ); - SchemaBuilder.CreateTable("CommonPartRecord", + SchemaBuilder.CreateTable("CommonPartRecord", table => table .ContentPartRecord() .Column("OwnerId") @@ -31,8 +31,8 @@ namespace Orchard.Core.Common { .Column("ModifiedUtc") .Column("Container_id") ); - - SchemaBuilder.CreateTable("CommonPartVersionRecord", + + SchemaBuilder.CreateTable("CommonPartVersionRecord", table => table .ContentPartVersionRecord() .Column("CreatedUtc") @@ -99,13 +99,17 @@ namespace Orchard.Core.Common { foreach (var existingIdentityPart in existingIdentityParts) { var updateIdentityPartRecord = _identityPartRepository.Get(existingIdentityPart.Id); - + updateIdentityPartRecord.Identifier = existingIdentityPart.Identifier; - + _identityPartRepository.Update(updateIdentityPartRecord); } return 4; } + public int UpdateFrom4() { + SchemaBuilder.AlterTable("CommonPartVersionRecord", table => table.AddColumn("ModifiedBy", command => command.Nullable())); + return 5; + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Models/CommonPart.cs b/src/Orchard.Web/Core/Common/Models/CommonPart.cs index 23fc22f4e..188f6afd0 100644 --- a/src/Orchard.Web/Core/Common/Models/CommonPart.cs +++ b/src/Orchard.Web/Core/Common/Models/CommonPart.cs @@ -54,6 +54,15 @@ namespace Orchard.Core.Common.Models { PartVersionRecord.CreatedUtc = value; } } + public string VersionModifiedBy { + get { + return PartVersionRecord == null ? null : PartVersionRecord.ModifiedBy; + } + set { + if (PartVersionRecord != null) + PartVersionRecord.ModifiedBy = value; + } + } public DateTime? VersionPublishedUtc { get { diff --git a/src/Orchard.Web/Core/Common/Models/CommonPartVersionRecord.cs b/src/Orchard.Web/Core/Common/Models/CommonPartVersionRecord.cs index 379392948..b62bf4888 100644 --- a/src/Orchard.Web/Core/Common/Models/CommonPartVersionRecord.cs +++ b/src/Orchard.Web/Core/Common/Models/CommonPartVersionRecord.cs @@ -6,5 +6,6 @@ namespace Orchard.Core.Common.Models { public virtual DateTime? CreatedUtc { get; set; } public virtual DateTime? PublishedUtc { get; set; } public virtual DateTime? ModifiedUtc { get; set; } + public virtual string ModifiedBy { get; set; } } } \ No newline at end of file