示例#1
0
        /// <summary>
        /// Displays a warning if any of the current configurations do not have sufficient security access permissions for write access on the local computer for the current user
        /// </summary>
        public void DisplayWarningIfLocalFilePermissionsAreInsufficient()
        {
            if (_selectedConfigurations == null)
            {
                return;
            }

            try
            {
                bool anyAreDeniedWriteAccess = false;
                foreach (XmlConfiguration configuration in _selectedConfigurations)
                {
                    string path = configuration.Path;
                    if (path != null && path != string.Empty)
                    {
                        if (System.IO.File.Exists(path))
                        {
                            using (SecurityAccessRight right = new SecurityAccessRight(path))
                            {
                                bool noWrite = right.AssertWriteAccess();
                                if (!noWrite)
                                {
                                    anyAreDeniedWriteAccess = true;
                                }
                            }
                        }
                        else
                        {
                            string dir = System.IO.Path.GetDirectoryName(path);
                            using (SecurityAccessRight right = new SecurityAccessRight(dir))
                            {
                                bool noWrite = right.AssertWriteAccess();
                                if (!noWrite)
                                {
                                    anyAreDeniedWriteAccess = true;
                                }
                            }
                        }
                    }
                }

                if (anyAreDeniedWriteAccess)
                {
                    ExceptionEngine.DisplayException(
                        this,
                        "Security restriction detected - Write access is denied to one or more configuration files",
                        MessageBoxIcon.Information,
                        MessageBoxButtons.OK,
                        null,
                        "One or more of the configuration files that store the options you are about to configure, has been denied write access for the current user '" + System.Environment.UserName + "'.",
                        "You may continue and make changes to the options as normal, however some options may not be saved when the appliation exits.",
                        "Please contact your system administrator for questions regarding Windows security and access rights.");
                }
            }
            catch (System.Exception systemException)
            {
                System.Diagnostics.Trace.WriteLine(systemException);
            }
        }
		/// <summary>
		/// Displays a warning if any of the current configurations do not have sufficient security access permissions for write access on the local computer for the current user
		/// </summary>
		public void DisplayWarningIfLocalFilePermissionsAreInsufficient()
		{
			if (_selectedConfigurations == null)
				return;

			try
			{
				bool anyAreDeniedWriteAccess = false;
				foreach(XmlConfiguration configuration in _selectedConfigurations)
				{						
					string path = configuration.Path;
					if (path != null && path != string.Empty)
					{
						if (System.IO.File.Exists(path))
						{
							using(SecurityAccessRight right = new SecurityAccessRight(path))
							{
								bool noWrite = right.AssertWriteAccess();
								if (!noWrite)
									anyAreDeniedWriteAccess = true;	
							}
						}
						else
						{
							string dir = System.IO.Path.GetDirectoryName(path);
							using(SecurityAccessRight right = new SecurityAccessRight(dir))
							{
								bool noWrite = right.AssertWriteAccess();
								if (!noWrite)
									anyAreDeniedWriteAccess = true;
							}								
						}
					}
				}

				if (anyAreDeniedWriteAccess)
				{
					ExceptionEngine.DisplayException(
						this,
						"Security restriction detected - Write access is denied to one or more configuration files",
						MessageBoxIcon.Information,
						MessageBoxButtons.OK,
						null,
						"One or more of the configuration files that store the options you are about to configure, has been denied write access for the current user '" + System.Environment.UserName + "'.",
						"You may continue and make changes to the options as normal, however some options may not be saved when the appliation exits.",
						"Please contact your system administrator for questions regarding Windows security and access rights.");
				}
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
		}
		/// <summary>
		/// Writes a configuration to a path using the specified encryption engine. Takes windows security into account and checks for write access before trying to write to the path.
		/// </summary>
		/// <param name="path"></param>
		/// <param name="configuration"></param>
		/// <param name="encryptionEngine"></param>
		/// <returns></returns>
		public static bool WriteConfiguration(bool verbose, string path, XmlConfiguration configuration, FileEncryptionEngine encryptionEngine)
		{
			try
			{								
				Trace.WriteLineIf(verbose, "'Configuration Engine': Checking to see if the path '" + path + "' exists.");
				
				/// if the file exists, we need to try and read it
				if (System.IO.File.Exists(path))
				{
					Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' exists.");

					/// but first see if we have permissions to read it
					using(SecurityAccessRight right = new SecurityAccessRight(path))
					{
						Trace.WriteLineIf(verbose, "'Configuration Engine': Checking to see if the path '" + path + "' has write access.");

						/// if we don't have rights to the file						
						if (!right.AssertWriteAccess())
						{
							Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' does not have write access.");
							Trace.WriteLineIf(verbose, "'Configuration Engine': Prompting for user intervention for the path '" + path + "'.");

							if (verbose)
							{
								/// prompt to see what we should do about this
								DialogResult result = ExceptionEngine.DisplayException(
									null,
									"Write access denied - Unable to write to file",
									MessageBoxIcon.Error,
									MessageBoxButtons.AbortRetryIgnore,
									null,
									"Write access has been denied for the file '" + path + "'.",
									"Ignoring this exception may result in a loss of data if any options in this file were changed.");

								switch(result)
								{							
								case DialogResult.Abort:
									Trace.WriteLineIf(verbose, "'Configuration Engine': Aborting attempt to write to the path '" + path + "' because of user intervention.");
									return false;					

								case DialogResult.Retry:
									Trace.WriteLineIf(verbose, "'Configuration Engine': Retrying attempt to write to the path '" + path + "' because of user intervention.");
									return ConfigurationEngine.WriteConfiguration(verbose, path, configuration, encryptionEngine);					

								case DialogResult.Ignore:
									Trace.WriteLineIf(verbose, "'Configuration Engine': Ignoring attempt to write to the path '" + path + "' because of user intervention.");
									return true;
									//break;					
								};												
							}
							else
							{
								/// it failed, but we're not in verbose mode so who cares?
								return true;
							}
						}
						else
						{
							Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' has write access, preparing to write the configuration.");

							/// rights to write to the file
							/// ask the configuration engine to write our configuration file for us into our configuration 
							if (!ConfigurationEngine.WriteConfiguration(encryptionEngine, configuration, path))
							{
								Trace.WriteLineIf(verbose, "'Configuration Engine': Failed to write the configuration, throwing last exception from the ConfigurationEngine.");
								throw ConfigurationEngine.LastException;
							}
														
							/// ensure that the configuration has no changes visible
							if (configuration != null)
							{
								Trace.WriteLineIf(verbose, "'Configuration Engine': Succeeded in writing the configuration, accepting changes .");
								configuration.AcceptChanges();
							}

							return true;
						}
					}
				}
				else
				{
					Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' does not exist, preparing to write the configuration for the first time.");
					
					/// ask the configuration engine to write our configuration file for us into our configuration 
					if (!ConfigurationEngine.WriteConfiguration(encryptionEngine, configuration, path))
					{
						Trace.WriteLineIf(verbose, "'Configuration Engine': Failed to write the configuration, throwing last exception from the ConfigurationEngine.");
						throw ConfigurationEngine.LastException;
					}

					/// ensure that the configuration has no changes visible
					if (configuration != null)
					{
						Trace.WriteLineIf(verbose, "'Configuration Engine': Succeeded in writing the configuration, accepting changes .");
						configuration.AcceptChanges();
					}

					return true;
				}
			}
			catch(System.Exception systemException)
			{
				Trace.WriteLineIf(verbose, "'Configuration Engine': An unexpected exception was encountered while writing the configuration, dumping exception.");
				System.Diagnostics.Trace.WriteLine(systemException);
				Trace.WriteLineIf(verbose, "'Configuration Engine': Prompting for user intervention for the path '" + path + "'.");

				if (verbose)
				{
					/// failed for some reason writing the file
					/// prompt to see what we should do about this
					DialogResult result = ExceptionEngine.DisplayException(
						null,					
						"Exception encountered - Unable to write to file",
						MessageBoxIcon.Error,
						MessageBoxButtons.AbortRetryIgnore,
						systemException,
						"An exception was encountered while trying to write to the file '" + path + "'.",
						"Ignoring this exception may result in a loss of data if any options in this file were changed");							

					switch(result)
					{							
					case DialogResult.Abort:	
						Trace.WriteLineIf(verbose, "'Configuration Engine': Aborting attempt to write to the path '" + path + "' because of user intervention.");
						return false;					

					case DialogResult.Retry:
						Trace.WriteLineIf(verbose, "'Configuration Engine': Retrying attempt to write to the path '" + path + "' because of user intervention.");
						return ConfigurationEngine.WriteConfiguration(verbose, path, configuration, encryptionEngine);					

					case DialogResult.Ignore:
						Trace.WriteLineIf(verbose, "'Configuration Engine': Ignoring attempt to write to the path '" + path + "' because of user intervention.");
						return true;			
					};
				}
			}				
			return true;
		}
        /// <summary>
        /// Writes a configuration to a path using the specified encryption engine. Takes windows security into account and checks for write access before trying to write to the path.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="configuration"></param>
        /// <param name="encryptionEngine"></param>
        /// <returns></returns>
        public static bool WriteConfiguration(bool verbose, string path, XmlConfiguration configuration, FileEncryptionEngine encryptionEngine)
        {
            try
            {
                Trace.WriteLineIf(verbose, "'Configuration Engine': Checking to see if the path '" + path + "' exists.");

                /// if the file exists, we need to try and read it
                if (System.IO.File.Exists(path))
                {
                    Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' exists.");

                    /// but first see if we have permissions to read it
                    using (SecurityAccessRight right = new SecurityAccessRight(path))
                    {
                        Trace.WriteLineIf(verbose, "'Configuration Engine': Checking to see if the path '" + path + "' has write access.");

                        /// if we don't have rights to the file
                        if (!right.AssertWriteAccess())
                        {
                            Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' does not have write access.");
                            Trace.WriteLineIf(verbose, "'Configuration Engine': Prompting for user intervention for the path '" + path + "'.");

                            if (verbose)
                            {
                                /// prompt to see what we should do about this
                                DialogResult result = ExceptionEngine.DisplayException(
                                    null,
                                    "Write access denied - Unable to write to file",
                                    MessageBoxIcon.Error,
                                    MessageBoxButtons.AbortRetryIgnore,
                                    null,
                                    "Write access has been denied for the file '" + path + "'.",
                                    "Ignoring this exception may result in a loss of data if any options in this file were changed.");

                                switch (result)
                                {
                                case DialogResult.Abort:
                                    Trace.WriteLineIf(verbose, "'Configuration Engine': Aborting attempt to write to the path '" + path + "' because of user intervention.");
                                    return(false);

                                case DialogResult.Retry:
                                    Trace.WriteLineIf(verbose, "'Configuration Engine': Retrying attempt to write to the path '" + path + "' because of user intervention.");
                                    return(ConfigurationEngine.WriteConfiguration(verbose, path, configuration, encryptionEngine));

                                case DialogResult.Ignore:
                                    Trace.WriteLineIf(verbose, "'Configuration Engine': Ignoring attempt to write to the path '" + path + "' because of user intervention.");
                                    return(true);
                                    //break;
                                }
                                ;
                            }
                            else
                            {
                                /// it failed, but we're not in verbose mode so who cares?
                                return(true);
                            }
                        }
                        else
                        {
                            Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' has write access, preparing to write the configuration.");

                            /// rights to write to the file
                            /// ask the configuration engine to write our configuration file for us into our configuration
                            if (!ConfigurationEngine.WriteConfiguration(encryptionEngine, configuration, path))
                            {
                                Trace.WriteLineIf(verbose, "'Configuration Engine': Failed to write the configuration, throwing last exception from the ConfigurationEngine.");
                                throw ConfigurationEngine.LastException;
                            }

                            /// ensure that the configuration has no changes visible
                            if (configuration != null)
                            {
                                Trace.WriteLineIf(verbose, "'Configuration Engine': Succeeded in writing the configuration, accepting changes .");
                                configuration.AcceptChanges();
                            }

                            return(true);
                        }
                    }
                }
                else
                {
                    Trace.WriteLineIf(verbose, "'Configuration Engine': The path '" + path + "' does not exist, preparing to write the configuration for the first time.");

                    /// ask the configuration engine to write our configuration file for us into our configuration
                    if (!ConfigurationEngine.WriteConfiguration(encryptionEngine, configuration, path))
                    {
                        Trace.WriteLineIf(verbose, "'Configuration Engine': Failed to write the configuration, throwing last exception from the ConfigurationEngine.");
                        throw ConfigurationEngine.LastException;
                    }

                    /// ensure that the configuration has no changes visible
                    if (configuration != null)
                    {
                        Trace.WriteLineIf(verbose, "'Configuration Engine': Succeeded in writing the configuration, accepting changes .");
                        configuration.AcceptChanges();
                    }

                    return(true);
                }
            }
            catch (System.Exception systemException)
            {
                Trace.WriteLineIf(verbose, "'Configuration Engine': An unexpected exception was encountered while writing the configuration, dumping exception.");
                System.Diagnostics.Trace.WriteLine(systemException);
                Trace.WriteLineIf(verbose, "'Configuration Engine': Prompting for user intervention for the path '" + path + "'.");

                if (verbose)
                {
                    /// failed for some reason writing the file
                    /// prompt to see what we should do about this
                    DialogResult result = ExceptionEngine.DisplayException(
                        null,
                        "Exception encountered - Unable to write to file",
                        MessageBoxIcon.Error,
                        MessageBoxButtons.AbortRetryIgnore,
                        systemException,
                        "An exception was encountered while trying to write to the file '" + path + "'.",
                        "Ignoring this exception may result in a loss of data if any options in this file were changed");

                    switch (result)
                    {
                    case DialogResult.Abort:
                        Trace.WriteLineIf(verbose, "'Configuration Engine': Aborting attempt to write to the path '" + path + "' because of user intervention.");
                        return(false);

                    case DialogResult.Retry:
                        Trace.WriteLineIf(verbose, "'Configuration Engine': Retrying attempt to write to the path '" + path + "' because of user intervention.");
                        return(ConfigurationEngine.WriteConfiguration(verbose, path, configuration, encryptionEngine));

                    case DialogResult.Ignore:
                        Trace.WriteLineIf(verbose, "'Configuration Engine': Ignoring attempt to write to the path '" + path + "' because of user intervention.");
                        return(true);
                    }
                    ;
                }
            }
            return(true);
        }