< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Users.DeviceAccessHost
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 8
Coverable lines: 8
Total lines: 76
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
StartAsync(...)100%210%
StopAsync(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs

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