DeviceManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 MediaBrowser.Common.Extensions;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Devices;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.Security;
  13. using MediaBrowser.Model.Devices;
  14. using MediaBrowser.Model.Events;
  15. using MediaBrowser.Model.Querying;
  16. using MediaBrowser.Model.Serialization;
  17. using MediaBrowser.Model.Session;
  18. using MediaBrowser.Model.Users;
  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. public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated;
  29. private readonly object _capabilitiesSyncLock = new object();
  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. if (DeviceOptionsUpdated != null)
  56. {
  57. DeviceOptionsUpdated(this, new GenericEventArgs<Tuple<string, DeviceOptions>>()
  58. {
  59. Argument = new Tuple<string, DeviceOptions>(deviceId, options)
  60. });
  61. }
  62. }
  63. public DeviceOptions GetDeviceOptions(string deviceId)
  64. {
  65. return _authRepo.GetDeviceOptions(deviceId);
  66. }
  67. public ClientCapabilities GetCapabilities(string id)
  68. {
  69. lock (_capabilitiesSyncLock)
  70. {
  71. if (_capabilitiesCache.TryGetValue(id, out var result))
  72. {
  73. return result;
  74. }
  75. var path = Path.Combine(GetDevicePath(id), "capabilities.json");
  76. try
  77. {
  78. return _json.DeserializeFromFile<ClientCapabilities>(path) ?? new ClientCapabilities();
  79. }
  80. catch
  81. {
  82. }
  83. }
  84. return new ClientCapabilities();
  85. }
  86. public DeviceInfo GetDevice(string id)
  87. {
  88. return GetDevice(id, true);
  89. }
  90. private DeviceInfo GetDevice(string id, bool includeCapabilities)
  91. {
  92. var session = _authRepo.Get(new AuthenticationInfoQuery
  93. {
  94. DeviceId = id
  95. }).Items.FirstOrDefault();
  96. var device = session == null ? null : ToDeviceInfo(session);
  97. return device;
  98. }
  99. public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
  100. {
  101. IEnumerable<AuthenticationInfo> sessions = _authRepo.Get(new AuthenticationInfoQuery
  102. {
  103. //UserId = query.UserId
  104. HasUser = true
  105. }).Items;
  106. // TODO: DeviceQuery doesn't seem to be used from client. Not even Swagger.
  107. if (query.SupportsSync.HasValue)
  108. {
  109. var val = query.SupportsSync.Value;
  110. sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == val);
  111. }
  112. if (!query.UserId.Equals(Guid.Empty))
  113. {
  114. var user = _userManager.GetUserById(query.UserId);
  115. sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
  116. }
  117. var array = sessions.Select(ToDeviceInfo).ToArray();
  118. return new QueryResult<DeviceInfo>(array);
  119. }
  120. private DeviceInfo ToDeviceInfo(AuthenticationInfo authInfo)
  121. {
  122. var caps = GetCapabilities(authInfo.DeviceId);
  123. return new DeviceInfo
  124. {
  125. AppName = authInfo.AppName,
  126. AppVersion = authInfo.AppVersion,
  127. Id = authInfo.DeviceId,
  128. LastUserId = authInfo.UserId,
  129. LastUserName = authInfo.UserName,
  130. Name = authInfo.DeviceName,
  131. DateLastActivity = authInfo.DateLastActivity,
  132. IconUrl = caps?.IconUrl
  133. };
  134. }
  135. private string GetDevicesPath()
  136. {
  137. return Path.Combine(_config.ApplicationPaths.DataPath, "devices");
  138. }
  139. private string GetDevicePath(string id)
  140. {
  141. return Path.Combine(GetDevicesPath(), id.GetMD5().ToString("N", CultureInfo.InvariantCulture));
  142. }
  143. public bool CanAccessDevice(User user, string deviceId)
  144. {
  145. if (user == null)
  146. {
  147. throw new ArgumentException("user not found");
  148. }
  149. if (string.IsNullOrEmpty(deviceId))
  150. {
  151. throw new ArgumentNullException(nameof(deviceId));
  152. }
  153. if (!CanAccessDevice(user.Policy, deviceId))
  154. {
  155. var capabilities = GetCapabilities(deviceId);
  156. if (capabilities != null && capabilities.SupportsPersistentIdentifier)
  157. {
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. private static bool CanAccessDevice(UserPolicy policy, string id)
  164. {
  165. if (policy.EnableAllDevices)
  166. {
  167. return true;
  168. }
  169. if (policy.IsAdministrator)
  170. {
  171. return true;
  172. }
  173. return policy.EnabledDevices.Contains(id, StringComparer.OrdinalIgnoreCase);
  174. }
  175. }
  176. }