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