Permissions & Authorization for Users #24
Initial Release; Includes Database and MVC refactoring
This commit is contained in:
@@ -11,30 +11,30 @@ namespace Disco.Data.Configuration
|
||||
{
|
||||
public abstract class ConfigurationBase
|
||||
{
|
||||
private DiscoDataContext dbContext;
|
||||
private DiscoDataContext Database;
|
||||
|
||||
public abstract string Scope { get; }
|
||||
|
||||
public ConfigurationBase(DiscoDataContext dbContext)
|
||||
public ConfigurationBase(DiscoDataContext Database)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.Database = Database;
|
||||
}
|
||||
|
||||
protected List<ConfigurationItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConfigurationCache.GetConfigurationItems(dbContext, this.Scope);
|
||||
return ConfigurationCache.GetConfigurationItems(Database, this.Scope);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetValue<ValueType>(string Key, ValueType Value)
|
||||
{
|
||||
ConfigurationCache.SetConfigurationValue(dbContext, this.Scope, Key, Value);
|
||||
ConfigurationCache.SetConfigurationValue(Database, this.Scope, Key, Value);
|
||||
}
|
||||
private ValueType GetValue<ValueType>(string Key, ValueType Default)
|
||||
{
|
||||
return ConfigurationCache.GetConfigurationValue(dbContext, this.Scope, Key, Default);
|
||||
return ConfigurationCache.GetConfigurationValue(Database, this.Scope, Key, Default);
|
||||
}
|
||||
|
||||
protected void Set<ValueType>(ValueType Value, [CallerMemberName] string Key = null)
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Disco.Data.Configuration
|
||||
private static List<ConfigurationItem> configurationItems = new List<ConfigurationItem>();
|
||||
private static object configurationItemsLock = new object();
|
||||
|
||||
private static void LoadConfigurationItems(DiscoDataContext dbContext, string Scope, bool Reload)
|
||||
private static void LoadConfigurationItems(DiscoDataContext Database, string Scope, bool Reload)
|
||||
{
|
||||
if (Reload || !configDictionary.ContainsKey(Scope))
|
||||
{
|
||||
@@ -24,10 +24,10 @@ namespace Disco.Data.Configuration
|
||||
{
|
||||
if (Reload || !configDictionary.ContainsKey(Scope))
|
||||
{
|
||||
if (dbContext == null)
|
||||
if (Database == null)
|
||||
throw new InvalidOperationException("Cache-miss where Configuration Item requested from Cache-Only Configuration Context");
|
||||
|
||||
var newItems = dbContext.ConfigurationItems.Where(ci => ci.Scope == Scope).ToArray();
|
||||
var newItems = Database.ConfigurationItems.Where(ci => ci.Scope == Scope).ToArray();
|
||||
|
||||
if (configDictionary.ContainsKey(Scope))
|
||||
{
|
||||
@@ -43,15 +43,15 @@ namespace Disco.Data.Configuration
|
||||
}
|
||||
}
|
||||
}
|
||||
private static Dictionary<string, Dictionary<string, ConfigurationItem>> ConfigurationDictionary(DiscoDataContext dbContext, string IncludingScope)
|
||||
private static Dictionary<string, Dictionary<string, ConfigurationItem>> ConfigurationDictionary(DiscoDataContext Database, string IncludingScope)
|
||||
{
|
||||
LoadConfigurationItems(dbContext, IncludingScope, false);
|
||||
LoadConfigurationItems(Database, IncludingScope, false);
|
||||
return configDictionary;
|
||||
}
|
||||
private static ConfigurationItem ConfigurationItem(DiscoDataContext dbContext, string Scope, string Key)
|
||||
private static ConfigurationItem ConfigurationItem(DiscoDataContext Database, string Scope, string Key)
|
||||
{
|
||||
Dictionary<string, ConfigurationItem> scopeDict = default(Dictionary<string, ConfigurationItem>);
|
||||
if (ConfigurationDictionary(dbContext, Scope).TryGetValue(Scope, out scopeDict))
|
||||
if (ConfigurationDictionary(Database, Scope).TryGetValue(Scope, out scopeDict))
|
||||
{
|
||||
ConfigurationItem item = default(ConfigurationItem);
|
||||
if (scopeDict.TryGetValue(Key, out item))
|
||||
@@ -59,42 +59,42 @@ namespace Disco.Data.Configuration
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private static List<ConfigurationItem> ConfigurationItems(DiscoDataContext dbContext, string IncludingScope)
|
||||
private static List<ConfigurationItem> ConfigurationItems(DiscoDataContext Database, string IncludingScope)
|
||||
{
|
||||
LoadConfigurationItems(dbContext, IncludingScope, false);
|
||||
LoadConfigurationItems(Database, IncludingScope, false);
|
||||
return configurationItems;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Helpers
|
||||
internal static ValueType GetConfigurationValue<ValueType>(DiscoDataContext dbContext, string Scope, string Key, ValueType Default)
|
||||
internal static ValueType GetConfigurationValue<ValueType>(DiscoDataContext Database, string Scope, string Key, ValueType Default)
|
||||
{
|
||||
var ci = ConfigurationItem(dbContext, Scope, Key);
|
||||
var ci = ConfigurationItem(Database, Scope, Key);
|
||||
if (ci == null)
|
||||
return Default;
|
||||
else
|
||||
return (ValueType)Convert.ChangeType(ci.Value, typeof(ValueType));
|
||||
}
|
||||
internal static void SetConfigurationValue<ValueType>(DiscoDataContext dbContext, string Scope, string Key, ValueType Value)
|
||||
internal static void SetConfigurationValue<ValueType>(DiscoDataContext Database, string Scope, string Key, ValueType Value)
|
||||
{
|
||||
if (dbContext == null)
|
||||
if (Database == null)
|
||||
throw new InvalidOperationException("Cannot save changes with a CacheOnly Context");
|
||||
|
||||
var ci = ConfigurationItem(dbContext, Scope, Key);
|
||||
var ci = ConfigurationItem(Database, Scope, Key);
|
||||
if (ci == null && Value != null)
|
||||
{
|
||||
lock (configurationItemsLock)
|
||||
{
|
||||
ci = ConfigurationItem(dbContext, Scope, Key);
|
||||
ci = ConfigurationItem(Database, Scope, Key);
|
||||
if (ci == null)
|
||||
{
|
||||
// Create Configuration Item
|
||||
ci = new ConfigurationItem() { Scope = Scope, Key = Key, Value = Value.ToString() };
|
||||
// Add Item to DB & Internal Collections
|
||||
dbContext.ConfigurationItems.Add(ci);
|
||||
ConfigurationItems(dbContext, Scope).Add(ci);
|
||||
ConfigurationDictionary(dbContext, Scope)[Scope].Add(Key, ci);
|
||||
Database.ConfigurationItems.Add(ci);
|
||||
ConfigurationItems(Database, Scope).Add(ci);
|
||||
ConfigurationDictionary(Database, Scope)[Scope].Add(Key, ci);
|
||||
ci = null;
|
||||
}
|
||||
}
|
||||
@@ -103,17 +103,17 @@ namespace Disco.Data.Configuration
|
||||
{
|
||||
lock (configurationItemsLock)
|
||||
{
|
||||
var entityInfo = dbContext.Entry(ci);
|
||||
var entityInfo = Database.Entry(ci);
|
||||
if (entityInfo.State == System.Data.EntityState.Detached)
|
||||
{
|
||||
// Reload Scope from DB
|
||||
LoadConfigurationItems(dbContext, Scope, true);
|
||||
ci = ConfigurationItem(dbContext, Scope, Key);
|
||||
LoadConfigurationItems(Database, Scope, true);
|
||||
ci = ConfigurationItem(Database, Scope, Key);
|
||||
}
|
||||
|
||||
if (Value == null)
|
||||
{
|
||||
dbContext.ConfigurationItems.Remove(ci);
|
||||
Database.ConfigurationItems.Remove(ci);
|
||||
configurationItems.Remove(ci);
|
||||
configDictionary[Scope].Remove(Key);
|
||||
}
|
||||
@@ -125,9 +125,9 @@ namespace Disco.Data.Configuration
|
||||
}
|
||||
}
|
||||
|
||||
internal static List<ConfigurationItem> GetConfigurationItems(DiscoDataContext dbContext, string Scope)
|
||||
internal static List<ConfigurationItem> GetConfigurationItems(DiscoDataContext Database, string Scope)
|
||||
{
|
||||
return ConfigurationDictionary(dbContext, Scope)[Scope].Values.ToList();
|
||||
return ConfigurationDictionary(Database, Scope)[Scope].Values.ToList();
|
||||
}
|
||||
|
||||
internal static string ObsfucateValue(string Value)
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Disco.Data.Configuration.Modules
|
||||
{
|
||||
public class BootstrapperConfiguration : ConfigurationBase
|
||||
{
|
||||
public BootstrapperConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
|
||||
public BootstrapperConfiguration(DiscoDataContext Database) : base(Database) { }
|
||||
|
||||
public override string Scope
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Disco.Data.Configuration.Modules
|
||||
{
|
||||
public class DeviceProfilesConfiguration : ConfigurationBase
|
||||
{
|
||||
public DeviceProfilesConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
|
||||
public DeviceProfilesConfiguration(DiscoDataContext Database) : base(Database) { }
|
||||
|
||||
public override string Scope { get { return "DeviceProfiles"; } }
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Disco.Data.Configuration.Modules
|
||||
{
|
||||
public class OrganisationAddressesConfiguration : ConfigurationBase
|
||||
{
|
||||
public OrganisationAddressesConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
|
||||
public OrganisationAddressesConfiguration(DiscoDataContext Database) : base(Database) { }
|
||||
|
||||
public override string Scope { get { return "OrganisationAddresses"; } }
|
||||
|
||||
|
||||
@@ -13,13 +13,13 @@ namespace Disco.Data.Configuration
|
||||
{
|
||||
public class SystemConfiguration : ConfigurationBase
|
||||
{
|
||||
public SystemConfiguration(DiscoDataContext dbContext)
|
||||
: base(dbContext)
|
||||
public SystemConfiguration(DiscoDataContext Database)
|
||||
: base(Database)
|
||||
{
|
||||
// Init Modules
|
||||
this.moduleBootstrapperConfiguration = new Lazy<Modules.BootstrapperConfiguration>(() => new Modules.BootstrapperConfiguration(dbContext));
|
||||
this.moduleDeviceProfilesConfiguration = new Lazy<Modules.DeviceProfilesConfiguration>(() => new Modules.DeviceProfilesConfiguration(dbContext));
|
||||
this.moduleOrganisationAddressesConfiguration = new Lazy<Modules.OrganisationAddressesConfiguration>(() => new Modules.OrganisationAddressesConfiguration(dbContext));
|
||||
this.moduleBootstrapperConfiguration = new Lazy<Modules.BootstrapperConfiguration>(() => new Modules.BootstrapperConfiguration(Database));
|
||||
this.moduleDeviceProfilesConfiguration = new Lazy<Modules.DeviceProfilesConfiguration>(() => new Modules.DeviceProfilesConfiguration(Database));
|
||||
this.moduleOrganisationAddressesConfiguration = new Lazy<Modules.OrganisationAddressesConfiguration>(() => new Modules.OrganisationAddressesConfiguration(Database));
|
||||
}
|
||||
|
||||
#region Configuration Modules
|
||||
|
||||
Reference in New Issue
Block a user