Files
Umbraco-CMS/src/Umbraco.Cms.Api.Management/Controllers/AuditLog/CurrentUserAuditLogController.cs
Nikolaj Geisle b96302a9da V13: Audit log controller (#13842)
* Implement AuditLogByTypeViewModel

* Add GetLogs endpoint

* Implement AuditLogViewModelFactory and use it

* Remove mapping and do that in ViewModelFactory

* Add Skip take pagination to AuditService.cs

* Get entity key from repository

* Only create filter if sincedate has value

* Refactor to handle enitity not being found

* Add by type route

* Rename AuditLogViewModel.cs

* Add CreateAuditLogWithUsernameViewModel method to factory

* Rename method in factory

* Fix up controllers to use async service

* Refactor to use pagedModel

* Update sincedate query

* Add FIXME

* Move entity call in scope

* Rename methods to async

* Update src/Umbraco.Core/PaginationHelper.cs

* Refactor to use IActionResult

* Audit log in two words

* Fix up by key

* Fix routing

* Fix ByType

* Update ByType routing

* Refactor to also pass DateStamp

* Add Key to WithUserNameViewModel

* Refactor extension method to call new GetAsync method

* use new GetAsync method

* Rename models and implement base class

* Update OpenApi.json

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch>
2023-02-23 11:32:24 +01:00

70 lines
2.6 KiB
C#

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.AuditLogs;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Exceptions;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.New.Cms.Core.Models;
namespace Umbraco.Cms.Api.Management.Controllers.AuditLog;
public class CurrentUserAuditLogController : AuditLogControllerBase
{
private readonly IAuditService _auditService;
private readonly IAuditLogViewModelFactory _auditLogViewModelFactory;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly IUserService _userService;
public CurrentUserAuditLogController(
IAuditService auditService,
IAuditLogViewModelFactory auditLogViewModelFactory,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IUserService userService)
{
_auditService = auditService;
_auditLogViewModelFactory = auditLogViewModelFactory;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_userService = userService;
}
[HttpGet]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<AuditLogWithUsernameResponseModel>), StatusCodes.Status200OK)]
public async Task<IActionResult> CurrentUser(Direction orderDirection = Direction.Descending, DateTime? sinceDate = null, int skip = 0, int take = 100)
{
// FIXME: Pull out current backoffice user when its implemented.
// var userId = _backOfficeSecurityAccessor.BackOfficeSecurity?.GetUserId().Result ?? -1;
var userId = Constants.Security.SuperUserId;
IUser? user = _userService.GetUserById(userId);
if (user is null)
{
throw new PanicException("Could not find current user");
}
PagedModel<IAuditItem> result = await _auditService.GetPagedItemsByUserAsync(
user.Key,
skip,
take,
orderDirection,
null,
sinceDate);
IEnumerable<AuditLogWithUsernameResponseModel> mapped = _auditLogViewModelFactory.CreateAuditLogWithUsernameViewModels(result.Items.Skip(skip).Take(take));
var viewModel = new PagedViewModel<AuditLogWithUsernameResponseModel>
{
Total = result.Total,
Items = mapped,
};
return Ok(viewModel);
}
}