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 ;
2020-05-26 19:01:37 +02:00
using Microsoft.AspNetCore.Mvc ;
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 ;
2020-03-24 09:37:46 +01:00
using Umbraco.Core.Media ;
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 ;
2020-05-26 19:01:37 +02:00
using Umbraco.Web.BackOffice.Filters ;
using Umbraco.Web.Common.Attributes ;
using Umbraco.Web.Editors ;
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
2020-05-26 19:01:37 +02:00
namespace Umbraco.Web.BackOffice.Controllers
2018-06-29 19:52:40 +02:00
{
/// <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 ;
2020-05-26 19:01:37 +02:00
private readonly IAuditService _auditService ;
private readonly UmbracoMapper _umbracoMapper ;
private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
private readonly IUserService _userService ;
private readonly AppCaches _appCaches ;
private readonly ISqlContext _sqlContext ;
2019-12-18 13:05:34 +01:00
public LogController (
2019-12-24 09:08:47 +01:00
IMediaFileSystem mediaFileSystem ,
2020-02-14 13:04:49 +01:00
IImageUrlGenerator imageUrlGenerator ,
2020-05-26 19:01:37 +02:00
IAuditService auditService ,
UmbracoMapper umbracoMapper ,
IUmbracoContextAccessor umbracoContextAccessor ,
IUserService userService ,
AppCaches appCaches ,
ISqlContext sqlContext )
{
_mediaFileSystem = 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 ) ) ;
_umbracoContextAccessor = umbracoContextAccessor ? ? throw new ArgumentNullException ( nameof ( umbracoContextAccessor ) ) ;
_userService = userService ? ? throw new ArgumentNullException ( nameof ( userService ) ) ;
_appCaches = appCaches ? ? throw new ArgumentNullException ( nameof ( appCaches ) ) ;
_sqlContext = sqlContext ? ? throw new ArgumentNullException ( nameof ( sqlContext ) ) ;
}
2019-12-18 13:05:34 +01:00
2020-05-26 19:01:37 +02:00
[TypeFilter(typeof(UmbracoApplicationAuthorizeAttribute), Arguments = new object[] { new string [ ] { Constants . Applications . Content , 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 ;
2020-05-26 19:01:37 +02:00
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 ) ) ;
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 ;
2020-05-26 19:01:37 +02:00
var umbracoContext = _umbracoContextAccessor . GetRequiredUmbracoContext ( ) ;
var dateQuery = sinceDate . HasValue ? _sqlContext . Query < IAuditItem > ( ) . Where ( x = > x . CreateDate > = sinceDate ) : null ;
var userId = umbracoContext . Security . GetUserId ( ) . ResultOr ( 0 ) ;
var result = _auditService . GetPagedItemsByUser ( userId , pageNumber - 1 , pageSize , out totalRecords , orderDirection , customFilter : dateQuery ) ;
var mapped = _umbracoMapper . 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 ( ) ;
2020-05-26 19:01:37 +02:00
var userAvatars = _userService . GetUsersById ( userIds )
. ToDictionary ( x = > x . Id , x = > x . GetUserAvatarUrls ( _appCaches . RuntimeCache , _mediaFileSystem , _imageUrlGenerator ) ) ;
var userNames = _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
}
}
}