// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Represents an image security section within a configuration file.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Configuration
{
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Xml;
using ImageProcessor.Web.Helpers;
///
/// Represents an image security section within a configuration file.
///
public sealed class ImageSecuritySection : ConfigurationSection
{
#region Properties
///
/// Gets the
///
/// The
[ConfigurationProperty("services", IsRequired = true)]
public ServiceElementCollection ImageServices
{
get
{
object o = this["services"];
return o as ServiceElementCollection;
}
}
#endregion
#region Methods
///
/// Retrieves the security configuration section from the current application configuration.
///
/// The cache configuration section from the current application configuration.
public static ImageSecuritySection GetConfiguration()
{
ImageSecuritySection imageSecuritySection = ConfigurationManager.GetSection("imageProcessor/security") as ImageSecuritySection;
if (imageSecuritySection != null)
{
return imageSecuritySection;
}
string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Configuration.Resources.security.config");
XmlReader reader = new XmlTextReader(new StringReader(section));
imageSecuritySection = new ImageSecuritySection();
imageSecuritySection.DeserializeSection(reader);
return imageSecuritySection;
}
#endregion
///
/// Represents a ServiceElement configuration element within the configuration.
///
public class ServiceElement : ConfigurationElement
{
///
/// Gets or sets the name of the plugin file.
///
/// The name of the plugin.
[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
///
/// Gets or sets the type of the service file.
///
/// The full Type definition of the plugin
[ConfigurationProperty("type", DefaultValue = "", IsRequired = true)]
public string Type
{
get { return (string)this["type"]; }
set { this["type"] = value; }
}
///
/// Gets the .
///
///
/// The .
///
[ConfigurationProperty("settings", IsRequired = false)]
public SettingElementCollection Settings
{
get
{
return this["settings"] as SettingElementCollection;
}
}
///
/// Gets the .
///
///
/// The .
///
[ConfigurationProperty("whitelist", IsRequired = false)]
public WhiteListElementCollection WhiteList
{
get
{
return this["whitelist"] as WhiteListElementCollection;
}
}
}
///
/// Represents a collection of elements within the configuration.
///
public class ServiceElementCollection : ConfigurationElementCollection
{
///
/// Gets or sets a value indicating whether to auto load all plugins.
/// Defaults to True.
///
/// If True plugins are auto discovered and loaded from all assemblies otherwise they must be defined in the configuration file
[ConfigurationProperty("autoLoadServices", DefaultValue = true, IsRequired = false)]
public bool AutoLoadServices
{
get { return (bool)this["autoLoadServices"]; }
set { this["autoLoadServices"] = value; }
}
///
/// Gets the type of the .
///
///
/// The of this collection.
///
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
///
/// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
///
///
/// The name of the collection; otherwise, an empty string. The default is an empty string.
///
protected override string ElementName
{
get { return "service"; }
}
///
/// Gets or sets the
/// at the specified index within the collection.
///
/// The index at which to get the specified object.
///
/// The
/// at the specified index within the collection.
///
public ServiceElement this[int index]
{
get
{
return (ServiceElement)BaseGet(index);
}
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
///
/// When overridden in a derived class, creates a new .
///
///
/// A new .
///
protected override ConfigurationElement CreateNewElement()
{
return new ServiceElement();
}
///
/// Gets the element key for a specified configuration element when overridden in a derived class.
///
///
/// An that acts as the key for the specified .
///
/// The to return the key for.
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServiceElement)element).Name;
}
}
///
/// Represents a SettingElement configuration element within the configuration.
///
public class SettingElement : ConfigurationElement
{
///
/// Gets or sets the key of the plugin setting.
///
/// The key of the plugin setting.
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return this["key"] as string;
}
set
{
this["key"] = value;
}
}
///
/// Gets or sets the value of the plugin setting.
///
/// The value of the plugin setting.
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get
{
return (string)this["value"];
}
set
{
this["value"] = value;
}
}
}
///
/// Represents a SettingElementCollection collection configuration element within the configuration.
///
public class SettingElementCollection : ConfigurationElementCollection
{
///
/// Gets the type of the .
///
///
/// The of this collection.
///
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
///
/// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
///
///
/// The name of the collection; otherwise, an empty string. The default is an empty string.
///
protected override string ElementName
{
get { return "setting"; }
}
///
/// Gets or sets the
/// at the specified index within the collection.
///
/// The index at which to get the specified object.
///
/// The
/// at the specified index within the collection.
///
public SettingElement this[int index]
{
get
{
return (SettingElement)BaseGet(index);
}
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
///
/// Returns the setting element with the specified key.
///
/// the key representing the element
/// the setting element
public new SettingElement this[string key]
{
get { return (SettingElement)BaseGet(key); }
}
///
/// Returns a value indicating whether the settings collection contains the
/// given object.
///
/// The key to identify the setting.
/// True if the collection contains the key; otherwise false.
public bool ContainsKey(string key)
{
object[] keys = BaseGetAllKeys();
return keys.Any(obj => (string)obj == key);
}
///
/// Gets the element key for a specified PluginElement configuration element.
///
///
/// The ConfigurationElement
/// to return the key for.
///
/// The element key for a specified PluginElement configuration element.
protected override object GetElementKey(ConfigurationElement element)
{
return ((SettingElement)element).Key;
}
///
/// Creates a new SettingElement configuration element.
///
///
/// A new SettingElement configuration element.
///
protected override ConfigurationElement CreateNewElement()
{
return new SettingElement();
}
}
///
/// Represents a whitelist collection configuration element within the configuration.
///
public class WhiteListElementCollection : ConfigurationElementCollection
{
///
/// Gets or sets the whitelist item at the given index.
///
/// The index of the whitelist item to get.
/// The whitelist item at the given index.
public SafeUrl this[int index]
{
get
{
return this.BaseGet(index) as SafeUrl;
}
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
///
/// Creates a new SafeURL configuration element.
///
///
/// A new SafeURL configuration element.
///
protected override ConfigurationElement CreateNewElement()
{
return new SafeUrl();
}
///
/// Gets the element key for a specified whitelist configuration element.
///
/// The ConfigurationElement to return the key for.
/// The element key for a specified whitelist configuration element.
protected override object GetElementKey(ConfigurationElement element)
{
return ((SafeUrl)element).Url;
}
}
///
/// Represents a whitelist configuration element within the configuration.
///
public class SafeUrl : ConfigurationElement
{
///
/// Gets or sets the url of the white listed file.
///
/// The url of the white listed file.
[ConfigurationProperty("url", DefaultValue = "", IsRequired = true)]
public Uri Url
{
get { return (Uri)this["url"]; }
set { this["url"] = value; }
}
}
}
}