AuthenticationSucceededLogger.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Globalization;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Database.Implementations.Entities;
  4. using MediaBrowser.Controller.Events;
  5. using MediaBrowser.Controller.Events.Authentication;
  6. using MediaBrowser.Model.Activity;
  7. using MediaBrowser.Model.Globalization;
  8. namespace Jellyfin.Server.Implementations.Events.Consumers.Security
  9. {
  10. /// <summary>
  11. /// Creates an entry in the activity log when there is a successful login attempt.
  12. /// </summary>
  13. public class AuthenticationSucceededLogger : IEventConsumer<AuthenticationResultEventArgs>
  14. {
  15. private readonly ILocalizationManager _localizationManager;
  16. private readonly IActivityManager _activityManager;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="AuthenticationSucceededLogger"/> class.
  19. /// </summary>
  20. /// <param name="localizationManager">The localization manager.</param>
  21. /// <param name="activityManager">The activity manager.</param>
  22. public AuthenticationSucceededLogger(ILocalizationManager localizationManager, IActivityManager activityManager)
  23. {
  24. _localizationManager = localizationManager;
  25. _activityManager = activityManager;
  26. }
  27. /// <inheritdoc />
  28. public async Task OnEvent(AuthenticationResultEventArgs eventArgs)
  29. {
  30. await _activityManager.CreateAsync(new ActivityLog(
  31. string.Format(
  32. CultureInfo.InvariantCulture,
  33. _localizationManager.GetLocalizedString("AuthenticationSucceededWithUserName"),
  34. eventArgs.User.Name),
  35. "AuthenticationSucceeded",
  36. eventArgs.User.Id)
  37. {
  38. ShortOverview = string.Format(
  39. CultureInfo.InvariantCulture,
  40. _localizationManager.GetLocalizedString("LabelIpAddressValue"),
  41. eventArgs.SessionInfo?.RemoteEndPoint),
  42. }).ConfigureAwait(false);
  43. }
  44. }
  45. }