| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Jellyfin.Data; |
| | | 5 | | using Jellyfin.Data.Events; |
| | | 6 | | using Jellyfin.Data.Queries; |
| | | 7 | | using Jellyfin.Database.Implementations.Entities; |
| | | 8 | | using Jellyfin.Database.Implementations.Enums; |
| | | 9 | | using MediaBrowser.Controller.Devices; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using MediaBrowser.Controller.Session; |
| | | 12 | | using Microsoft.Extensions.Hosting; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Server.Implementations.Users; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// <see cref="IHostedService"/> responsible for managing user device permissions. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class DeviceAccessHost : IHostedService |
| | | 21 | | { |
| | | 22 | | private readonly IUserManager _userManager; |
| | | 23 | | private readonly IDeviceManager _deviceManager; |
| | | 24 | | private readonly ISessionManager _sessionManager; |
| | | 25 | | private readonly ILogger<DeviceAccessHost> _logger; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="DeviceAccessHost"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="userManager">The <see cref="IUserManager"/>.</param> |
| | | 31 | | /// <param name="deviceManager">The <see cref="IDeviceManager"/>.</param> |
| | | 32 | | /// <param name="sessionManager">The <see cref="ISessionManager"/>.</param> |
| | | 33 | | /// <param name="logger">The <see cref="ILogger{TCategoryName}"/>.</param> |
| | | 34 | | public DeviceAccessHost( |
| | | 35 | | IUserManager userManager, |
| | | 36 | | IDeviceManager deviceManager, |
| | | 37 | | ISessionManager sessionManager, |
| | | 38 | | ILogger<DeviceAccessHost> logger) |
| | | 39 | | { |
| | 29 | 40 | | _userManager = userManager; |
| | 29 | 41 | | _deviceManager = deviceManager; |
| | 29 | 42 | | _sessionManager = sessionManager; |
| | 29 | 43 | | _logger = logger; |
| | 29 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public Task StartAsync(CancellationToken cancellationToken) |
| | | 48 | | { |
| | 29 | 49 | | _userManager.OnUserUpdated += OnUserUpdated; |
| | | 50 | | |
| | 29 | 51 | | return Task.CompletedTask; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public Task StopAsync(CancellationToken cancellationToken) |
| | | 56 | | { |
| | 27 | 57 | | _userManager.OnUserUpdated -= OnUserUpdated; |
| | | 58 | | |
| | 27 | 59 | | return Task.CompletedTask; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private async void OnUserUpdated(object? sender, GenericEventArgs<User> e) |
| | | 63 | | { |
| | 6 | 64 | | var user = e.Argument; |
| | | 65 | | |
| | | 66 | | // This handler is async void, so an escaping exception would terminate the process. |
| | | 67 | | try |
| | | 68 | | { |
| | 6 | 69 | | if (!user.HasPermission(PermissionKind.EnableAllDevices)) |
| | | 70 | | { |
| | 2 | 71 | | await UpdateDeviceAccess(user).ConfigureAwait(false); |
| | | 72 | | } |
| | 5 | 73 | | } |
| | 1 | 74 | | catch (Exception ex) |
| | | 75 | | { |
| | 1 | 76 | | _logger.LogError(ex, "Error updating device access for user {UserId}", user.Id); |
| | 1 | 77 | | } |
| | 6 | 78 | | } |
| | | 79 | | |
| | | 80 | | private async Task UpdateDeviceAccess(User user) |
| | | 81 | | { |
| | 2 | 82 | | var existing = _deviceManager.GetDevices(new DeviceQuery |
| | 2 | 83 | | { |
| | 2 | 84 | | UserId = user.Id |
| | 2 | 85 | | }).Items; |
| | | 86 | | |
| | 7 | 87 | | foreach (var device in existing) |
| | | 88 | | { |
| | 2 | 89 | | if (!string.IsNullOrEmpty(device.DeviceId) && !_deviceManager.CanAccessDevice(user, device.DeviceId)) |
| | | 90 | | { |
| | 2 | 91 | | await _sessionManager.Logout(device).ConfigureAwait(false); |
| | | 92 | | } |
| | | 93 | | } |
| | 1 | 94 | | } |
| | | 95 | | } |