< 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: 77
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;
 4using Jellyfin.Data.Events;
 5using Jellyfin.Data.Queries;
 6using Jellyfin.Database.Implementations.Entities;
 7using Jellyfin.Database.Implementations.Enums;
 8using MediaBrowser.Controller.Devices;
 9using MediaBrowser.Controller.Library;
 10using MediaBrowser.Controller.Session;
 11using Microsoft.Extensions.Hosting;
 12
 13namespace Jellyfin.Server.Implementations.Users;
 14
 15/// <summary>
 16/// <see cref="IHostedService"/> responsible for managing user device permissions.
 17/// </summary>
 18public 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    {
 032        _userManager = userManager;
 033        _deviceManager = deviceManager;
 034        _sessionManager = sessionManager;
 035    }
 36
 37    /// <inheritdoc />
 38    public Task StartAsync(CancellationToken cancellationToken)
 39    {
 040        _userManager.OnUserUpdated += OnUserUpdated;
 41
 042        return Task.CompletedTask;
 43    }
 44
 45    /// <inheritdoc />
 46    public Task StopAsync(CancellationToken cancellationToken)
 47    {
 048        _userManager.OnUserUpdated -= OnUserUpdated;
 49
 050        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}