2019-03-19 19:16:24 +01:00
|
|
|
|
using System;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2019-05-20 16:54:20 +02:00
|
|
|
|
using Umbraco.Core;
|
2019-12-18 13:05:34 +01:00
|
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
2020-01-20 14:15:54 -08:00
|
|
|
|
using Umbraco.Core.Mapping;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
2019-12-18 13:05:34 +01:00
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
2019-12-24 09:08:47 +01:00
|
|
|
|
using Umbraco.Core.Strings;
|
2018-10-01 14:32:46 +02:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Web.Mvc;
|
2020-02-14 13:04:49 +01:00
|
|
|
|
using Umbraco.Web.Routing;
|
2018-10-19 13:24:36 +11:00
|
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The API controller used for getting log history
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PluginController("UmbracoApi")]
|
|
|
|
|
|
public class LogController : UmbracoAuthorizedJsonController
|
|
|
|
|
|
{
|
2019-12-18 13:05:34 +01:00
|
|
|
|
private readonly IMediaFileSystem _mediaFileSystem;
|
2020-02-11 11:43:54 -08:00
|
|
|
|
private readonly IImageUrlGenerator _imageUrlGenerator;
|
2019-12-18 13:05:34 +01:00
|
|
|
|
|
|
|
|
|
|
public LogController(
|
|
|
|
|
|
IGlobalSettings globalSettings,
|
|
|
|
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
|
|
|
|
|
ISqlContext sqlContext,
|
|
|
|
|
|
ServiceContext services,
|
|
|
|
|
|
AppCaches appCaches,
|
|
|
|
|
|
IProfilingLogger logger,
|
|
|
|
|
|
IRuntimeState runtimeState,
|
2019-12-24 09:08:47 +01:00
|
|
|
|
IMediaFileSystem mediaFileSystem,
|
2020-01-20 14:15:54 -08:00
|
|
|
|
IShortStringHelper shortStringHelper,
|
2020-02-11 11:43:54 -08:00
|
|
|
|
UmbracoMapper umbracoMapper,
|
2020-02-14 13:04:49 +01:00
|
|
|
|
IImageUrlGenerator imageUrlGenerator,
|
|
|
|
|
|
IPublishedUrlProvider publishedUrlProvider)
|
2020-03-03 11:59:17 +01:00
|
|
|
|
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, shortStringHelper, umbracoMapper, publishedUrlProvider)
|
2019-12-18 13:05:34 +01:00
|
|
|
|
{
|
|
|
|
|
|
_mediaFileSystem = mediaFileSystem;
|
2020-02-11 11:43:54 -08:00
|
|
|
|
_imageUrlGenerator = imageUrlGenerator;
|
2019-12-18 13:05:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-05 13:45:42 +01:00
|
|
|
|
[UmbracoApplicationAuthorize(Core.Constants.Applications.Content, Core.Constants.Applications.Media)]
|
2018-06-29 19:52:40 +02:00
|
|
|
|
public PagedResult<AuditLog> GetPagedEntityLog(int id,
|
|
|
|
|
|
int pageNumber = 1,
|
2019-03-28 13:38:53 +01:00
|
|
|
|
int pageSize = 10,
|
2018-06-29 19:52:40 +02:00
|
|
|
|
Direction orderDirection = Direction.Descending,
|
|
|
|
|
|
DateTime? sinceDate = null)
|
|
|
|
|
|
{
|
2019-03-28 13:38:53 +01:00
|
|
|
|
if (pageSize <= 0 || pageNumber <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new PagedResult<AuditLog>(0, pageNumber, pageSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
long totalRecords;
|
|
|
|
|
|
var dateQuery = sinceDate.HasValue ? SqlContext.Query<IAuditItem>().Where(x => x.CreateDate >= sinceDate) : null;
|
|
|
|
|
|
var result = Services.AuditService.GetPagedItemsByEntity(id, pageNumber - 1, pageSize, out totalRecords, orderDirection, customFilter: dateQuery);
|
2019-11-05 12:54:22 +01:00
|
|
|
|
var mapped = result.Select(item => Mapper.Map<AuditLog>(item));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
var page = new PagedResult<AuditLog>(totalRecords, pageNumber, pageSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
Items = MapAvatarsAndNames(mapped)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return page;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PagedResult<AuditLog> GetPagedCurrentUserLog(
|
|
|
|
|
|
int pageNumber = 1,
|
2019-03-28 13:38:53 +01:00
|
|
|
|
int pageSize = 10,
|
2018-06-29 19:52:40 +02:00
|
|
|
|
Direction orderDirection = Direction.Descending,
|
|
|
|
|
|
DateTime? sinceDate = null)
|
|
|
|
|
|
{
|
2019-03-28 13:38:53 +01:00
|
|
|
|
if (pageSize <= 0 || pageNumber <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new PagedResult<AuditLog>(0, pageNumber, pageSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
long totalRecords;
|
|
|
|
|
|
var dateQuery = sinceDate.HasValue ? SqlContext.Query<IAuditItem>().Where(x => x.CreateDate >= sinceDate) : null;
|
|
|
|
|
|
var userId = Security.GetUserId().ResultOr(0);
|
|
|
|
|
|
var result = Services.AuditService.GetPagedItemsByUser(userId, pageNumber - 1, pageSize, out totalRecords, orderDirection, customFilter:dateQuery);
|
2019-04-08 16:38:18 +02:00
|
|
|
|
var mapped = Mapper.MapEnumerable<IAuditItem, AuditLog>(result);
|
2018-10-01 14:32:46 +02:00
|
|
|
|
return new PagedResult<AuditLog>(totalRecords, pageNumber, pageSize)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
Items = MapAvatarsAndNames(mapped)
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2019-11-05 12:54:22 +01:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
private IEnumerable<AuditLog> MapAvatarsAndNames(IEnumerable<AuditLog> items)
|
|
|
|
|
|
{
|
2019-01-03 10:32:34 +01:00
|
|
|
|
var mappedItems = items.ToList();
|
|
|
|
|
|
var userIds = mappedItems.Select(x => x.UserId).ToArray();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
var userAvatars = Services.UserService.GetUsersById(userIds)
|
2020-02-11 11:43:54 -08:00
|
|
|
|
.ToDictionary(x => x.Id, x => x.GetUserAvatarUrls(AppCaches.RuntimeCache, _mediaFileSystem, _imageUrlGenerator));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
var userNames = Services.UserService.GetUsersById(userIds).ToDictionary(x => x.Id, x => x.Name);
|
2019-01-03 10:32:34 +01:00
|
|
|
|
foreach (var item in mappedItems)
|
2018-05-31 15:54:23 +10:00
|
|
|
|
{
|
|
|
|
|
|
if (userAvatars.TryGetValue(item.UserId, out var avatars))
|
|
|
|
|
|
{
|
|
|
|
|
|
item.UserAvatars = avatars;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (userNames.TryGetValue(item.UserId, out var name))
|
|
|
|
|
|
{
|
|
|
|
|
|
item.UserName = name;
|
|
|
|
|
|
}
|
2019-11-05 12:54:22 +01:00
|
|
|
|
|
2018-05-31 15:54:23 +10:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
2019-01-03 10:32:34 +01:00
|
|
|
|
return mappedItems;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|