2013-09-13 13:45:12 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-03 22:43:06 -07:00
|
|
|
|
using MediaBrowser.Common.Security;
|
2013-06-10 09:57:30 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-01-25 14:11:09 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-09-13 13:45:12 -07:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-23 15:44:42 -07:00
|
|
|
|
using System;
|
2013-09-13 13:45:12 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-03-03 22:43:06 -07:00
|
|
|
|
namespace MediaBrowser.Common.Implementations.Security
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class PluginSecurityManager
|
|
|
|
|
/// </summary>
|
2013-02-26 15:13:58 -07:00
|
|
|
|
public class PluginSecurityManager : ISecurityManager
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _is MB supporter
|
|
|
|
|
/// </summary>
|
2013-04-08 08:55:53 -07:00
|
|
|
|
private bool? _isMbSupporter;
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _is MB supporter initialized
|
|
|
|
|
/// </summary>
|
2013-04-08 08:55:53 -07:00
|
|
|
|
private bool _isMbSupporterInitialized;
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _is MB supporter sync lock
|
|
|
|
|
/// </summary>
|
2013-04-08 08:55:53 -07:00
|
|
|
|
private object _isMbSupporterSyncLock = new object();
|
2013-02-20 23:02:10 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether this instance is MB supporter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance is MB supporter; otherwise, <c>false</c>.</value>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public bool IsMBSupporter
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-01-25 14:11:09 -07:00
|
|
|
|
LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetRegistrationStatus("MBSupporter", null, _appHost.ApplicationVersion.ToString()).Result.IsRegistered);
|
2013-04-08 08:55:53 -07:00
|
|
|
|
return _isMbSupporter.Value;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 08:55:53 -07:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
private readonly IApplicationHost _appHost;
|
|
|
|
|
private readonly IApplicationPaths _applciationPaths;
|
2013-09-13 13:45:12 -07:00
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IRequiresRegistration> _registeredEntities;
|
2013-03-14 10:16:07 -07:00
|
|
|
|
protected IEnumerable<IRequiresRegistration> RegisteredEntities
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _registeredEntities ?? (_registeredEntities = _appHost.GetExports<IRequiresRegistration>());
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-23 15:44:42 -07:00
|
|
|
|
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
|
|
|
|
|
/// </summary>
|
2014-01-25 14:11:09 -07:00
|
|
|
|
public PluginSecurityManager(IApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer,
|
|
|
|
|
IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-25 20:43:04 -07:00
|
|
|
|
if (httpClient == null)
|
2013-02-23 15:44:42 -07:00
|
|
|
|
{
|
2013-02-25 20:43:04 -07:00
|
|
|
|
throw new ArgumentNullException("httpClient");
|
2013-02-23 15:44:42 -07:00
|
|
|
|
}
|
2013-03-06 22:34:00 -07:00
|
|
|
|
|
2013-04-08 08:55:53 -07:00
|
|
|
|
_applciationPaths = appPaths;
|
2013-09-13 13:45:12 -07:00
|
|
|
|
_networkManager = networkManager;
|
2013-03-06 22:34:00 -07:00
|
|
|
|
_appHost = appHost;
|
2013-02-25 20:43:04 -07:00
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-01-25 14:11:09 -07:00
|
|
|
|
MBRegistration.Init(_applciationPaths, _networkManager, logManager);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 09:32:16 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Load all registration info for all entities that require registration
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task LoadAllRegistrationInfo()
|
|
|
|
|
{
|
|
|
|
|
var tasks = new List<Task>();
|
|
|
|
|
|
2013-03-14 09:40:36 -07:00
|
|
|
|
ResetSupporterInfo();
|
2013-03-14 10:16:07 -07:00
|
|
|
|
tasks.AddRange(RegisteredEntities.Select(i => i.LoadRegistrationInfoAsync()));
|
2013-03-14 09:32:16 -07:00
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the registration status.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="feature">The feature.</param>
|
|
|
|
|
/// <param name="mb2Equivalent">The MB2 equivalent.</param>
|
2014-01-25 14:11:09 -07:00
|
|
|
|
/// <param name="version">The version of this feature</param>
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <returns>Task{MBRegistrationRecord}.</returns>
|
2014-01-25 14:11:09 -07:00
|
|
|
|
public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null, string version = null)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2014-01-25 14:11:09 -07:00
|
|
|
|
return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent, version).ConfigureAwait(false);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the supporter key.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The supporter key.</value>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public string SupporterKey
|
|
|
|
|
{
|
2013-04-08 08:55:53 -07:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return MBRegistration.SupporterKey;
|
|
|
|
|
}
|
2013-02-23 15:44:42 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
2013-02-20 18:33:05 -07:00
|
|
|
|
if (value != MBRegistration.SupporterKey)
|
|
|
|
|
{
|
|
|
|
|
MBRegistration.SupporterKey = value;
|
2013-03-14 09:40:36 -07:00
|
|
|
|
// re-load registration info
|
|
|
|
|
Task.Run(() => LoadAllRegistrationInfo());
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the legacy key.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The legacy key.</value>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public string LegacyKey
|
|
|
|
|
{
|
2013-04-08 08:55:53 -07:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return MBRegistration.LegacyKey;
|
|
|
|
|
}
|
2013-02-23 15:44:42 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
2013-03-14 09:40:36 -07:00
|
|
|
|
if (value != MBRegistration.LegacyKey)
|
|
|
|
|
{
|
|
|
|
|
MBRegistration.LegacyKey = value;
|
|
|
|
|
// re-load registration info
|
|
|
|
|
Task.Run(() => LoadAllRegistrationInfo());
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 23:02:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resets the supporter info.
|
|
|
|
|
/// </summary>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
private void ResetSupporterInfo()
|
|
|
|
|
{
|
2013-04-08 08:55:53 -07:00
|
|
|
|
_isMbSupporter = null;
|
|
|
|
|
_isMbSupporterInitialized = false;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|