2019-11-01 13:38:54 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-02-28 17:22:57 -05:00
|
|
|
using System.Globalization;
|
2014-10-11 16:38:13 -04:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-05-12 22:10:35 -04:00
|
|
|
using Jellyfin.Data.Enums;
|
2020-05-15 17:20:07 -04:00
|
|
|
using Jellyfin.Data.Entities;
|
2019-01-13 14:20:41 -05:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2015-11-02 23:34:47 -05:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-01-13 14:20:41 -05:00
|
|
|
using MediaBrowser.Controller.Devices;
|
2018-09-12 13:26:21 -04:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2019-01-13 14:20:41 -05:00
|
|
|
using MediaBrowser.Controller.Library;
|
2018-09-12 13:26:21 -04:00
|
|
|
using MediaBrowser.Controller.Security;
|
2019-01-13 14:20:41 -05:00
|
|
|
using MediaBrowser.Model.Devices;
|
|
|
|
using MediaBrowser.Model.Events;
|
|
|
|
using MediaBrowser.Model.Querying;
|
2018-09-12 13:26:21 -04:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-01-13 14:20:41 -05:00
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
using MediaBrowser.Model.Users;
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2016-11-03 03:14:14 -04:00
|
|
|
namespace Emby.Server.Implementations.Devices
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
|
|
|
public class DeviceManager : IDeviceManager
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
private readonly IJsonSerializer _json;
|
2014-10-11 16:38:13 -04:00
|
|
|
private readonly IUserManager _userManager;
|
2015-11-02 23:34:47 -05:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2018-09-12 13:26:21 -04:00
|
|
|
private readonly IAuthenticationRepository _authRepo;
|
2020-04-04 15:40:06 -04:00
|
|
|
private readonly Dictionary<string, ClientCapabilities> _capabilitiesCache;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated;
|
2020-04-04 15:40:06 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private readonly object _capabilitiesSyncLock = new object();
|
2014-12-12 22:56:30 -05:00
|
|
|
|
2019-01-17 17:55:05 -05:00
|
|
|
public DeviceManager(
|
|
|
|
IAuthenticationRepository authRepo,
|
|
|
|
IJsonSerializer json,
|
|
|
|
IUserManager userManager,
|
2019-02-06 14:38:42 -05:00
|
|
|
IServerConfigurationManager config)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
_json = json;
|
2014-10-11 16:38:13 -04:00
|
|
|
_userManager = userManager;
|
|
|
|
_config = config;
|
2018-09-12 13:26:21 -04:00
|
|
|
_authRepo = authRepo;
|
2020-04-04 15:40:06 -04:00
|
|
|
_capabilitiesCache = new Dictionary<string, ClientCapabilities>(StringComparer.OrdinalIgnoreCase);
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var path = Path.Combine(GetDevicePath(deviceId), "capabilities.json");
|
2019-01-26 16:08:04 -05:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
2016-05-30 12:22:31 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
lock (_capabilitiesSyncLock)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
_capabilitiesCache[deviceId] = capabilities;
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
_json.SerializeToFile(capabilities, path);
|
|
|
|
}
|
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
|
|
|
|
{
|
|
|
|
_authRepo.UpdateDeviceOptions(deviceId, options);
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (DeviceOptionsUpdated != null)
|
|
|
|
{
|
|
|
|
DeviceOptionsUpdated(this, new GenericEventArgs<Tuple<string, DeviceOptions>>()
|
|
|
|
{
|
|
|
|
Argument = new Tuple<string, DeviceOptions>(deviceId, options)
|
|
|
|
});
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public DeviceOptions GetDeviceOptions(string deviceId)
|
|
|
|
{
|
|
|
|
return _authRepo.GetDeviceOptions(deviceId);
|
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public ClientCapabilities GetCapabilities(string id)
|
|
|
|
{
|
|
|
|
lock (_capabilitiesSyncLock)
|
|
|
|
{
|
2019-01-13 15:46:33 -05:00
|
|
|
if (_capabilitiesCache.TryGetValue(id, out var result))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2017-10-13 15:18:05 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
var path = Path.Combine(GetDevicePath(id), "capabilities.json");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return _json.DeserializeFromFile<ClientCapabilities>(path) ?? new ClientCapabilities();
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 16:14:53 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
return new ClientCapabilities();
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public DeviceInfo GetDevice(string id)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
return GetDevice(id, true);
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private DeviceInfo GetDevice(string id, bool includeCapabilities)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var session = _authRepo.Get(new AuthenticationInfoQuery
|
|
|
|
{
|
|
|
|
DeviceId = id
|
|
|
|
}).Items.FirstOrDefault();
|
|
|
|
|
|
|
|
var device = session == null ? null : ToDeviceInfo(session);
|
|
|
|
|
|
|
|
return device;
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2014-12-12 22:56:30 -05:00
|
|
|
public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2020-02-19 15:56:35 -05:00
|
|
|
IEnumerable<AuthenticationInfo> sessions = _authRepo.Get(new AuthenticationInfoQuery
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
//UserId = query.UserId
|
|
|
|
HasUser = true
|
|
|
|
}).Items;
|
2019-01-07 18:27:46 -05:00
|
|
|
|
2019-01-02 13:13:35 -05:00
|
|
|
// TODO: DeviceQuery doesn't seem to be used from client. Not even Swagger.
|
2014-12-15 00:49:04 -05:00
|
|
|
if (query.SupportsSync.HasValue)
|
|
|
|
{
|
|
|
|
var val = query.SupportsSync.Value;
|
|
|
|
|
2020-02-19 15:56:35 -05:00
|
|
|
sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == val);
|
2014-12-15 00:49:04 -05:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!query.UserId.Equals(Guid.Empty))
|
2014-12-12 22:56:30 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var user = _userManager.GetUserById(query.UserId);
|
2014-12-12 22:56:30 -05:00
|
|
|
|
2020-02-19 15:56:35 -05:00
|
|
|
sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
|
2014-12-31 01:24:49 -05:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
var array = sessions.Select(ToDeviceInfo).ToArray();
|
2015-01-26 11:47:15 -05:00
|
|
|
|
2020-02-19 15:56:35 -05:00
|
|
|
return new QueryResult<DeviceInfo>(array);
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04: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 15:56:35 -05:00
|
|
|
IconUrl = caps?.IconUrl
|
2018-09-12 13:26:21 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetDevicesPath()
|
|
|
|
{
|
|
|
|
return Path.Combine(_config.ApplicationPaths.DataPath, "devices");
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetDevicePath(string id)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2019-02-28 17:22:57 -05:00
|
|
|
return Path.Combine(GetDevicesPath(), id.GetMD5().ToString("N", CultureInfo.InvariantCulture));
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public bool CanAccessDevice(User user, string deviceId)
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
if (user == null)
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
throw new ArgumentException("user not found");
|
2014-12-29 15:18:48 -05:00
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
if (string.IsNullOrEmpty(deviceId))
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
2019-01-06 15:50:43 -05:00
|
|
|
throw new ArgumentNullException(nameof(deviceId));
|
2014-12-29 15:18:48 -05:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:10:35 -04:00
|
|
|
if (user.HasPermission(PermissionKind.EnableAllDevices)
|
|
|
|
|| user.HasPermission(PermissionKind.IsAdministrator))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase))
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
|
|
|
var capabilities = GetCapabilities(deviceId);
|
|
|
|
|
2015-01-20 00:19:13 -05:00
|
|
|
if (capabilities != null && capabilities.SupportsPersistentIdentifier)
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2018-12-28 10:48:26 -05:00
|
|
|
}
|