initial source commit

This commit is contained in:
Gary Sharp
2013-02-01 12:35:28 +11:00
parent 543a005d31
commit 0a93429800
1103 changed files with 285609 additions and 0 deletions
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Disco.Data.Configuration
{
public abstract class ConfigurationBase
{
private ConfigurationContext _context;
public ConfigurationContext Context
{
get
{
return _context;
}
}
public abstract string Scope { get; }
public ConfigurationBase(ConfigurationContext Context)
{
this._context = Context;
}
protected void SetValue<ValueType>(string Key, ValueType Value)
{
this.Context.SetConfigurationValue(this.Scope, Key, Value);
}
protected ValueType GetValue<ValueType>(string Key, ValueType Default)
{
return this.Context.GetConfigurationValue(this.Scope, Key, Default);
}
}
}
@@ -0,0 +1,472 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Data.Repository;
using Disco.Models.Repository;
using System.IO;
using System.Security.Cryptography;
using Disco.Models.BI.Interop.Community;
using Newtonsoft.Json;
namespace Disco.Data.Configuration
{
public class ConfigurationContext
{
private DiscoDataContext _dbContext;
private DiscoDataContext dbContext
{
get
{
if (_dbContext != null)
return _dbContext;
else
throw new InvalidOperationException("Cache-miss where Configuration Item requested from Cache-Only Configuration Context");
}
}
public bool CacheOnly
{
get
{
return _dbContext == null;
}
}
public ConfigurationContext(DiscoDataContext dbContext)
{
this._dbContext = dbContext;
// Init Modules
this.moduleBootstrapperConfiguration = new Lazy<Modules.BootstrapperConfiguration>(() => new Modules.BootstrapperConfiguration(this));
this.moduleDeviceProfilesConfiguration = new Lazy<Modules.DeviceProfilesConfiguration>(() => new Modules.DeviceProfilesConfiguration(this));
this.moduleOrganisationAddressesConfiguration = new Lazy<Modules.OrganisationAddressesConfiguration>(() => new Modules.OrganisationAddressesConfiguration(this));
this.moduleWirelessConfiguration = new Lazy<Modules.WirelessConfiguration>(() => new Modules.WirelessConfiguration(this));
}
#region Item Cache
private static Dictionary<String, Dictionary<String, ConfigurationItem>> configDictionary = new Dictionary<string, Dictionary<string, ConfigurationItem>>();
private static List<ConfigurationItem> configurationItems = new List<ConfigurationItem>();
private static object configurationItemsLock = new object();
private void loadConfigurationItems(string Scope, bool Reload)
{
if (Reload || !configDictionary.ContainsKey(Scope))
{
lock (configurationItemsLock)
{
if (Reload || !configDictionary.ContainsKey(Scope))
{
var newItems = this.dbContext.ConfigurationItems.Where(ci => ci.Scope == Scope).ToArray();
if (configDictionary.ContainsKey(Scope))
{
var existingItems = configDictionary[Scope];
foreach (var existingItem in existingItems.Values)
{
configurationItems.Remove(existingItem);
}
}
configurationItems.AddRange(newItems);
configDictionary[Scope] = newItems.ToDictionary(ci => ci.Key);
}
}
}
}
public Dictionary<string, Dictionary<string, ConfigurationItem>> ConfigurationDictionary(string IncludingScope)
{
this.loadConfigurationItems(IncludingScope, false);
return configDictionary;
}
public ConfigurationItem ConfigurationItem(string Scope, string Key)
{
Dictionary<string, ConfigurationItem> scopeDict = default(Dictionary<string, ConfigurationItem>);
if (this.ConfigurationDictionary(Scope).TryGetValue(Scope, out scopeDict))
{
ConfigurationItem item = default(ConfigurationItem);
if (scopeDict.TryGetValue(Key, out item))
return item;
}
return null;
}
private List<ConfigurationItem> ConfigurationItems(string IncludingScope)
{
this.loadConfigurationItems(IncludingScope, false);
return configurationItems;
}
#endregion
#region Helpers
public ValueType GetConfigurationValue<ValueType>(string Scope, string Key, ValueType Default)
{
var ci = this.ConfigurationItem(Scope, Key);
if (ci == null)
return Default;
else
return (ValueType)Convert.ChangeType(ci.Value, typeof(ValueType));
}
public void SetConfigurationValue<ValueType>(string Scope, string Key, ValueType Value)
{
if (CacheOnly)
throw new InvalidOperationException("Cannot save changes with a CacheOnly Context");
var ci = this.ConfigurationItem(Scope, Key);
if (ci == null && Value != null)
{
lock (configurationItemsLock)
{
ci = this.ConfigurationItem(Scope, Key);
if (ci == null)
{
// Create Configuration Item
ci = new ConfigurationItem() { Scope = Scope, Key = Key, Value = Value.ToString() };
// Add Item to DB & Internal Collections
this.dbContext.ConfigurationItems.Add(ci);
this.ConfigurationItems(Scope).Add(ci);
this.ConfigurationDictionary(Scope)[Scope].Add(Key, ci);
ci = null;
}
}
}
if (ci != null)
{
lock (configurationItemsLock)
{
var entityInfo = dbContext.Entry(ci);
if (entityInfo.State == System.Data.EntityState.Detached)
{
// Reload Scope from DB
this.loadConfigurationItems(Scope, true);
ci = this.ConfigurationItem(Scope, Key);
}
if (Value == null)
{
dbContext.ConfigurationItems.Remove(ci);
configurationItems.Remove(ci);
configDictionary[Scope].Remove(Key);
}
else
{
ci.Value = Value.ToString();
}
}
}
}
public static string ObsfucateValue(string Value)
{
if (string.IsNullOrEmpty(Value))
return Value;
else
return Convert.ToBase64String(Encoding.Unicode.GetBytes(Value));
}
public static string DeobsfucateValue(string ObsfucatedValue)
{
if (string.IsNullOrEmpty(ObsfucatedValue))
return ObsfucatedValue;
else
return Encoding.Unicode.GetString(Convert.FromBase64String(ObsfucatedValue));
}
#endregion
#region Configuration Modules
private Lazy<Modules.BootstrapperConfiguration> moduleBootstrapperConfiguration;
private Lazy<Modules.DeviceProfilesConfiguration> moduleDeviceProfilesConfiguration;
private Lazy<Modules.OrganisationAddressesConfiguration> moduleOrganisationAddressesConfiguration;
private Lazy<Modules.WirelessConfiguration> moduleWirelessConfiguration;
public Modules.BootstrapperConfiguration Bootstrapper
{
get
{
return moduleBootstrapperConfiguration.Value;
}
}
public Modules.DeviceProfilesConfiguration DeviceProfiles
{
get
{
return moduleDeviceProfilesConfiguration.Value;
}
}
public Modules.OrganisationAddressesConfiguration OrganisationAddresses
{
get
{
return moduleOrganisationAddressesConfiguration.Value;
}
}
public Modules.WirelessConfiguration Wireless
{
get
{
return moduleWirelessConfiguration.Value;
}
}
#endregion
#region System Configuration Items
public string Scope { get { return "System"; } }
public string DataStoreLocation
{
get
{
var result = this.GetConfigurationValue<string>(this.Scope, "DataStoreLocation", null);
if (result == null)
{
var appDataPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data");
if (appDataPath.EndsWith("\\"))
return appDataPath;
else
return string.Concat(appDataPath, '\\');
}
else
return result;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (!System.IO.Directory.Exists(value))
throw new System.IO.DirectoryNotFoundException(string.Format("DataStoreLocation: '{0}' could not be found", value));
string storePath;
if (value.EndsWith("\\"))
storePath = value;
else
storePath = string.Concat(value, '\\');
this.SetConfigurationValue(this.Scope, "DataStoreLocation", storePath);
}
}
public string PluginsLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"Plugins\");
}
}
public string PluginStorageLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"PluginStorage\");
}
}
#region Organisation Logo
private string OrganisationLogoPath
{
get
{
return System.IO.Path.Combine(DataStoreLocation, "OrganisationLogo.png");
}
}
//private static string _OrganisationLogoHash;
//private static byte[] _OrganisationLogo;
//private static object _OrganisationLogoLock = new object();
//private static void LoadOrganisationLogo(ConfigurationContext context, bool reload = false)
//{
// if (_OrganisationLogoHash == null || reload)
// {
// lock (_OrganisationLogoLock)
// {
// if (_OrganisationLogoHash == null || reload)
// {
// _OrganisationLogo = null;
// _OrganisationLogoHash = null;
// string organisationLogoPath = context.OrganisationLogoPath;
// if (System.IO.File.Exists(organisationLogoPath))
// _OrganisationLogo = System.IO.File.ReadAllBytes(organisationLogoPath);
// }
// if (_OrganisationLogo == null || _OrganisationLogo.Length == 0)
// {
// _OrganisationLogo = Disco.Data.Properties.Resources.EmptyLogo;
// }
// if (_OrganisationLogoHash == null)
// {
// using (SHA256 h = SHA256.Create())
// {
// _OrganisationLogoHash = Convert.ToBase64String(h.ComputeHash(_OrganisationLogo));
// }
// }
// }
// }
//}
public string OrganisationLogoHash
{
get
{
var path = this.OrganisationLogoPath;
if (File.Exists(path))
return File.GetLastWriteTimeUtc(path).ToBinary().ToString();
else
return "-1";
}
}
public Stream OrganisationLogo
{
get
{
//LoadOrganisationLogo(this);
//if (_OrganisationLogo == null || _OrganisationLogo.Length != 0)
// return new MemoryStream(_OrganisationLogo);
//else
// return null;
var path = this.OrganisationLogoPath;
if (File.Exists(path))
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
else
return new MemoryStream(Disco.Data.Properties.Resources.EmptyLogo);
}
set
{
string organisationLogoPath = this.OrganisationLogoPath;
if (value == null)
{
if (System.IO.File.Exists(organisationLogoPath))
System.IO.File.Delete(organisationLogoPath);
}
else
{
using (FileStream fs = new FileStream(organisationLogoPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
value.CopyTo(fs);
}
}
//LoadOrganisationLogo(this, true);
}
}
#endregion
public string OrganisationName
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "OrganisationName", null);
}
set
{
this.SetConfigurationValue(this.Scope, "OrganisationName", value);
}
}
public bool MultiSiteMode
{
get
{
return this.GetConfigurationValue<bool>(this.Scope, "MultiSiteMode", false);
}
set
{
this.SetConfigurationValue(this.Scope, "MultiSiteMode", value);
}
}
#region Proxy Configuration
public string ProxyAddress
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "ProxyAddress", null);
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyAddress", value);
}
}
public int ProxyPort
{
get
{
return this.GetConfigurationValue(this.Scope, "ProxyPort", 8080);
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyPort", value);
}
}
public string ProxyUsername
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "ProxyUsername", null);
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyUsername", value);
}
}
public string ProxyPassword
{
get
{
return DeobsfucateValue(this.GetConfigurationValue<string>(this.Scope, "ProxyPassword", null));
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyPassword", ObsfucateValue(value));
}
}
#endregion
#region UpdateCheck
public string DeploymentId
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "DeploymentId", null);
}
}
public UpdateResponse UpdateLastCheck
{
get
{
var json = this.GetConfigurationValue<string>(this.Scope, "UpdateLastCheck", null);
if (json != null)
{
try
{
return JsonConvert.DeserializeObject<UpdateResponse>(json);
}
catch (Exception)
{ }// Ignore Serialization Issues
}
return null;
}
set
{
if (value == null)
this.SetConfigurationValue<string>(this.Scope, "UpdateLastCheck", null);
var json = JsonConvert.SerializeObject(value);
this.SetConfigurationValue<string>(this.Scope, "UpdateLastCheck", json);
}
}
public bool UpdateBetaDeployment
{
get
{
return this.GetConfigurationValue<bool>(this.Scope, "UpdateBetaDeployment", false);
}
}
public string InstalledDatabaseVersion
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "InstalledDatabaseVersion", null);
}
set
{
this.SetConfigurationValue<string>(this.Scope, "InstalledDatabaseVersion", value);
}
}
#endregion
#endregion
}
}
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Disco.Data.Configuration.Modules
{
public class BootstrapperConfiguration : ConfigurationBase
{
public BootstrapperConfiguration(ConfigurationContext Context) : base(Context) { }
public override string Scope
{
get { return "Bootstrapper"; }
}
public string MacSshUsername
{
get
{
return this.GetValue("MacSshUsername", "root");
}
set
{
this.SetValue("MacSshUsername", value);
}
}
public string MacSshPassword
{
get
{
return ConfigurationContext.DeobsfucateValue(this.GetValue("MacSshPassword", string.Empty));
}
set
{
this.SetValue("MacSshPassword", ConfigurationContext.ObsfucateValue(value));
}
}
}
}
@@ -0,0 +1,86 @@
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//
//
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Disco.Models.Repository;
//namespace Disco.Data.Configuration.Modules
//{
// public class DeviceProfileConfiguration : ConfigurationBase
// {
// private DeviceProfilesConfiguration deviceProfilesConfig;
// private DeviceProfile deviceProfile;
// public DeviceProfileConfiguration(ConfigurationContext Context, DeviceProfile DeviceProfile)
// : base(Context)
// {
// this.deviceProfilesConfig = Context.DeviceProfiles;
// this.deviceProfile = DeviceProfile;
// }
// public override string Scope
// {
// get
// {
// return string.Format("DeviceProfile:{0}", this.deviceProfile.Id);
// }
// }
// public string ComputerNameTemplate
// {
// get
// {
// return this.GetValue("ComputerNameTemplate", "DeviceProfile.ShortName + '-' + SerialNumber");
// }
// set
// {
// this.SetValue("ComputerNameTemplate", value);
// }
// }
// public enum DeviceProfileDistributionTypes : int
// {
// OneToMany = 0,
// OneToOne = 1
// }
// public DeviceProfileDistributionTypes DistributionType
// {
// get
// {
// return (DeviceProfileDistributionTypes)this.GetValue("DistributionType", (int)DeviceProfileDistributionTypes.OneToMany);
// }
// set
// {
// this.SetValue("DistributionType", (int)value);
// }
// }
// public string OrganisationalUnit
// {
// get
// {
// return this.GetValue<string>("OrganisationalUnit", null);
// }
// set
// {
// this.SetValue("OrganisationalUnit", value);
// }
// }
// public bool AllocateWirelessCertificate
// {
// get
// {
// return this.GetValue("AllocateWirelessCertificate", false);
// }
// set
// {
// this.SetValue("AllocateWirelessCertificate", value);
// }
// }
// }
//}
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Models.Repository;
namespace Disco.Data.Configuration.Modules
{
public class DeviceProfilesConfiguration : ConfigurationBase
{
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//private Dictionary<int, DeviceProfileConfiguration> deviceProfileConfigurations;
public DeviceProfilesConfiguration(ConfigurationContext Context)
: base(Context)
{
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//this.deviceProfileConfigurations = new Dictionary<int, DeviceProfileConfiguration>();
}
public override string Scope
{
get { return "DeviceProfiles"; }
}
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//public DeviceProfileConfiguration DeviceProfile(DeviceProfile Profile)
//{
// DeviceProfileConfiguration dpc = default(DeviceProfileConfiguration);
// if (!this.deviceProfileConfigurations.TryGetValue(Profile.Id, out dpc))
// {
// dpc = new DeviceProfileConfiguration(this.Context, Profile);
// this.deviceProfileConfigurations[Profile.Id] = dpc;
// }
// return dpc;
//}
public int DefaultDeviceProfileId
{
get
{
var v = this.GetValue("DefaultDeviceProfileId", 1);
if (v > 0)
return v;
else
return 1;
}
set
{
if (value < 1)
throw new ArgumentOutOfRangeException("value", "Expected >= 1");
this.SetValue("DefaultDeviceProfileId", value);
}
}
public int DefaultAddDeviceOfflineDeviceProfileId
{
get
{
return this.GetValue("DefaultAddDeviceOfflineDeviceProfileId", 0);
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value", "Expected >= 0");
this.SetValue("DefaultAddDeviceOfflineDeviceProfileId", value);
}
}
}
}
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Models.BI.Config;
using Disco.Models.Repository;
using Newtonsoft.Json;
namespace Disco.Data.Configuration.Modules
{
public class OrganisationAddressesConfiguration : ConfigurationBase
{
public OrganisationAddressesConfiguration(ConfigurationContext Context) : base(Context) { }
public override string Scope
{
get { return "OrganisationAddresses"; }
}
public OrganisationAddress GetAddress(int Id)
{
var address = default(OrganisationAddress);
var addressString = this.GetValue<string>(Id.ToString(), null);
if (addressString != null)
{
if (addressString.StartsWith("{"))
{
// Assume Json
address = JsonConvert.DeserializeObject<OrganisationAddress>(addressString);
}
else
{
// Assume Old Storage Method
address = OrganisationAddress.FromConfigurationEntry(Id, addressString);
}
}
return address;
}
public OrganisationAddress SetAddress(OrganisationAddress Address)
{
if (!Address.Id.HasValue)
{
Address.Id = NextOrganisationAddressId;
}
string addressString = JsonConvert.SerializeObject(Address);
this.SetValue(Address.Id.ToString(), addressString); //Address.ToConfigurationEntry());
return Address;
}
public void RemoveAddress(int Id)
{
// Set Config Item to null = Remove Configuration Item
this.SetValue<string>(Id.ToString(), null);
}
public List<OrganisationAddress> Addresses
{
get
{
Dictionary<string, ConfigurationItem> configAddress = default(Dictionary<string, ConfigurationItem>);
if (this.Context.ConfigurationDictionary(this.Scope).TryGetValue(this.Scope, out configAddress))
return configAddress.Select(
ca => ca.Value.Value.StartsWith("{") ?
JsonConvert.DeserializeObject<OrganisationAddress>(ca.Value.Value) :
OrganisationAddress.FromConfigurationEntry(int.Parse(ca.Key), ca.Value.Value)
).ToList();
else
return new List<OrganisationAddress>(); // Empty List - No Addresses
}
}
private int NextOrganisationAddressId
{
get
{
int nextId = 0;
while (true)
{
if (this.Context.ConfigurationItem(this.Scope, nextId.ToString()) == null)
break;
nextId++;
}
return nextId;
}
}
}
}
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Disco.Data.Configuration.Modules
{
public class WirelessConfiguration : ConfigurationBase
{
public const string Provider_eduSTAR = "eduSTAR";
public const string Provider_eduPaSS = "eduPaSS";
public WirelessConfiguration(ConfigurationContext Context) : base(Context) { }
public override string Scope
{
get { return "Wireless"; }
}
public int CertificateAutoBufferMax
{
get
{
return this.GetValue("CertificateAutoBufferMax", 50);
}
set
{
this.SetValue("CertificateAutoBufferMax", value);
}
}
public int CertificateAutoBufferLow
{
get
{
return this.GetValue("CertificateAutoBufferLow", 10);
}
set
{
this.SetValue("CertificateAutoBufferLow", value);
}
}
public string Provider
{
get
{
return this.GetValue("Provider", Provider_eduSTAR);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
if (value.Equals(Provider_eduSTAR, StringComparison.InvariantCultureIgnoreCase))
this.SetValue("Provider", Provider_eduSTAR);
else
throw new NotSupportedException(string.Format("Unsupported Wireless Provider: ", value));
}
}
#region eduSTAR Configuration
public string eduSTAR_Scope
{
get { return "Wireless_eduSTAR"; }
}
public string eduSTAR_ServiceAccountSchoolId
{
get
{
return this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountSchoolId", null);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountSchoolId", value);
}
}
public string eduSTAR_ServiceAccountUsername
{
get
{
return this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountUsername", null);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountUsername", value);
}
}
public string eduSTAR_ServiceAccountPassword
{
get
{
return ConfigurationContext.DeobsfucateValue(this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountPassword", null));
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountPassword", ConfigurationContext.ObsfucateValue(value));
}
}
#endregion
}
}