2
0

PluginSecurityManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Security;
  3. using MediaBrowser.Model.Serialization;
  4. using Mediabrowser.Model.Entities;
  5. using Mediabrowser.PluginSecurity;
  6. using MediaBrowser.Common.Kernel;
  7. using MediaBrowser.Common.Net;
  8. using System;
  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 IHttpClient _httpClient;
  43. private IJsonSerializer _jsonSerializer;
  44. /// <summary>
  45. /// The _kernel
  46. /// </summary>
  47. private readonly IKernel _kernel;
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
  50. /// </summary>
  51. /// <param name="kernel">The kernel.</param>
  52. public PluginSecurityManager(IKernel kernel, IHttpClient httpClient, IJsonSerializer jsonSerializer, IApplicationPaths appPaths)
  53. {
  54. if (kernel == null)
  55. {
  56. throw new ArgumentNullException("kernel");
  57. }
  58. if (httpClient == null)
  59. {
  60. throw new ArgumentNullException("httpClient");
  61. }
  62. _kernel = kernel;
  63. _httpClient = httpClient;
  64. _jsonSerializer = jsonSerializer;
  65. MBRegistration.Init(appPaths);
  66. }
  67. /// <summary>
  68. /// Gets the registration status.
  69. /// </summary>
  70. /// <param name="feature">The feature.</param>
  71. /// <param name="mb2Equivalent">The MB2 equivalent.</param>
  72. /// <returns>Task{MBRegistrationRecord}.</returns>
  73. public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
  74. {
  75. return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false);
  76. }
  77. /// <summary>
  78. /// Gets or sets the supporter key.
  79. /// </summary>
  80. /// <value>The supporter key.</value>
  81. public string SupporterKey
  82. {
  83. get { return MBRegistration.SupporterKey; }
  84. set
  85. {
  86. if (value != MBRegistration.SupporterKey)
  87. {
  88. MBRegistration.SupporterKey = value;
  89. // Clear this so it will re-evaluate
  90. ResetSupporterInfo();
  91. // And we'll need to restart to re-evaluate the status of plug-ins
  92. _kernel.NotifyPendingRestart();
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Gets or sets the legacy key.
  98. /// </summary>
  99. /// <value>The legacy key.</value>
  100. public string LegacyKey
  101. {
  102. get { return MBRegistration.LegacyKey; }
  103. set
  104. {
  105. MBRegistration.LegacyKey = value;
  106. // And we'll need to restart to re-evaluate the status of plug-ins
  107. _kernel.NotifyPendingRestart();
  108. }
  109. }
  110. /// <summary>
  111. /// Resets the supporter info.
  112. /// </summary>
  113. private void ResetSupporterInfo()
  114. {
  115. _isMBSupporter = null;
  116. _isMBSupporterInitialized = false;
  117. }
  118. }
  119. }