mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00

--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041298
49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
using Orchard.Core.Common.Records;
|
|
using Orchard.Data;
|
|
using Orchard.Models;
|
|
using Orchard.Models.Driver;
|
|
using Orchard.Security;
|
|
using Orchard.Services;
|
|
|
|
namespace Orchard.Core.Common.Models {
|
|
public class CommonDriver : ModelDriver {
|
|
private readonly IClock _clock;
|
|
private readonly IAuthenticationService _authenticationService;
|
|
private readonly IModelManager _modelManager;
|
|
|
|
public CommonDriver(
|
|
IRepository<CommonRecord> repository,
|
|
IClock clock,
|
|
IAuthenticationService authenticationService,
|
|
IModelManager modelManager) {
|
|
|
|
_clock = clock;
|
|
_authenticationService = authenticationService;
|
|
_modelManager = modelManager;
|
|
|
|
AddOnCreating<CommonModel>(SetCreateTimesAndAuthor);
|
|
Filters.Add(new StorageFilterForRecord<CommonRecord>(repository));
|
|
AddOnLoaded<CommonModel>(LoadOwnerModel);
|
|
}
|
|
|
|
void SetCreateTimesAndAuthor(CreateModelContext context, CommonModel instance) {
|
|
if (instance.Record.CreatedUtc == null) {
|
|
instance.Record.CreatedUtc = _clock.UtcNow;
|
|
}
|
|
if (instance.Record.ModifiedUtc == null) {
|
|
instance.Record.ModifiedUtc = _clock.UtcNow;
|
|
}
|
|
if (instance.Record.OwnerId == 0) {
|
|
instance.Owner = _authenticationService.GetAuthenticatedUser();
|
|
if (instance.Owner != null)
|
|
instance.Record.OwnerId = instance.Owner.Id;
|
|
}
|
|
}
|
|
|
|
void LoadOwnerModel(LoadModelContext context, CommonModel instance) {
|
|
if (instance.Record.OwnerId != 0) {
|
|
instance.Owner = _modelManager.Get(instance.Record.OwnerId).As<IUser>();
|
|
}
|
|
}
|
|
}
|
|
} |