PluginSecurityManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 IHttpClient _httpClient;
  44. private IJsonSerializer _jsonSerializer;
  45. private IApplicationHost _appHost;
  46. private IEnumerable<IRequiresRegistration> _registeredEntities;
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
  49. /// </summary>
  50. public PluginSecurityManager(IApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, IApplicationPaths appPaths)
  51. {
  52. if (httpClient == null)
  53. {
  54. throw new ArgumentNullException("httpClient");
  55. }
  56. _appHost = appHost;
  57. _httpClient = httpClient;
  58. _jsonSerializer = jsonSerializer;
  59. _registeredEntities = _appHost.GetExports<IRequiresRegistration>();
  60. MBRegistration.Init(appPaths);
  61. }
  62. /// <summary>
  63. /// Load all registration info for all entities that require registration
  64. /// </summary>
  65. /// <returns></returns>
  66. public async Task LoadAllRegistrationInfo()
  67. {
  68. var tasks = new List<Task>();
  69. ResetSupporterInfo();
  70. tasks.AddRange(_registeredEntities.Select(i => i.LoadRegistrationInfoAsync()));
  71. await Task.WhenAll(tasks);
  72. }
  73. /// <summary>
  74. /// Gets the registration status.
  75. /// </summary>
  76. /// <param name="feature">The feature.</param>
  77. /// <param name="mb2Equivalent">The MB2 equivalent.</param>
  78. /// <returns>Task{MBRegistrationRecord}.</returns>
  79. public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
  80. {
  81. return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false);
  82. }
  83. /// <summary>
  84. /// Gets or sets the supporter key.
  85. /// </summary>
  86. /// <value>The supporter key.</value>
  87. public string SupporterKey
  88. {
  89. get { return MBRegistration.SupporterKey; }
  90. set
  91. {
  92. if (value != MBRegistration.SupporterKey)
  93. {
  94. MBRegistration.SupporterKey = value;
  95. // re-load registration info
  96. Task.Run(() => LoadAllRegistrationInfo());
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Gets or sets the legacy key.
  102. /// </summary>
  103. /// <value>The legacy key.</value>
  104. public string LegacyKey
  105. {
  106. get { return MBRegistration.LegacyKey; }
  107. set
  108. {
  109. if (value != MBRegistration.LegacyKey)
  110. {
  111. MBRegistration.LegacyKey = value;
  112. // re-load registration info
  113. Task.Run(() => LoadAllRegistrationInfo());
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// Resets the supporter info.
  119. /// </summary>
  120. private void ResetSupporterInfo()
  121. {
  122. _isMBSupporter = null;
  123. _isMBSupporterInitialized = false;
  124. }
  125. }
  126. }