jellyfin/Jellyfin.Server.Implementations/Users/DeviceAccessEntryPoint.cs

67 lines
2.0 KiB
C#
Raw Normal View History

2020-05-12 19:10:35 -07:00
#pragma warning disable CS1591
using System.Threading.Tasks;
2020-05-15 14:24:01 -07:00
using Jellyfin.Data.Entities;
2020-05-12 19:10:35 -07:00
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Events;
2020-05-15 14:24:01 -07:00
namespace Jellyfin.Server.Implementations.Users
2020-05-12 19:10:35 -07:00
{
public sealed class DeviceAccessEntryPoint : IServerEntryPoint
{
private readonly IUserManager _userManager;
private readonly IAuthenticationRepository _authRepo;
private readonly IDeviceManager _deviceManager;
private readonly ISessionManager _sessionManager;
public DeviceAccessEntryPoint(IUserManager userManager, IAuthenticationRepository authRepo, IDeviceManager deviceManager, ISessionManager sessionManager)
{
_userManager = userManager;
_authRepo = authRepo;
_deviceManager = deviceManager;
_sessionManager = sessionManager;
}
public Task RunAsync()
{
_userManager.OnUserUpdated += OnUserUpdated;
return Task.CompletedTask;
}
2020-05-15 14:24:01 -07:00
public void Dispose()
{
}
private void OnUserUpdated(object sender, GenericEventArgs<User> e)
2020-05-12 19:10:35 -07:00
{
var user = e.Argument;
if (!user.HasPermission(PermissionKind.EnableAllDevices))
{
UpdateDeviceAccess(user);
}
}
2020-05-15 14:24:01 -07:00
private void UpdateDeviceAccess(User user)
2020-05-12 19:10:35 -07:00
{
var existing = _authRepo.Get(new AuthenticationInfoQuery
{
UserId = user.Id
}).Items;
foreach (var authInfo in existing)
{
if (!string.IsNullOrEmpty(authInfo.DeviceId) && !_deviceManager.CanAccessDevice(user, authInfo.DeviceId))
{
_sessionManager.Logout(authInfo);
}
}
}
}
}