2
0

PluginSecurityManager.cs 6.2 KB

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