2020-12-12 16:20:48 +00:00
|
|
|
|
#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;
|
2020-08-14 00:48:28 +00:00
|
|
|
|
using Jellyfin.Data.Events;
|
2021-05-21 03:56:59 +00:00
|
|
|
|
using Jellyfin.Data.Queries;
|
2020-05-13 02:10:35 +00:00
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
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 IDeviceManager _deviceManager;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
2021-05-21 03:56:59 +00:00
|
|
|
|
public DeviceAccessEntryPoint(IUserManager userManager, IDeviceManager deviceManager, ISessionManager sessionManager)
|
2020-05-13 02:10:35 +00:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_deviceManager = deviceManager;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task RunAsync()
|
|
|
|
|
{
|
|
|
|
|
_userManager.OnUserUpdated += OnUserUpdated;
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 21:24:01 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 03:56:59 +00:00
|
|
|
|
private async void OnUserUpdated(object? sender, GenericEventArgs<User> e)
|
2020-05-13 02:10:35 +00:00
|
|
|
|
{
|
|
|
|
|
var user = e.Argument;
|
|
|
|
|
if (!user.HasPermission(PermissionKind.EnableAllDevices))
|
|
|
|
|
{
|
2021-05-21 03:56:59 +00:00
|
|
|
|
await UpdateDeviceAccess(user).ConfigureAwait(false);
|
2020-05-13 02:10:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 03:56:59 +00:00
|
|
|
|
private async Task UpdateDeviceAccess(User user)
|
2020-05-13 02:10:35 +00:00
|
|
|
|
{
|
2021-05-21 03:56:59 +00:00
|
|
|
|
var existing = (await _deviceManager.GetDevices(new DeviceQuery
|
2020-05-13 02:10:35 +00:00
|
|
|
|
{
|
|
|
|
|
UserId = user.Id
|
2021-05-21 03:56:59 +00:00
|
|
|
|
}).ConfigureAwait(false)).Items;
|
2020-05-13 02:10:35 +00:00
|
|
|
|
|
2021-05-21 03:56:59 +00:00
|
|
|
|
foreach (var device in existing)
|
2020-05-13 02:10:35 +00:00
|
|
|
|
{
|
2021-05-21 03:56:59 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(device.DeviceId) && !_deviceManager.CanAccessDevice(user, device.DeviceId))
|
2020-05-13 02:10:35 +00:00
|
|
|
|
{
|
2021-05-21 03:56:59 +00:00
|
|
|
|
await _sessionManager.Logout(device).ConfigureAwait(false);
|
2020-05-13 02:10:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|