jellyfin/Jellyfin.Server.Implementations/Users/DeviceAccessEntryPoint.cs

68 lines
2.0 KiB
C#
Raw Normal View History

2020-06-09 16:21:21 +00:00
#nullable enable
#pragma warning disable CS1591
2020-05-13 02:10:35 +00:00
using System.Threading.Tasks;
2020-05-15 21:24:01 +00:00
using Jellyfin.Data.Entities;
2020-05-13 02:10:35 +00:00
using Jellyfin.Data.Enums;
using Jellyfin.Data.Events;
2020-05-13 02:10:35 +00:00
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
2020-05-15 21:24:01 +00:00
namespace Jellyfin.Server.Implementations.Users
2020-05-13 02:10:35 +00:00
{
public sealed class DeviceAccessEntryPoint : IServerEntryPoint
{
private readonly IUserManager _userManager;
private readonly IAuthenticationRepository _authRepo;
private readonly IDeviceManager _deviceManager;
private readonly ISessionManager _sessionManager;
public DeviceAccessEntryPoint(IUserManager userManager, IAuthenticationRepository authRepo, IDeviceManager deviceManager, ISessionManager sessionManager)
{
_userManager = userManager;
_authRepo = authRepo;
_deviceManager = deviceManager;
_sessionManager = sessionManager;
}
public Task RunAsync()
{
_userManager.OnUserUpdated += OnUserUpdated;
return Task.CompletedTask;
}
2020-05-15 21:24:01 +00:00
public void Dispose()
{
}
2020-06-09 16:21:21 +00:00
private void OnUserUpdated(object? sender, GenericEventArgs<User> e)
2020-05-13 02:10:35 +00:00
{
var user = e.Argument;
if (!user.HasPermission(PermissionKind.EnableAllDevices))
{
UpdateDeviceAccess(user);
}
}
2020-05-15 21:24:01 +00:00
private void UpdateDeviceAccess(User user)
2020-05-13 02:10:35 +00:00
{
var existing = _authRepo.Get(new AuthenticationInfoQuery
{
UserId = user.Id
}).Items;
foreach (var authInfo in existing)
{
if (!string.IsNullOrEmpty(authInfo.DeviceId) && !_deviceManager.CanAccessDevice(user, authInfo.DeviceId))
{
_sessionManager.Logout(authInfo);
}
}
}
}
}