PluginSecurityManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections.Generic;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Security;
  4. using MediaBrowser.Model.Serialization;
  5. using Mediabrowser.Model.Entities;
  6. using Mediabrowser.PluginSecurity;
  7. using MediaBrowser.Common.Net;
  8. using System;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Linq;
  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").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 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)
  59. {
  60. if (httpClient == null)
  61. {
  62. throw new ArgumentNullException("httpClient");
  63. }
  64. _applciationPaths = appPaths;
  65. _appHost = appHost;
  66. _httpClient = httpClient;
  67. _jsonSerializer = jsonSerializer;
  68. }
  69. /// <summary>
  70. /// Load all registration info for all entities that require registration
  71. /// </summary>
  72. /// <returns></returns>
  73. public async Task LoadAllRegistrationInfo()
  74. {
  75. var tasks = new List<Task>();
  76. ResetSupporterInfo();
  77. tasks.AddRange(RegisteredEntities.Select(i => i.LoadRegistrationInfoAsync()));
  78. await Task.WhenAll(tasks);
  79. }
  80. /// <summary>
  81. /// Gets the registration status.
  82. /// </summary>
  83. /// <param name="feature">The feature.</param>
  84. /// <param name="mb2Equivalent">The MB2 equivalent.</param>
  85. /// <returns>Task{MBRegistrationRecord}.</returns>
  86. public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
  87. {
  88. // Do this on demend instead of in the constructor to delay the external assembly load
  89. // Todo: Refactor external methods to take app paths as a param
  90. MBRegistration.Init(_applciationPaths);
  91. return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false);
  92. }
  93. /// <summary>
  94. /// Gets or sets the supporter key.
  95. /// </summary>
  96. /// <value>The supporter key.</value>
  97. public string SupporterKey
  98. {
  99. get
  100. {
  101. // Do this on demend instead of in the constructor to delay the external assembly load
  102. // Todo: Refactor external methods to take app paths as a param
  103. MBRegistration.Init(_applciationPaths);
  104. return MBRegistration.SupporterKey;
  105. }
  106. set
  107. {
  108. // Do this on demend instead of in the constructor to delay the external assembly load
  109. // Todo: Refactor external methods to take app paths as a param
  110. MBRegistration.Init(_applciationPaths);
  111. if (value != MBRegistration.SupporterKey)
  112. {
  113. MBRegistration.SupporterKey = value;
  114. // re-load registration info
  115. Task.Run(() => LoadAllRegistrationInfo());
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets the legacy key.
  121. /// </summary>
  122. /// <value>The legacy key.</value>
  123. public string LegacyKey
  124. {
  125. get
  126. {
  127. // Do this on demend instead of in the constructor to delay the external assembly load
  128. // Todo: Refactor external methods to take app paths as a param
  129. MBRegistration.Init(_applciationPaths);
  130. return MBRegistration.LegacyKey;
  131. }
  132. set
  133. {
  134. // Do this on demend instead of in the constructor to delay the external assembly load
  135. // Todo: Refactor external methods to take app paths as a param
  136. MBRegistration.Init(_applciationPaths);
  137. if (value != MBRegistration.LegacyKey)
  138. {
  139. MBRegistration.LegacyKey = value;
  140. // re-load registration info
  141. Task.Run(() => LoadAllRegistrationInfo());
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// Resets the supporter info.
  147. /// </summary>
  148. private void ResetSupporterInfo()
  149. {
  150. _isMbSupporter = null;
  151. _isMbSupporterInitialized = false;
  152. }
  153. }
  154. }