Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.AuditLog;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Actions;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Security.Authorization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Controllers.Document;

[ApiVersion("1.0")]
public class GetAuditLogDocumentController : DocumentControllerBase
{
private readonly IAuthorizationService _authorizationService;
private readonly IAuditService _auditService;
private readonly IAuditLogPresentationFactory _auditLogPresentationFactory;

public GetAuditLogDocumentController(
IAuthorizationService authorizationService,
IAuditService auditService,
IAuditLogPresentationFactory auditLogPresentationFactory)
{
_authorizationService = authorizationService;
_auditService = auditService;
_auditLogPresentationFactory = auditLogPresentationFactory;
}

[MapToApiVersion("1.0")]
[HttpGet("{id:guid}/audit-log")]
[ProducesResponseType(typeof(PagedViewModel<AuditLogResponseModel>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAuditLog(CancellationToken cancellationToken, Guid id, Direction orderDirection = Direction.Descending, DateTimeOffset? sinceDate = null, int skip = 0, int take = 100)
{
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
User,
ContentPermissionResource.WithKeys(ActionProtect.ActionLetter, id),
AuthorizationPolicies.ContentPermissionByResource);

if (!authorizationResult.Succeeded)
{
return Forbidden();
}

PagedModel<IAuditItem> result = await _auditService.GetItemsByKeyAsync(id, UmbracoObjectTypes.Document, skip, take, orderDirection, sinceDate);
IEnumerable<AuditLogResponseModel> mapped = _auditLogPresentationFactory.CreateAuditLogViewModel(result.Items);
var viewModel = new PagedViewModel<AuditLogResponseModel>
{
Total = result.Total,
Items = mapped,
};

return Ok(viewModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public async Task<IActionResult> AllLogs(
Direction orderDirection = Direction.Descending,
string? filterExpression = null,
[FromQuery(Name = "logLevel")] LogLevel[]? logLevels = null,
DateTime? startDate = null,
DateTime? endDate = null)
DateTimeOffset? startDate = null,
DateTimeOffset? endDate = null)
{
var levels = logLevels?.Select(l => l.ToString()).ToArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public async Task<IActionResult> AllMessageTemplates(
CancellationToken cancellationToken,
int skip = 0,
int take = 100,
DateTime? startDate = null,
DateTime? endDate = null)
DateTimeOffset? startDate = null,
DateTimeOffset? endDate = null)
{
Attempt<PagedModel<LogTemplate>, LogViewerOperationStatus> messageTemplatesAttempt = await _logViewerService.GetMessageTemplatesAsync(startDate, endDate, skip, take);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public LogLevelCountLogViewerController(ILogViewerService logViewerService, IUmb
[ProducesResponseType(typeof(LogLevelCountsReponseModel), StatusCodes.Status200OK)]
public async Task<IActionResult> LogLevelCounts(
CancellationToken cancellationToken,
DateTime? startDate = null,
DateTime? endDate = null)
DateTimeOffset? startDate = null,
DateTimeOffset? endDate = null)
{
Attempt<LogLevelCounts?, LogViewerOperationStatus> logLevelCountsAttempt =
await _logViewerService.GetLogLevelCountsAsync(startDate, endDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class ValidateLogFileSizeLogViewerController : LogViewerControllerBase
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> CanViewLogs(
CancellationToken cancellationToken,
DateTime? startDate = null,
DateTime? endDate = null)
DateTimeOffset? startDate = null,
DateTimeOffset? endDate = null)
{
Attempt<bool, LogViewerOperationStatus> result = await _logViewerService.CanViewLogsAsync(startDate, endDate);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.AuditLog;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Security.Authorization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Controllers.Media;

[ApiVersion("1.0")]
public class GetAuditLogMediaController : MediaControllerBase
{
private readonly IAuthorizationService _authorizationService;
private readonly IAuditService _auditService;
private readonly IAuditLogPresentationFactory _auditLogPresentationFactory;

public GetAuditLogMediaController(
IAuthorizationService authorizationService,
IAuditService auditService,
IAuditLogPresentationFactory auditLogPresentationFactory)
{
_authorizationService = authorizationService;
_auditService = auditService;
_auditLogPresentationFactory = auditLogPresentationFactory;
}

[MapToApiVersion("1.0")]
[HttpGet("{id:guid}/audit-log")]
[ProducesResponseType(typeof(PagedViewModel<AuditLogResponseModel>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAuditLog(CancellationToken cancellationToken, Guid id, Direction orderDirection = Direction.Descending, DateTimeOffset? sinceDate = null, int skip = 0, int take = 100)
{
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
User,
MediaPermissionResource.WithKeys(id),
AuthorizationPolicies.MediaPermissionByResource);

if (!authorizationResult.Succeeded)
{
return Forbidden();
}

PagedModel<IAuditItem> result = await _auditService.GetItemsByKeyAsync(id, UmbracoObjectTypes.Media, skip, take, orderDirection, sinceDate);
IEnumerable<AuditLogResponseModel> mapped = _auditLogPresentationFactory.CreateAuditLogViewModel(result.Items);
var viewModel = new PagedViewModel<AuditLogResponseModel>
{
Total = result.Total,
Items = mapped,
};

return Ok(viewModel);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.AuditLogs;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Api.Management.ViewModels.AuditLog;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;

Expand All @@ -13,57 +9,25 @@ namespace Umbraco.Cms.Api.Management.Factories;
public class AuditLogPresentationFactory : IAuditLogPresentationFactory
{
private readonly IUserService _userService;
private readonly AppCaches _appCaches;
private readonly MediaFileManager _mediaFileManager;
private readonly IImageUrlGenerator _imageUrlGenerator;
private readonly IEntityService _entityService;
private readonly IUserIdKeyResolver _userIdKeyResolver;

public AuditLogPresentationFactory(IUserService userService, AppCaches appCaches, MediaFileManager mediaFileManager, IImageUrlGenerator imageUrlGenerator, IEntityService entityService, IUserIdKeyResolver userIdKeyResolver)
public AuditLogPresentationFactory(IUserService userService, IUserIdKeyResolver userIdKeyResolver)
{
_userService = userService;
_appCaches = appCaches;
_mediaFileManager = mediaFileManager;
_imageUrlGenerator = imageUrlGenerator;
_entityService = entityService;
_userIdKeyResolver = userIdKeyResolver;
}

public IEnumerable<AuditLogResponseModel> CreateAuditLogViewModel(IEnumerable<IAuditItem> auditItems) => auditItems.Select(CreateAuditLogViewModel);

public IEnumerable<AuditLogWithUsernameResponseModel> CreateAuditLogWithUsernameViewModels(IEnumerable<IAuditItem> auditItems) => auditItems.Select(CreateAuditLogWithUsernameViewModel);

private AuditLogWithUsernameResponseModel CreateAuditLogWithUsernameViewModel(IAuditItem auditItem)
{
AuditLogWithUsernameResponseModel target = CreateResponseModel<AuditLogWithUsernameResponseModel>(auditItem, out IUser user);

target.UserAvatars = user.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileManager, _imageUrlGenerator);
target.UserName = user.Name;
return target;
}

private AuditLogResponseModel CreateAuditLogViewModel(IAuditItem auditItem)
=> CreateResponseModel<AuditLogResponseModel>(auditItem, out _);

private T CreateResponseModel<T>(IAuditItem auditItem, out IUser user)
where T : AuditLogBaseModel, new()
{
Guid userKey = _userIdKeyResolver.GetAsync(auditItem.UserId).GetAwaiter().GetResult();
user = _userService.GetAsync(userKey).GetAwaiter().GetResult()
?? throw new ArgumentException($"Could not find user with id {auditItem.UserId}");

IEntitySlim? entitySlim = _entityService.Get(auditItem.Id);
IUser user = _userService.GetAsync(userKey).GetAwaiter().GetResult()
?? throw new ArgumentException($"Could not find user with id {auditItem.UserId}");

return new T
return new AuditLogResponseModel
{
Comment = auditItem.Comment,
Entity = auditItem.EntityType is not null || entitySlim is not null
? new AuditLogEntity
{
Id = entitySlim?.Key,
Type = auditItem.EntityType
}
: null,
LogType = auditItem.AuditType,
Parameters = auditItem.Parameters,
Timestamp = auditItem.CreateDate,
Expand Down
Loading