mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Merge pull request #5192 from fakedevil/feature/modified-by
Adding modified by to common part
This commit is contained in:
@@ -138,6 +138,7 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
Filters.Add(new ActivatingFilter<CommonPart>("test-item"));
|
||||
Filters.Add(new ActivatingFilter<ContentPart<CommonPartVersionRecord>>("test-item"));
|
||||
Filters.Add(new ActivatingFilter<TestUser>("User"));
|
||||
Filters.Add(new ActivatingFilter<AlternateTestUser>("AlternateUser"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,6 +147,11 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
public string UserName { get { return "x"; } }
|
||||
public string Email { get { return "y"; } }
|
||||
}
|
||||
class AlternateTestUser : ContentPart, IUser {
|
||||
public new int Id { get { return 6655322; } }
|
||||
public string UserName { get { return "y"; } }
|
||||
public string Email { get { return "x"; } }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OwnerShouldBeNullAndZeroByDefault() {
|
||||
@@ -359,14 +365,22 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
public void EditingShouldSetModifiedUtc() {
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
var user = contentManager.New<IUser>("User");
|
||||
_authn.Setup(x => x.GetAuthenticatedUser()).Returns(user);
|
||||
|
||||
var createUtc = _clock.UtcNow;
|
||||
var item = contentManager.Create<ICommonPart>("test-item", VersionOptions.Draft, init => { });
|
||||
contentManager.Publish(item.ContentItem);
|
||||
|
||||
Assert.That(item.CreatedUtc, Is.EqualTo(createUtc));
|
||||
Assert.That(item.ModifiedUtc, Is.EqualTo(createUtc));
|
||||
Assert.That(item.VersionModifiedBy, Is.EqualTo(user.UserName));
|
||||
Assert.That(item.PublishedUtc, Is.EqualTo(createUtc));
|
||||
|
||||
// Switch user
|
||||
var secondUser = contentManager.New<IUser>("AlternateUser");
|
||||
_authn.Setup(x => x.GetAuthenticatedUser()).Returns(secondUser);
|
||||
|
||||
_clock.Advance(TimeSpan.FromMinutes(1));
|
||||
var editUtc = _clock.UtcNow;
|
||||
|
||||
@@ -377,6 +391,7 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
Assert.That(item.ModifiedUtc, Is.EqualTo(editUtc));
|
||||
Assert.That(item.PublishedUtc, Is.EqualTo(createUtc));
|
||||
Assert.That(updater.ModelErrors.Count, Is.EqualTo(0));
|
||||
Assert.That(item.VersionModifiedBy, Is.EqualTo(secondUser.UserName));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -127,12 +127,12 @@
|
||||
<ProjectReference Include="..\Orchard.Web\Core\Orchard.Core.csproj">
|
||||
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
|
||||
<Name>Orchard.Core</Name>
|
||||
<Private>false</Private>
|
||||
<Private>True</Private>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Orchard\Orchard.Framework.csproj">
|
||||
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
|
||||
<Name>Orchard.Framework</Name>
|
||||
<Private>false</Private>
|
||||
<Private>True</Private>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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<IUser>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,14 +15,14 @@ namespace Orchard.Core.Common {
|
||||
}
|
||||
|
||||
public int Create() {
|
||||
SchemaBuilder.CreateTable("BodyPartRecord",
|
||||
SchemaBuilder.CreateTable("BodyPartRecord",
|
||||
table => table
|
||||
.ContentPartVersionRecord()
|
||||
.Column<string>("Text", column => column.Unlimited())
|
||||
.Column<string>("Format")
|
||||
);
|
||||
|
||||
SchemaBuilder.CreateTable("CommonPartRecord",
|
||||
SchemaBuilder.CreateTable("CommonPartRecord",
|
||||
table => table
|
||||
.ContentPartRecord()
|
||||
.Column<int>("OwnerId")
|
||||
@@ -31,13 +31,14 @@ namespace Orchard.Core.Common {
|
||||
.Column<DateTime>("ModifiedUtc")
|
||||
.Column<int>("Container_id")
|
||||
);
|
||||
|
||||
SchemaBuilder.CreateTable("CommonPartVersionRecord",
|
||||
|
||||
SchemaBuilder.CreateTable("CommonPartVersionRecord",
|
||||
table => table
|
||||
.ContentPartVersionRecord()
|
||||
.Column<DateTime>("CreatedUtc")
|
||||
.Column<DateTime>("PublishedUtc")
|
||||
.Column<DateTime>("ModifiedUtc")
|
||||
.Column<string>("ModifiedBy")
|
||||
);
|
||||
|
||||
SchemaBuilder.CreateTable("IdentityPartRecord",
|
||||
@@ -58,7 +59,7 @@ namespace Orchard.Core.Common {
|
||||
.Attachable()
|
||||
.WithDescription("Automatically generates a unique identity for the content item, which is required in import/export scenarios where one content item references another."));
|
||||
|
||||
return 4;
|
||||
return 5;
|
||||
}
|
||||
|
||||
public int UpdateFrom1() {
|
||||
@@ -99,13 +100,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<string>("ModifiedBy", command => command.Nullable()));
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
@*
|
||||
Model:
|
||||
ContentPart
|
||||
*@
|
||||
|
||||
@using Orchard.ContentManagement
|
||||
@using Orchard.Core.Common.Models;
|
||||
@{
|
||||
CommonPart commonPart = Model.ContentPart;
|
||||
string lastModifiedBy = commonPart.As<CommonPart>() == null ? null : commonPart.As<CommonPart>().VersionModifiedBy;
|
||||
}
|
||||
|
||||
@T("By {0}", lastModifiedBy ?? T("unknown").ToString())
|
||||
@@ -1,14 +0,0 @@
|
||||
@*
|
||||
Model:
|
||||
ContentPart
|
||||
*@
|
||||
|
||||
@using Orchard.ContentManagement;
|
||||
@using Orchard.Core.Common.Models;
|
||||
@using Orchard.Security;
|
||||
@{
|
||||
CommonPart commonPart = Model.ContentPart;
|
||||
// owner isn't really who last modified this, is it?
|
||||
IUser owner = commonPart.As<CommonPart>() == null ? null : commonPart.As<CommonPart>().Owner;
|
||||
}
|
||||
@T("By {0}", owner == null ? T("unknown").ToString() : Convert.ToString(Html.ItemDisplayText(owner)))
|
||||
@@ -8,5 +8,5 @@
|
||||
<li>@if (modifiedUtc.HasValue) {
|
||||
@T("Last modified: {0}", Display.DateTimeRelative(DateTimeUtc: modifiedUtc.Value))} |
|
||||
</li>
|
||||
<li>@Display.CommonMetadataOwner(ContentPart: Model.ContentPart)</li>
|
||||
<li>@Display.CommonMetadataLastModified(ContentPart: Model.ContentPart)</li>
|
||||
</ul>
|
||||
@@ -292,7 +292,7 @@
|
||||
<Content Include="Common\Views\Fields.Common.Text.cshtml" />
|
||||
<Content Include="Common\Views\Parts.Common.Body.SummaryAdmin.cshtml" />
|
||||
<Content Include="Common\Views\Parts.Common.Metadata.cshtml" />
|
||||
<Content Include="Common\Views\CommonMetadataOwner.cshtml" />
|
||||
<Content Include="Common\Views\CommonMetadataLastModified.cshtml" />
|
||||
<Content Include="Containers\Module.txt" />
|
||||
<Content Include="Contents\Styles\images\menu.content.png" />
|
||||
<Content Include="Contents\Styles\menu.content-admin.css" />
|
||||
|
||||
@@ -13,5 +13,6 @@ namespace Orchard.ContentManagement.Aspects {
|
||||
DateTime? VersionCreatedUtc { get; set; }
|
||||
DateTime? VersionPublishedUtc { get; set; }
|
||||
DateTime? VersionModifiedUtc { get; set; }
|
||||
string VersionModifiedBy { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user