2019-11-01 17:38:54 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2014-10-11 20:38:13 +00:00
|
|
|
using System;
|
2020-11-19 14:38:54 +00:00
|
|
|
using System.Collections.Concurrent;
|
2014-10-11 20:38:13 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-05-15 21:20:07 +00:00
|
|
|
using Jellyfin.Data.Entities;
|
2020-07-29 23:25:47 +00:00
|
|
|
using Jellyfin.Data.Enums;
|
2020-08-14 00:48:28 +00:00
|
|
|
using Jellyfin.Data.Events;
|
2019-01-13 19:20:41 +00:00
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2018-09-12 17:26:21 +00:00
|
|
|
using MediaBrowser.Controller.Security;
|
2019-01-13 19:20:41 +00:00
|
|
|
using MediaBrowser.Model.Devices;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
using MediaBrowser.Model.Session;
|
2014-10-11 20:38:13 +00:00
|
|
|
|
2016-11-03 07:14:14 +00:00
|
|
|
namespace Emby.Server.Implementations.Devices
|
2014-10-11 20:38:13 +00:00
|
|
|
{
|
|
|
|
public class DeviceManager : IDeviceManager
|
|
|
|
{
|
|
|
|
private readonly IUserManager _userManager;
|
2018-09-12 17:26:21 +00:00
|
|
|
private readonly IAuthenticationRepository _authRepo;
|
2020-11-19 14:38:54 +00:00
|
|
|
private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new ();
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-11-19 14:38:54 +00:00
|
|
|
public DeviceManager(IAuthenticationRepository authRepo, IUserManager userManager)
|
2014-10-11 20:38:13 +00:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
2018-09-12 17:26:21 +00:00
|
|
|
_authRepo = authRepo;
|
2014-10-11 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:38:54 +00:00
|
|
|
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
|
2014-10-11 20:38:13 +00:00
|
|
|
{
|
2020-11-19 14:38:54 +00:00
|
|
|
_capabilitiesMap[deviceId] = capabilities;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2014-10-11 20:38:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
|
|
|
|
{
|
|
|
|
_authRepo.UpdateDeviceOptions(deviceId, options);
|
2014-10-11 20:38:13 +00:00
|
|
|
|
2020-04-05 16:10:56 +00:00
|
|
|
DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, options)));
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2014-10-11 20:38:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public DeviceOptions GetDeviceOptions(string deviceId)
|
|
|
|
{
|
|
|
|
return _authRepo.GetDeviceOptions(deviceId);
|
|
|
|
}
|
2014-10-11 20:38:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public ClientCapabilities GetCapabilities(string id)
|
|
|
|
{
|
2020-11-19 14:38:54 +00:00
|
|
|
return _capabilitiesMap.TryGetValue(id, out ClientCapabilities result)
|
|
|
|
? result
|
|
|
|
: new ClientCapabilities();
|
2014-10-11 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public DeviceInfo GetDevice(string id)
|
2014-10-11 20:38:13 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var session = _authRepo.Get(new AuthenticationInfoQuery
|
|
|
|
{
|
|
|
|
DeviceId = id
|
|
|
|
}).Items.FirstOrDefault();
|
|
|
|
|
|
|
|
var device = session == null ? null : ToDeviceInfo(session);
|
|
|
|
|
|
|
|
return device;
|
2014-10-11 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 03:56:30 +00:00
|
|
|
public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
|
2014-10-11 20:38:13 +00:00
|
|
|
{
|
2020-02-19 20:56:35 +00:00
|
|
|
IEnumerable<AuthenticationInfo> sessions = _authRepo.Get(new AuthenticationInfoQuery
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-06-14 09:11:11 +00:00
|
|
|
// UserId = query.UserId
|
2018-09-12 17:26:21 +00:00
|
|
|
HasUser = true
|
|
|
|
}).Items;
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2019-01-02 18:13:35 +00:00
|
|
|
// TODO: DeviceQuery doesn't seem to be used from client. Not even Swagger.
|
2014-12-15 05:49:04 +00:00
|
|
|
if (query.SupportsSync.HasValue)
|
|
|
|
{
|
|
|
|
var val = query.SupportsSync.Value;
|
|
|
|
|
2020-02-19 20:56:35 +00:00
|
|
|
sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == val);
|
2014-12-15 05:49:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!query.UserId.Equals(Guid.Empty))
|
2014-12-13 03:56:30 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var user = _userManager.GetUserById(query.UserId);
|
2014-12-13 03:56:30 +00:00
|
|
|
|
2020-02-19 20:56:35 +00:00
|
|
|
sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
|
2014-12-31 06:24:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var array = sessions.Select(ToDeviceInfo).ToArray();
|
2015-01-26 16:47:15 +00:00
|
|
|
|
2020-02-19 20:56:35 +00:00
|
|
|
return new QueryResult<DeviceInfo>(array);
|
2014-10-11 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private DeviceInfo ToDeviceInfo(AuthenticationInfo authInfo)
|
|
|
|
{
|
|
|
|
var caps = GetCapabilities(authInfo.DeviceId);
|
|
|
|
|
|
|
|
return new DeviceInfo
|
|
|
|
{
|
|
|
|
AppName = authInfo.AppName,
|
|
|
|
AppVersion = authInfo.AppVersion,
|
|
|
|
Id = authInfo.DeviceId,
|
|
|
|
LastUserId = authInfo.UserId,
|
|
|
|
LastUserName = authInfo.UserName,
|
|
|
|
Name = authInfo.DeviceName,
|
|
|
|
DateLastActivity = authInfo.DateLastActivity,
|
2020-02-19 20:56:35 +00:00
|
|
|
IconUrl = caps?.IconUrl
|
2018-09-12 17:26:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanAccessDevice(User user, string deviceId)
|
2014-12-29 20:18:48 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (user == null)
|
2014-12-29 20:18:48 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
throw new ArgumentException("user not found");
|
2014-12-29 20:18:48 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(deviceId))
|
2014-12-29 20:18:48 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(deviceId));
|
2014-12-29 20:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 23:47:41 +00:00
|
|
|
if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
|
2020-05-13 02:10:35 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase))
|
2014-12-29 20:18:48 +00:00
|
|
|
{
|
|
|
|
var capabilities = GetCapabilities(deviceId);
|
|
|
|
|
2015-01-20 05:19:13 +00:00
|
|
|
if (capabilities != null && capabilities.SupportsPersistentIdentifier)
|
2014-12-29 20:18:48 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2018-12-28 15:48:26 +00:00
|
|
|
}
|