* Allow IMediaFileSystem to be replace in the DI, or registered with inner filesystem * Remove GetFileSystem from Filesystems It was only used by tests. * Make MediaFileSystem inherit from PhysicalFileSystem directly * Remove FileSystemWrapper * Remove inner filesystem from MediaFileSystem * Add MediaFileManager and bare minimum to make it testable * Remove MediaFileSystem * Fix unit tests using MediaFileManager * Remove IFileSystem and rely only on FileSystem * Hide dangerous methods in FileSystems and do some cleaning * Apply stylecop warnings to MediaFileManager * Add FilesystemsCreator to Tests.Common This allows you to create an instance if FileSystems with your own specified IFileSystem for testing purposes outside our own test suite. * Allow the stylesheet filesystem to be replaced. * Fix tests * Don't save stylesheetWrapper in a temporary var * refactor(FileSystems): change how stylesheet filesystem is registered * fix(FileSystems): unable to overwrite media filesystem SetMediaFileSystem added the MediaManager as a Singleton instead of replacing the existing instance. * fix(FileSystems): calling AddFileSystems replaces MediaManager When calling AddFileSystems after SetMediaFileSystem the MediaManager gets replaced by the default PhysicalFileSystem, so instead of calling SetMediaFileSystem in AddFileSystems we now call TrySetMediaFileSystem instead. This method will not replace any existing instance of the MediaManager if there's already a MediaManager registered. * Use SetMediaFileSystem instead of TrySet, and rename AddFilesystems to ConfigureFileSystems Also don't call AddFileSystems again in ConfigureFilesystems * Don't wrap CSS filesystem twice * Add CreateShadowWrapperInternal to avoid casting * Throw UnauthorizedAccessException isntead of InvalidOperationException * Remove ResetShadowId Co-authored-by: Rasmus John Pedersen <mail@rjp.dk>
133 lines
5.8 KiB
C#
133 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.Cache;
|
|
using Umbraco.Cms.Core.IO;
|
|
using Umbraco.Cms.Core.Mapping;
|
|
using Umbraco.Cms.Core.Media;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
using Umbraco.Cms.Core.Security;
|
|
using Umbraco.Cms.Core.Services;
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
|
{
|
|
/// <summary>
|
|
/// The API controller used for getting log history
|
|
/// </summary>
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
|
public class LogController : UmbracoAuthorizedJsonController
|
|
{
|
|
private readonly MediaFileManager _mediaFileManager;
|
|
private readonly IImageUrlGenerator _imageUrlGenerator;
|
|
private readonly IAuditService _auditService;
|
|
private readonly IUmbracoMapper _umbracoMapper;
|
|
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
|
|
private readonly IUserService _userService;
|
|
private readonly AppCaches _appCaches;
|
|
private readonly ISqlContext _sqlContext;
|
|
|
|
public LogController(
|
|
MediaFileManager mediaFileSystem,
|
|
IImageUrlGenerator imageUrlGenerator,
|
|
IAuditService auditService,
|
|
IUmbracoMapper umbracoMapper,
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor,
|
|
IUserService userService,
|
|
AppCaches appCaches,
|
|
ISqlContext sqlContext)
|
|
{
|
|
_mediaFileManager = mediaFileSystem ?? throw new ArgumentNullException(nameof(mediaFileSystem));
|
|
_imageUrlGenerator = imageUrlGenerator ?? throw new ArgumentNullException(nameof(imageUrlGenerator));
|
|
_auditService = auditService ?? throw new ArgumentNullException(nameof(auditService));
|
|
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
|
_backofficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor));
|
|
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
|
|
_appCaches = appCaches ?? throw new ArgumentNullException(nameof(appCaches));
|
|
_sqlContext = sqlContext ?? throw new ArgumentNullException(nameof(sqlContext));
|
|
}
|
|
|
|
[Authorize(Policy = AuthorizationPolicies.SectionAccessContentOrMedia)]
|
|
public PagedResult<AuditLog> GetPagedEntityLog(int id,
|
|
int pageNumber = 1,
|
|
int pageSize = 10,
|
|
Direction orderDirection = Direction.Descending,
|
|
DateTime? sinceDate = null)
|
|
{
|
|
if (pageSize <= 0 || pageNumber <= 0)
|
|
{
|
|
return new PagedResult<AuditLog>(0, pageNumber, pageSize);
|
|
}
|
|
|
|
long totalRecords;
|
|
var dateQuery = sinceDate.HasValue ? _sqlContext.Query<IAuditItem>().Where(x => x.CreateDate >= sinceDate) : null;
|
|
var result = _auditService.GetPagedItemsByEntity(id, pageNumber - 1, pageSize, out totalRecords, orderDirection, customFilter: dateQuery);
|
|
var mapped = result.Select(item => _umbracoMapper.Map<AuditLog>(item));
|
|
|
|
var page = new PagedResult<AuditLog>(totalRecords, pageNumber, pageSize)
|
|
{
|
|
Items = MapAvatarsAndNames(mapped)
|
|
};
|
|
|
|
return page;
|
|
}
|
|
|
|
public PagedResult<AuditLog> GetPagedCurrentUserLog(
|
|
int pageNumber = 1,
|
|
int pageSize = 10,
|
|
Direction orderDirection = Direction.Descending,
|
|
DateTime? sinceDate = null)
|
|
{
|
|
if (pageSize <= 0 || pageNumber <= 0)
|
|
{
|
|
return new PagedResult<AuditLog>(0, pageNumber, pageSize);
|
|
}
|
|
|
|
long totalRecords;
|
|
var dateQuery = sinceDate.HasValue ? _sqlContext.Query<IAuditItem>().Where(x => x.CreateDate >= sinceDate) : null;
|
|
var userId = _backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().ResultOr(0);
|
|
var result = _auditService.GetPagedItemsByUser(userId, pageNumber - 1, pageSize, out totalRecords, orderDirection, customFilter:dateQuery);
|
|
var mapped = _umbracoMapper.MapEnumerable<IAuditItem, AuditLog>(result);
|
|
return new PagedResult<AuditLog>(totalRecords, pageNumber, pageSize)
|
|
{
|
|
Items = MapAvatarsAndNames(mapped)
|
|
};
|
|
}
|
|
|
|
public IEnumerable<AuditLog> GetLog(AuditType logType, DateTime? sinceDate = null)
|
|
{
|
|
var result = _auditService.GetLogs(Enum<AuditType>.Parse(logType.ToString()), sinceDate);
|
|
var mapped = _umbracoMapper.MapEnumerable<IAuditItem, AuditLog>(result);
|
|
return mapped;
|
|
}
|
|
|
|
private IEnumerable<AuditLog> MapAvatarsAndNames(IEnumerable<AuditLog> items)
|
|
{
|
|
var mappedItems = items.ToList();
|
|
var userIds = mappedItems.Select(x => x.UserId).ToArray();
|
|
var userAvatars = Enumerable.ToDictionary(_userService.GetUsersById(userIds), x => x.Id, x => x.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileManager, _imageUrlGenerator));
|
|
var userNames = Enumerable.ToDictionary(_userService.GetUsersById(userIds), x => x.Id, x => x.Name);
|
|
foreach (var item in mappedItems)
|
|
{
|
|
if (userAvatars.TryGetValue(item.UserId, out var avatars))
|
|
{
|
|
item.UserAvatars = avatars;
|
|
}
|
|
if (userNames.TryGetValue(item.UserId, out var name))
|
|
{
|
|
item.UserName = name;
|
|
}
|
|
|
|
|
|
}
|
|
return mappedItems;
|
|
}
|
|
}
|
|
}
|