PluginSecurityManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Security;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Common.Implementations.Security
  13. {
  14. /// <summary>
  15. /// Class PluginSecurityManager
  16. /// </summary>
  17. public class PluginSecurityManager : ISecurityManager
  18. {
  19. /// <summary>
  20. /// The _is MB supporter
  21. /// </summary>
  22. private bool? _isMbSupporter;
  23. /// <summary>
  24. /// The _is MB supporter initialized
  25. /// </summary>
  26. private bool _isMbSupporterInitialized;
  27. /// <summary>
  28. /// The _is MB supporter sync lock
  29. /// </summary>
  30. private object _isMbSupporterSyncLock = new object();
  31. /// <summary>
  32. /// Gets a value indicating whether this instance is MB supporter.
  33. /// </summary>
  34. /// <value><c>true</c> if this instance is MB supporter; otherwise, <c>false</c>.</value>
  35. public bool IsMBSupporter
  36. {
  37. get
  38. {
  39. LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetRegistrationStatus("MBSupporter", null, _appHost.ApplicationVersion.ToString()).Result.IsRegistered);
  40. return _isMbSupporter.Value;
  41. }
  42. }
  43. private readonly IHttpClient _httpClient;
  44. private readonly IJsonSerializer _jsonSerializer;
  45. private readonly IApplicationHost _appHost;
  46. private readonly IApplicationPaths _applciationPaths;
  47. private readonly INetworkManager _networkManager;
  48. private IEnumerable<IRequiresRegistration> _registeredEntities;
  49. protected IEnumerable<IRequiresRegistration> RegisteredEntities
  50. {
  51. get
  52. {
  53. return _registeredEntities ?? (_registeredEntities = _appHost.GetExports<IRequiresRegistration>());
  54. }
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
  58. /// </summary>
  59. public PluginSecurityManager(IApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer,
  60. IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager)
  61. {
  62. if (httpClient == null)
  63. {
  64. throw new ArgumentNullException("httpClient");
  65. }
  66. _applciationPaths = appPaths;
  67. _networkManager = networkManager;
  68. _appHost = appHost;
  69. _httpClient = httpClient;
  70. _jsonSerializer = jsonSerializer;
  71. MBRegistration.Init(_applciationPaths, _networkManager, logManager);
  72. }
  73. /// <summary>
  74. /// Load all registration info for all entities that require registration
  75. /// </summary>
  76. /// <returns></returns>
  77. public async Task LoadAllRegistrationInfo()
  78. {
  79. var tasks = new List<Task>();
  80. ResetSupporterInfo();
  81. tasks.AddRange(RegisteredEntities.Select(i => i.LoadRegistrationInfoAsync()));
  82. await Task.WhenAll(tasks);
  83. }
  84. /// <summary>
  85. /// Gets the registration status.
  86. /// </summary>
  87. /// <param name="feature">The feature.</param>
  88. /// <param name="mb2Equivalent">The MB2 equivalent.</param>
  89. /// <param name="version">The version of this feature</param>
  90. /// <returns>Task{MBRegistrationRecord}.</returns>
  91. public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null, string version = null)
  92. {
  93. return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent, version).ConfigureAwait(false);
  94. }
  95. /// <summary>
  96. /// Gets or sets the supporter key.
  97. /// </summary>
  98. /// <value>The supporter key.</value>
  99. public string SupporterKey
  100. {
  101. get
  102. {
  103. return MBRegistration.SupporterKey;
  104. }
  105. set
  106. {
  107. if (value != MBRegistration.SupporterKey)
  108. {
  109. MBRegistration.SupporterKey = value;
  110. // re-load registration info
  111. Task.Run(() => LoadAllRegistrationInfo());
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets the legacy key.
  117. /// </summary>
  118. /// <value>The legacy key.</value>
  119. public string LegacyKey
  120. {
  121. get
  122. {
  123. return MBRegistration.LegacyKey;
  124. }
  125. set
  126. {
  127. if (value != MBRegistration.LegacyKey)
  128. {
  129. MBRegistration.LegacyKey = value;
  130. // re-load registration info
  131. Task.Run(() => LoadAllRegistrationInfo());
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Resets the supporter info.
  137. /// </summary>
  138. private void ResetSupporterInfo()
  139. {
  140. _isMbSupporter = null;
  141. _isMbSupporterInitialized = false;
  142. }
  143. }
  144. }