-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeleteUser.cs
More file actions
112 lines (96 loc) · 4.86 KB
/
Copy pathDeleteUser.cs
File metadata and controls
112 lines (96 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Hl7.Fhir.Utility;
using LantanaGroup.Link.Account.Application.Commands.AuditEvent;
using LantanaGroup.Link.Account.Application.Interfaces.Infrastructure;
using LantanaGroup.Link.Account.Application.Interfaces.Persistence;
using LantanaGroup.Link.Account.Domain.Entities;
using LantanaGroup.Link.Account.Infrastructure;
using LantanaGroup.Link.Account.Infrastructure.Logging;
using LantanaGroup.Link.Shared.Application.Extensions.Telemetry;
using LantanaGroup.Link.Shared.Application.Interfaces;
using LantanaGroup.Link.Shared.Application.Models;
using LantanaGroup.Link.Shared.Application.Models.Kafka;
using LantanaGroup.Link.Shared.Application.Models.Telemetry;
using Link.Authorization.Infrastructure;
using System.Diagnostics;
using System.Security.Claims;
namespace LantanaGroup.Link.Account.Application.Commands.User
{
public class DeleteUser : IDeleteUser
{
private readonly ILogger<CreateUser> _logger;
private readonly IUserRepository _userRepository;
private readonly IAccountServiceMetrics _metrics;
private readonly ICreateAuditEvent _createAuditEvent;
private readonly ICacheService _cache;
public DeleteUser(ILogger<CreateUser> logger, IUserRepository userRepository, ICacheService cache, IAccountServiceMetrics metrics, ICreateAuditEvent createAuditEvent)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository));
_metrics = metrics ?? throw new ArgumentNullException(nameof(metrics));
_createAuditEvent = createAuditEvent ?? throw new ArgumentNullException(nameof(createAuditEvent));
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}
public async Task<bool> Execute(ClaimsPrincipal? requestor, Guid userId, CancellationToken cancellationToken = default)
{
List<KeyValuePair<string, object?>> tagList = [new KeyValuePair<string, object?>(DiagnosticNames.UserId, userId)];
using Activity? activity = ServiceActivitySource.Instance.StartActivityWithTags("ActivateUser:Execute", tagList);
try
{
var user = await _userRepository.GetUserAsync(userId, cancellationToken: cancellationToken) ?? throw new ApplicationException($"User with id {userId} not found");
if (user.IsDeleted)
{
return true;
}
user.IsDeleted = true;
if (requestor is not null)
{
user.LastModifiedBy = requestor?.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;
}
var result = await _userRepository.UpdateAsync(user, cancellationToken);
if (!result)
{
_logger.LogDeleteUserException(userId.ToString(), "Failed to delete user");
return false;
}
//generate tags for telemetry
foreach (var claim in user.Claims.Where(x => x.ClaimType == LinkAuthorizationConstants.LinkSystemClaims.Facility))
{
var tag = new KeyValuePair<string, object?>(DiagnosticNames.FacilityId, claim.ClaimValue);
tagList.Add(tag);
activity?.AddTag(tag.Key, tag.Value);
}
_metrics.IncrementAccountDeletedCounter(tagList);
_logger.LogDeleteUser(userId.ToString(), requestor?.Claims.FirstOrDefault(c => c.Type == "sub")?.Value ?? "Unknown");
//generate audit event
var auditMessage = new AuditEventMessage
{
Action = AuditEventType.Delete,
EventDate = DateTime.UtcNow,
UserId = user.LastModifiedBy,
User = requestor?.Identity?.Name ?? string.Empty,
Resource = typeof(LinkUser).Name,
PropertyChanges = new List<PropertyChangeModel>([
new PropertyChangeModel("IsDeleted", "False", "True")
]),
Notes = $"User ({user.Id}) deleted by '{user.LastModifiedBy}'."
};
_ = Task.Run(() => _createAuditEvent.Execute(auditMessage, cancellationToken));
var userKey = "user:" + user.Email;
try
{
await _cache.RemoveAsync(userKey, cancellationToken);
}
catch (Exception ex)
{
_logger.LogCacheException(userKey, ex.Message);
}
return result;
}
catch (Exception)
{
Activity.Current?.SetStatus(ActivityStatusCode.Error);
throw;
}
}
}
}