PluginSecurityManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using MediaBrowser.Common.Security;
  2. using MediaBrowser.Model.Serialization;
  3. using Mediabrowser.Model.Entities;
  4. using Mediabrowser.PluginSecurity;
  5. using MediaBrowser.Common.Kernel;
  6. using MediaBrowser.Common.Net;
  7. using System;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Plugins
  11. {
  12. /// <summary>
  13. /// Class PluginSecurityManager
  14. /// </summary>
  15. public class PluginSecurityManager : ISecurityManager
  16. {
  17. /// <summary>
  18. /// The _is MB supporter
  19. /// </summary>
  20. private bool? _isMBSupporter;
  21. /// <summary>
  22. /// The _is MB supporter initialized
  23. /// </summary>
  24. private bool _isMBSupporterInitialized;
  25. /// <summary>
  26. /// The _is MB supporter sync lock
  27. /// </summary>
  28. private object _isMBSupporterSyncLock = new object();
  29. /// <summary>
  30. /// Gets a value indicating whether this instance is MB supporter.
  31. /// </summary>
  32. /// <value><c>true</c> if this instance is MB supporter; otherwise, <c>false</c>.</value>
  33. public bool IsMBSupporter
  34. {
  35. get
  36. {
  37. LazyInitializer.EnsureInitialized(ref _isMBSupporter, ref _isMBSupporterInitialized, ref _isMBSupporterSyncLock, () => GetRegistrationStatus("MBSupporter").Result.IsRegistered);
  38. return _isMBSupporter.Value;
  39. }
  40. }
  41. private IHttpClient _httpClient;
  42. private IJsonSerializer _jsonSerializer;
  43. /// <summary>
  44. /// The _kernel
  45. /// </summary>
  46. private readonly IKernel _kernel;
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
  49. /// </summary>
  50. /// <param name="kernel">The kernel.</param>
  51. public PluginSecurityManager(IKernel kernel, IHttpClient httpClient, IJsonSerializer jsonSerializer, IApplicationPaths appPaths)
  52. {
  53. if (kernel == null)
  54. {
  55. throw new ArgumentNullException("kernel");
  56. }
  57. if (httpClient == null)
  58. {
  59. throw new ArgumentNullException("httpClient");
  60. }
  61. _kernel = kernel;
  62. _httpClient = httpClient;
  63. _jsonSerializer = jsonSerializer;
  64. MBRegistration.Init(appPaths);
  65. }
  66. /// <summary>
  67. /// Gets the registration status.
  68. /// </summary>
  69. /// <param name="feature">The feature.</param>
  70. /// <param name="mb2Equivalent">The MB2 equivalent.</param>
  71. /// <returns>Task{MBRegistrationRecord}.</returns>
  72. public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
  73. {
  74. return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false);
  75. }
  76. /// <summary>
  77. /// Gets or sets the supporter key.
  78. /// </summary>
  79. /// <value>The supporter key.</value>
  80. public string SupporterKey
  81. {
  82. get { return MBRegistration.SupporterKey; }
  83. set
  84. {
  85. if (value != MBRegistration.SupporterKey)
  86. {
  87. MBRegistration.SupporterKey = value;
  88. // Clear this so it will re-evaluate
  89. ResetSupporterInfo();
  90. // And we'll need to restart to re-evaluate the status of plug-ins
  91. _kernel.NotifyPendingRestart();
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Gets or sets the legacy key.
  97. /// </summary>
  98. /// <value>The legacy key.</value>
  99. public string LegacyKey
  100. {
  101. get { return MBRegistration.LegacyKey; }
  102. set
  103. {
  104. MBRegistration.LegacyKey = value;
  105. // And we'll need to restart to re-evaluate the status of plug-ins
  106. _kernel.NotifyPendingRestart();
  107. }
  108. }
  109. /// <summary>
  110. /// Resets the supporter info.
  111. /// </summary>
  112. private void ResetSupporterInfo()
  113. {
  114. _isMBSupporter = null;
  115. _isMBSupporterInitialized = false;
  116. }
  117. }
  118. }