AuthenticationSucceededLogger.cs 2.0 KB

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