DeviceManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using Jellyfin.Data.Enums;
  8. using Jellyfin.Data.Entities;
  9. using MediaBrowser.Common.Extensions;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Devices;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.Security;
  14. using MediaBrowser.Model.Devices;
  15. using MediaBrowser.Model.Events;
  16. using MediaBrowser.Model.Querying;
  17. using MediaBrowser.Model.Serialization;
  18. using MediaBrowser.Model.Session;
  19. namespace Emby.Server.Implementations.Devices
  20. {
  21. public class DeviceManager : IDeviceManager
  22. {
  23. private readonly IJsonSerializer _json;
  24. private readonly IUserManager _userManager;
  25. private readonly IServerConfigurationManager _config;
  26. private readonly IAuthenticationRepository _authRepo;
  27. private readonly Dictionary<string, ClientCapabilities> _capabilitiesCache;
  28. private readonly object _capabilitiesSyncLock = new object();
  29. public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated;
  30. public DeviceManager(
  31. IAuthenticationRepository authRepo,
  32. IJsonSerializer json,
  33. IUserManager userManager,
  34. IServerConfigurationManager config)
  35. {
  36. _json = json;
  37. _userManager = userManager;
  38. _config = config;
  39. _authRepo = authRepo;
  40. _capabilitiesCache = new Dictionary<string, ClientCapabilities>(StringComparer.OrdinalIgnoreCase);
  41. }
  42. public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
  43. {
  44. var path = Path.Combine(GetDevicePath(deviceId), "capabilities.json");
  45. Directory.CreateDirectory(Path.GetDirectoryName(path));
  46. lock (_capabilitiesSyncLock)
  47. {
  48. _capabilitiesCache[deviceId] = capabilities;
  49. _json.SerializeToFile(capabilities, path);
  50. }
  51. }
  52. public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
  53. {
  54. _authRepo.UpdateDeviceOptions(deviceId, options);
  55. DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, options)));
  56. }
  57. public DeviceOptions GetDeviceOptions(string deviceId)
  58. {
  59. return _authRepo.GetDeviceOptions(deviceId);
  60. }
  61. public ClientCapabilities GetCapabilities(string id)
  62. {
  63. lock (_capabilitiesSyncLock)
  64. {
  65. if (_capabilitiesCache.TryGetValue(id, out var result))
  66. {
  67. return result;
  68. }
  69. var path = Path.Combine(GetDevicePath(id), "capabilities.json");
  70. try
  71. {
  72. return _json.DeserializeFromFile<ClientCapabilities>(path) ?? new ClientCapabilities();
  73. }
  74. catch
  75. {
  76. }
  77. }
  78. return new ClientCapabilities();
  79. }
  80. public DeviceInfo GetDevice(string id)
  81. {
  82. return GetDevice(id, true);
  83. }
  84. private DeviceInfo GetDevice(string id, bool includeCapabilities)
  85. {
  86. var session = _authRepo.Get(new AuthenticationInfoQuery
  87. {
  88. DeviceId = id
  89. }).Items.FirstOrDefault();
  90. var device = session == null ? null : ToDeviceInfo(session);
  91. return device;
  92. }
  93. public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
  94. {
  95. IEnumerable<AuthenticationInfo> sessions = _authRepo.Get(new AuthenticationInfoQuery
  96. {
  97. // UserId = query.UserId
  98. HasUser = true
  99. }).Items;
  100. // TODO: DeviceQuery doesn't seem to be used from client. Not even Swagger.
  101. if (query.SupportsSync.HasValue)
  102. {
  103. var val = query.SupportsSync.Value;
  104. sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == val);
  105. }
  106. if (!query.UserId.Equals(Guid.Empty))
  107. {
  108. var user = _userManager.GetUserById(query.UserId);
  109. sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
  110. }
  111. var array = sessions.Select(ToDeviceInfo).ToArray();
  112. return new QueryResult<DeviceInfo>(array);
  113. }
  114. private DeviceInfo ToDeviceInfo(AuthenticationInfo authInfo)
  115. {
  116. var caps = GetCapabilities(authInfo.DeviceId);
  117. return new DeviceInfo
  118. {
  119. AppName = authInfo.AppName,
  120. AppVersion = authInfo.AppVersion,
  121. Id = authInfo.DeviceId,
  122. LastUserId = authInfo.UserId,
  123. LastUserName = authInfo.UserName,
  124. Name = authInfo.DeviceName,
  125. DateLastActivity = authInfo.DateLastActivity,
  126. IconUrl = caps?.IconUrl
  127. };
  128. }
  129. private string GetDevicesPath()
  130. {
  131. return Path.Combine(_config.ApplicationPaths.DataPath, "devices");
  132. }
  133. private string GetDevicePath(string id)
  134. {
  135. return Path.Combine(GetDevicesPath(), id.GetMD5().ToString("N", CultureInfo.InvariantCulture));
  136. }
  137. public bool CanAccessDevice(User user, string deviceId)
  138. {
  139. if (user == null)
  140. {
  141. throw new ArgumentException("user not found");
  142. }
  143. if (string.IsNullOrEmpty(deviceId))
  144. {
  145. throw new ArgumentNullException(nameof(deviceId));
  146. }
  147. if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
  148. {
  149. return true;
  150. }
  151. if (!user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase))
  152. {
  153. var capabilities = GetCapabilities(deviceId);
  154. if (capabilities != null && capabilities.SupportsPersistentIdentifier)
  155. {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. }
  162. }