jellyfin-server/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs

273 lines
11 KiB
C#
Raw Normal View History

using System;
using System.Globalization;
2019-01-27 14:40:37 +00:00
using System.Threading.Tasks;
2020-05-02 22:32:22 +00:00
using Jellyfin.Data.Entities;
using Jellyfin.Data.Events;
2014-08-10 22:13:17 +00:00
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller.Authentication;
2014-08-10 22:13:17 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Activity;
2016-10-24 02:45:23 +00:00
using MediaBrowser.Model.Globalization;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.Notifications;
using MediaBrowser.Model.Updates;
using Microsoft.Extensions.Logging;
2014-08-10 22:13:17 +00:00
2016-11-03 07:14:14 +00:00
namespace Emby.Server.Implementations.Activity
2014-08-10 22:13:17 +00:00
{
/// <summary>
2020-04-11 10:03:10 +00:00
/// Entry point for the activity logger.
/// </summary>
2020-02-08 21:25:44 +00:00
public sealed class ActivityLogEntryPoint : IServerEntryPoint
2014-08-10 22:13:17 +00:00
{
private readonly IInstallationManager _installationManager;
private readonly ISessionManager _sessionManager;
private readonly IActivityManager _activityManager;
private readonly ILocalizationManager _localization;
private readonly IUserManager _userManager;
2019-11-01 17:38:54 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ActivityLogEntryPoint"/> class.
/// </summary>
/// <param name="sessionManager">The session manager.</param>
/// <param name="activityManager">The activity manager.</param>
/// <param name="localization">The localization manager.</param>
/// <param name="installationManager">The installation manager.</param>
/// <param name="userManager">The user manager.</param>
public ActivityLogEntryPoint(
ISessionManager sessionManager,
IActivityManager activityManager,
ILocalizationManager localization,
IInstallationManager installationManager,
2020-02-08 21:25:44 +00:00
IUserManager userManager)
2014-08-10 22:13:17 +00:00
{
_sessionManager = sessionManager;
_activityManager = activityManager;
_localization = localization;
_installationManager = installationManager;
_userManager = userManager;
}
/// <inheritdoc />
2019-01-27 14:40:37 +00:00
public Task RunAsync()
2014-08-10 22:13:17 +00:00
{
2019-02-28 21:33:20 +00:00
_installationManager.PluginInstalled += OnPluginInstalled;
_installationManager.PluginUninstalled += OnPluginUninstalled;
_installationManager.PluginUpdated += OnPluginUpdated;
_installationManager.PackageInstallationFailed += OnPackageInstallationFailed;
2014-08-10 22:13:17 +00:00
2019-02-28 21:33:20 +00:00
_sessionManager.SessionStarted += OnSessionStarted;
_sessionManager.AuthenticationFailed += OnAuthenticationFailed;
_sessionManager.AuthenticationSucceeded += OnAuthenticationSucceeded;
_sessionManager.SessionEnded += OnSessionEnded;
2014-08-10 22:13:17 +00:00
2020-05-13 02:10:35 +00:00
_userManager.OnUserPasswordChanged += OnUserPasswordChanged;
_userManager.OnUserLockedOut += OnUserLockedOut;
2014-08-10 22:13:17 +00:00
2019-01-27 14:40:37 +00:00
return Task.CompletedTask;
2014-08-10 22:13:17 +00:00
}
2020-05-13 02:10:35 +00:00
private async void OnUserLockedOut(object sender, GenericEventArgs<User> e)
2015-03-02 05:16:29 +00:00
{
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
2020-05-13 19:03:35 +00:00
string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("UserLockedOutWithName"),
e.Argument.Username),
2020-05-13 19:03:35 +00:00
NotificationType.UserLockedOut.ToString(),
e.Argument.Id)
{
LogSeverity = LogLevel.Error
}).ConfigureAwait(false);
2015-03-02 05:16:29 +00:00
}
2020-05-02 22:32:22 +00:00
private async void OnSessionEnded(object sender, SessionEventArgs e)
2014-08-10 22:13:17 +00:00
{
var session = e.SessionInfo;
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(session.UserName))
2014-08-10 22:13:17 +00:00
{
2014-08-14 13:24:30 +00:00
return;
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("UserOfflineFromDevice"),
session.UserName,
session.DeviceName),
2020-05-02 22:32:22 +00:00
"SessionEnded",
2020-05-13 19:03:35 +00:00
session.UserId)
2020-05-02 22:32:22 +00:00
{
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("LabelIpAddressValue"),
session.RemoteEndPoint),
2020-05-02 22:32:22 +00:00
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
private async void OnAuthenticationSucceeded(object sender, GenericEventArgs<AuthenticationResult> e)
2014-08-10 22:13:17 +00:00
{
2018-09-12 17:26:21 +00:00
var user = e.Argument.User;
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("AuthenticationSucceededWithUserName"),
user.Name),
2020-05-02 22:32:22 +00:00
"AuthenticationSucceeded",
2020-05-13 19:03:35 +00:00
user.Id)
2020-05-02 22:32:22 +00:00
{
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("LabelIpAddressValue"),
e.Argument.SessionInfo.RemoteEndPoint),
2020-05-02 22:32:22 +00:00
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
private async void OnAuthenticationFailed(object sender, GenericEventArgs<AuthenticationRequest> e)
2014-08-10 22:13:17 +00:00
{
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("FailedLoginAttemptWithUserName"),
e.Argument.Username),
2020-05-02 22:32:22 +00:00
"AuthenticationFailed",
2020-05-13 19:03:35 +00:00
Guid.Empty)
2020-05-02 22:32:22 +00:00
{
2020-05-13 19:03:35 +00:00
LogSeverity = LogLevel.Error,
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("LabelIpAddressValue"),
e.Argument.RemoteEndPoint),
2020-05-02 22:32:22 +00:00
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
2020-05-13 02:10:35 +00:00
private async void OnUserPasswordChanged(object sender, GenericEventArgs<User> e)
2014-08-10 22:13:17 +00:00
{
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("UserPasswordChangedWithName"),
2020-05-13 02:10:35 +00:00
e.Argument.Username),
2020-05-02 22:32:22 +00:00
"UserPasswordChanged",
2020-05-13 19:03:35 +00:00
e.Argument.Id))
.ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
private async void OnSessionStarted(object sender, SessionEventArgs e)
2014-08-10 22:13:17 +00:00
{
var session = e.SessionInfo;
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(session.UserName))
2014-08-10 22:13:17 +00:00
{
2014-08-14 13:24:30 +00:00
return;
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("UserOnlineFromDevice"),
session.UserName,
session.DeviceName),
2020-05-02 22:32:22 +00:00
"SessionStarted",
2020-05-13 19:03:35 +00:00
session.UserId)
2020-05-02 22:32:22 +00:00
{
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("LabelIpAddressValue"),
2020-05-02 22:32:22 +00:00
session.RemoteEndPoint)
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
private async void OnPluginUpdated(object sender, InstallationInfo e)
2014-08-10 22:13:17 +00:00
{
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("PluginUpdatedWithName"),
e.Name),
2020-05-02 22:32:22 +00:00
NotificationType.PluginUpdateInstalled.ToString(),
2020-05-13 19:03:35 +00:00
Guid.Empty)
2020-05-02 22:32:22 +00:00
{
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("VersionNumber"),
e.Version),
Overview = e.Changelog
2020-05-02 22:32:22 +00:00
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
private async void OnPluginUninstalled(object sender, IPlugin e)
2014-08-10 22:13:17 +00:00
{
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("PluginUninstalledWithName"),
e.Name),
2020-05-02 22:32:22 +00:00
NotificationType.PluginUninstalled.ToString(),
2020-05-13 19:03:35 +00:00
Guid.Empty))
2020-05-02 22:32:22 +00:00
.ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
private async void OnPluginInstalled(object sender, InstallationInfo e)
2014-08-10 22:13:17 +00:00
{
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("PluginInstalledWithName"),
e.Name),
2020-05-02 22:32:22 +00:00
NotificationType.PluginInstalled.ToString(),
2020-05-13 19:03:35 +00:00
Guid.Empty)
2020-05-02 22:32:22 +00:00
{
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("VersionNumber"),
e.Version)
2020-05-02 22:32:22 +00:00
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
private async void OnPackageInstallationFailed(object sender, InstallationFailedEventArgs e)
2014-08-10 22:13:17 +00:00
{
2018-09-12 17:26:21 +00:00
var installationInfo = e.InstallationInfo;
2014-08-10 22:13:17 +00:00
2020-05-02 22:32:22 +00:00
await CreateLogEntry(new ActivityLog(
string.Format(
2019-08-29 20:28:33 +00:00
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("NameInstallFailed"),
installationInfo.Name),
2020-05-02 22:32:22 +00:00
NotificationType.InstallationFailed.ToString(),
2020-05-13 19:03:35 +00:00
Guid.Empty)
2020-05-02 22:32:22 +00:00
{
2019-08-29 20:28:33 +00:00
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("VersionNumber"),
installationInfo.Version),
2018-09-12 17:26:21 +00:00
Overview = e.Exception.Message
2020-05-02 22:32:22 +00:00
}).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
2020-05-02 22:32:22 +00:00
private async Task CreateLogEntry(ActivityLog entry)
=> await _activityManager.CreateAsync(entry).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
2020-02-08 21:25:44 +00:00
/// <inheritdoc />
2014-08-10 22:13:17 +00:00
public void Dispose()
{
2019-02-28 21:33:20 +00:00
_installationManager.PluginInstalled -= OnPluginInstalled;
_installationManager.PluginUninstalled -= OnPluginUninstalled;
_installationManager.PluginUpdated -= OnPluginUpdated;
_installationManager.PackageInstallationFailed -= OnPackageInstallationFailed;
2014-08-10 22:13:17 +00:00
2019-02-28 21:33:20 +00:00
_sessionManager.SessionStarted -= OnSessionStarted;
_sessionManager.AuthenticationFailed -= OnAuthenticationFailed;
_sessionManager.AuthenticationSucceeded -= OnAuthenticationSucceeded;
_sessionManager.SessionEnded -= OnSessionEnded;
2014-08-10 22:13:17 +00:00
2020-05-13 02:10:35 +00:00
_userManager.OnUserPasswordChanged -= OnUserPasswordChanged;
_userManager.OnUserLockedOut -= OnUserLockedOut;
2014-08-10 22:13:17 +00:00
}
}
}