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 repository, IClock clock, IAuthenticationService authenticationService, IModelManager modelManager) { _clock = clock; _authenticationService = authenticationService; _modelManager = modelManager; AddOnCreating(SetCreateTimesAndAuthor); Filters.Add(new StorageFilterForRecord(repository)); AddOnLoaded(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(); } } } }