Skip to content

Commit

Permalink
Fixed the exception when file not found. Beefed up validation and add…
Browse files Browse the repository at this point in the history
…ed more post install ACL modifications.
  • Loading branch information
ScottyMac52 committed Sep 17, 2019
1 parent 4220c5b commit 8757c47
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 103 deletions.
4 changes: 2 additions & 2 deletions CustomActions/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
56 changes: 33 additions & 23 deletions CustomActions/SetUserPerms.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
Expand All @@ -24,34 +25,43 @@ public override void Install(IDictionary stateSaver)

// This gets the named parameters passed in from your custom action
string folder = Context.Parameters["folder"];
var cacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Vyper Industries\MFD4CTS\cache");

var powerUserSid = new SecurityIdentifier(WellKnownSidType.BuiltinPowerUsersSid, null);
var userSid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var creatorOwnerSid = new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null);
if(!Directory.Exists(cacheFolder))
{
Directory.CreateDirectory(cacheFolder);
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : Created cache folder: {cacheFolder}.");
}

// This gets the built in users group
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : This is output from the installer, Creator: {creatorOwnerSid?.Value} PowerUser: {powerUserSid} User: {userSid} the folder: {folder}{Environment.NewLine}");
var writerulePowerUsers = new FileSystemAccessRule(powerUserSid, FileSystemRights.FullControl, AccessControlType.Allow);
var writeruleUsers = new FileSystemAccessRule(userSid, FileSystemRights.FullControl, AccessControlType.Allow);
var writeruleCreator = new FileSystemAccessRule(creatorOwnerSid, FileSystemRights.FullControl, AccessControlType.Allow);
if (!string.IsNullOrEmpty(folder) && Directory.Exists(folder))
{
// Get your file's ACL
DirectorySecurity fsecurity = Directory.GetAccessControl(folder);
DirectorySecurity cacheSecurity = Directory.GetAccessControl(cacheFolder);

if (!string.IsNullOrEmpty(folder) && Directory.Exists(folder))
{
// Get your file's ACL
DirectorySecurity fsecurity = Directory.GetAccessControl(folder);
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : Processing: {folder}");
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : Processing: {cacheFolder}");
var sidsToProcess = new SortedList<string, SecurityIdentifier>()
{
{ "PowerUser", new SecurityIdentifier(WellKnownSidType.BuiltinPowerUsersSid, null) },
{ "Users", new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null) },
{ "CreatorOwner", new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null) }
};

// Add the new rule to the ACL
fsecurity.AddAccessRule(writerulePowerUsers);
fsecurity.AddAccessRule(writeruleUsers);
fsecurity.AddAccessRule(writeruleCreator);
foreach(var sid in sidsToProcess)
{
var fullControlRule = new FileSystemAccessRule(sid.Value, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : Adding SID: {sid.Value?.Value ?? "Unknown"} Named: {sid.Key}");
fsecurity.AddAccessRule(fullControlRule);
cacheSecurity.AddAccessRule(fullControlRule);
}

// Set the ACL back to the file
Directory.SetAccessControl(folder, fsecurity);

File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : folder: {folder} was processed.{Environment.NewLine}");
}

}
Directory.SetAccessControl(folder, fsecurity);
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : folder: {folder} was processed for full control.");
Directory.SetAccessControl(cacheFolder, cacheSecurity);
File.AppendAllText(Path.Combine(folder, "customactions.log"), $"{Environment.NewLine}{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()} : folder: {cacheFolder} was processed for full control.{Environment.NewLine}");
}
}

[SecurityPermission(SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
Expand Down
4 changes: 2 additions & 2 deletions MFDSettingsManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.6.3.0")]
[assembly: AssemblyFileVersion("2.6.3.0")]
[assembly: AssemblyVersion("2.6.4.0")]
[assembly: AssemblyFileVersion("2.6.4.0")]
133 changes: 68 additions & 65 deletions MFDisplay/AuxWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public partial class AuxWindow : Window
/// </summary>
public string FilePath { get; set; }

/// <summary>
/// Is the Configuration for the display valid?
/// </summary>
public bool IsValid { get; private set; } = false;

/// <summary>
/// Ctor
/// </summary>
Expand All @@ -47,23 +52,28 @@ public AuxWindow()
InitializeComponent();
}

private bool IsValid => CheckForValidConfig();

private bool CheckForValidConfig()
{
return Directory.Exists(FilePath)
&& !string.IsNullOrEmpty(Configuration?.FileName)
&& File.Exists(System.IO.Path.Combine(FilePath, Configuration.FileName)
);
if(Directory.Exists(FilePath))
{
if(File.Exists(Path.Combine(FilePath, Configuration?.FileName)))
{
// If there are sub configurations then make sure they are valid
return Configuration?.SubConfigurations?.All(
sub => Directory.Exists(sub?.FilePath)
&& !string.IsNullOrEmpty(sub?.FilePath)
&& File.Exists(Path.Combine(sub?.FilePath, sub?.FileName))) ?? true;
}
}
return false;
}

/// <summary>
/// Uses the Configuration to set the properties for this MFD
/// </summary>
/// <returns></returns>
public bool InitializeWindow(ConfigurationDefinition definition)
{
Visibility = Visibility.Hidden;
Configuration = definition;
Title = definition?.Name;
ResizeMode = ResizeMode.NoResize;
Expand All @@ -72,7 +82,8 @@ public bool InitializeWindow(ConfigurationDefinition definition)
Left = Configuration?.Left ?? 0;
Top = Configuration?.Top ?? 0;
Opacity = Configuration?.Opacity ?? 1.0;
return true;
IsValid = CheckForValidConfig();
return IsValid;
}

/// <summary>
Expand All @@ -82,24 +93,24 @@ public bool InitializeWindow(ConfigurationDefinition definition)
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitializeWindow(Configuration);

if(Opacity == 0)
{
Logger?.Info($"Skipped {Configuration.ToReadableString()}...");
Close();
return;
}
if (Configuration.Enabled)
{
LoadImage();
Logger?.Debug($"Loading the configuration for {Configuration.Name} from Module {Configuration.ModuleName}");
}
else
{
Logger?.Info($"Configuration for {Configuration.Name} for Module {Configuration.ModuleName} is currently disabled in configuration.");
}
}
if (InitializeWindow(Configuration))
{
if (Configuration.Enabled)
{
LoadImage();
Logger?.Debug($"Loading the configuration for {Configuration.Name} from Module {Configuration.ModuleName}");
}
else
{
Logger?.Info($"Configuration for {Configuration.Name} for Module {Configuration.ModuleName} is currently disabled in configuration.");
}
}
else
{
var subConfigs = string.Join(Environment.NewLine, Configuration?.SubConfigurations.Select(sub => sub?.ToReadableString()));
Logger?.Error($"The configuration {Configuration.ToReadableString()} cannot be loaded using the full path: {Path.Combine(FilePath, Configuration.FileName)} OR there is a bad subconfiguration here: {subConfigs ?? "None"}.");
}
}

/// <summary>
/// Loads the specified image, logs failure and closes self if it fails to load
Expand All @@ -108,51 +119,43 @@ public void LoadImage()
{
SubConfigurationDefinition subConfig = null;
IsWindowLoaded = false;
if (!IsValid)
{
Logger?.Error($"The configuration {Configuration.ToReadableString()} cannot be loaded using the full path: {Path.Combine(FilePath, Configuration.FileName)}.");
Close();
}
else
{
var cacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"Vyper Industries\\MFD4CTS\\cache\\{Configuration.ModuleName}");
var cacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"Vyper Industries\\MFD4CTS\\cache\\{Configuration.ModuleName}");

// Previous renders are cached so if the cache file is available then it will be used
var imagePrefix = $"X_{Configuration.XOffsetStart}To{Configuration.XOffsetFinish}Y_{Configuration.YOffsetStart}To{Configuration.YOffsetFinish}_{Configuration.Opacity}";
var cacheFile = Path.Combine(cacheFolder, $"{imagePrefix}_{Configuration.Name}_{Configuration.Width}_{Configuration.Height}.png");
imgMain.Source = GetBitMapSource(Configuration, cacheFolder, cacheFile);
imgMain.Width = Width;
imgMain.Height = Height;
imgMain.Visibility = Visibility.Visible;
// Previous renders are cached so if the cache file is available then it will be used
var imagePrefix = $"X_{Configuration.XOffsetStart}To{Configuration.XOffsetFinish}Y_{Configuration.YOffsetStart}To{Configuration.YOffsetFinish}_{Configuration.Opacity}";
var cacheFile = Path.Combine(cacheFolder, $"{imagePrefix}_{Configuration.Name}_{Configuration.Width}_{Configuration.Height}.png");
imgMain.Source = GetBitMapSource(Configuration, cacheFolder, cacheFile);
imgMain.Width = Width;
imgMain.Height = Height;
imgMain.Visibility = Visibility.Visible;

try
try
{
// First check to see if we are using a SubConfiguration
if (!string.IsNullOrEmpty(SubConfigurationName))
{
// First check to see if we are using a SubConfiguration
if (!string.IsNullOrEmpty(SubConfigurationName))
subConfig = Configuration?.SubConfigurations?.FirstOrDefault(sc => sc.Name.Equals(SubConfigurationName, StringComparison.InvariantCultureIgnoreCase));
if (subConfig != null)
{
subConfig = Configuration?.SubConfigurations?.FirstOrDefault(sc => sc.Name.Equals(SubConfigurationName, StringComparison.InvariantCultureIgnoreCase));
if (subConfig != null)
{
Logger?.Info($"Processing sub-configuration: {subConfig.ToString()} for Module: {Configuration.ModuleName}!");
var insetImagePrefix = $"X_{subConfig.XOffsetStart}To{subConfig.XOffsetFinish}Y_{subConfig.YOffsetStart}To{subConfig.YOffsetFinish}_{subConfig.Opacity}";
var width = subConfig.EndX - subConfig.StartX;
var height = subConfig.EndY - subConfig.StartY;
var insetCacheFile = Path.Combine(cacheFolder, $"{insetImagePrefix}_{subConfig.Name}_{width}_{height}.png");
imgInsert.Source = GetBitMapSource(subConfig, cacheFolder, insetCacheFile);
imgInsert.Width = width;
imgInsert.Height = height;
imgInsert.Opacity = subConfig.Opacity;
imgInsert.Visibility = Visibility.Visible;
}
Logger?.Info($"Processing sub-configuration: {subConfig.ToString()} for Module: {Configuration.ModuleName}!");
var insetImagePrefix = $"X_{subConfig.XOffsetStart}To{subConfig.XOffsetFinish}Y_{subConfig.YOffsetStart}To{subConfig.YOffsetFinish}_{subConfig.Opacity}";
var width = subConfig.EndX - subConfig.StartX;
var height = subConfig.EndY - subConfig.StartY;
var insetCacheFile = Path.Combine(cacheFolder, $"{insetImagePrefix}_{subConfig.Name}_{width}_{height}.png");
imgInsert.Source = GetBitMapSource(subConfig, cacheFolder, insetCacheFile);
imgInsert.Width = width;
imgInsert.Height = height;
imgInsert.Opacity = subConfig.Opacity;
imgInsert.Visibility = Visibility.Visible;
}
}
catch (Exception ex)
{
Logger?.Error($"Unable to load {subConfig?.ToReadableString()}.", ex);
Close();
}
IsWindowLoaded = true;
}
catch (Exception ex)
{
Logger?.Error($"Unable to load {subConfig?.ToReadableString()}.", ex);
Close();
}
IsWindowLoaded = true;
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion MFDisplay/MFDisplay.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<EnableSecurityDebugging>false</EnableSecurityDebugging>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>-mod F-14RHV -submod TGT</StartArguments>
<StartArguments>
</StartArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<StartArguments>-mod F-14R -submod NAV</StartArguments>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions MFDisplay/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.6.3.0")]
[assembly: AssemblyFileVersion("2.6.3.0")]
[assembly: AssemblyVersion("2.6.4.0")]
[assembly: AssemblyFileVersion("2.6.4.0")]
[assembly: Guid("FE237DCB-D0D2-477C-BCC0-6004B30B0D9E")]

10 changes: 5 additions & 5 deletions MFDisplay/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
xsi:schemaLocation="http://tempuri.org/mfdsettings.xsd mfdsettings.xsd" filePath="C:\Users\Scott\Downloads\" defaultConfig="TestJpg">

<DefaultConfigurations>
<add name="LMFD" opacity=".75" width="870" height="700" left="-1280" top="0" xOffsetStart="0" xOffsetFinish="851" yOffsetStart="0" yOffsetFinish="479" />
<add name="RMFD" opacity=".75" width="870" height="700" left="4890" top="0" xOffsetStart="0" xOffsetFinish="851" yOffsetStart="0" yOffsetFinish="479" />
<add name="WHKEY" opacity=".45" width="1200" height="1000" left="2560" top="0" xOffsetStart="0" xOffsetFinish="851" yOffsetStart="0" yOffsetFinish="479" />
<add name="LMFD" enable="true" opacity=".75" width="870" height="700" left="-1280" top="0" xOffsetStart="0" xOffsetFinish="851" yOffsetStart="0" yOffsetFinish="479" />
<add name="RMFD" enable="true" opacity=".75" width="870" height="700" left="4890" top="0" xOffsetStart="0" xOffsetFinish="851" yOffsetStart="0" yOffsetFinish="479" />
<add name="WHKEY" enable="true" opacity=".45" width="1200" height="1000" left="2560" top="0" xOffsetStart="0" xOffsetFinish="851" yOffsetStart="0" yOffsetFinish="479" />
</DefaultConfigurations>
<Modules>
<add moduleName="TestJpg" displayName="Test Pattern JPG" filename="testpattern.jpg">
<Configurations>
<add name="LMFD">
<add name="LMFD" opacity=".2">
<SubConfigurations>
<add name="SubTest" opacity="1" filename="tv-test-pattern-146649_960_720.png" startX="325" startY="350" endX="535" endY="795" xOffsetStart="0" xOffsetFinish="960" yOffsetStart="0" yOffsetFinish="720" />
<add name="SubTest" enable="true" opacity="1.0" filename="tv-test-pattern-146649_960_720.png" startX="200" startY="200" endX="400" endY="400" xOffsetStart="0" xOffsetFinish="960" yOffsetStart="0" yOffsetFinish="720" />
</SubConfigurations>
<!--
-->
Expand Down
6 changes: 3 additions & 3 deletions Setup/Setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -3940,15 +3940,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:MFD4CTS"
"ProductCode" = "8:{18A80962-0CA7-4C1E-AA7A-6B458FEFD02D}"
"PackageCode" = "8:{CC22F24B-2314-43E4-87F9-117881297E81}"
"ProductCode" = "8:{415CD2CB-FC24-493D-92C2-96B5AD4A8907}"
"PackageCode" = "8:{18A2E9A1-504C-4077-B889-A74D0C906B16}"
"UpgradeCode" = "8:{684D58BF-80C4-4679-8A57-A5856787407F}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:TRUE"
"ProductVersion" = "8:2.6.3"
"ProductVersion" = "8:2.6.4"
"Manufacturer" = "8:Vyper Industries"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:https://github.com/ScottyMac52/MFDisplay/issues"
Expand Down

0 comments on commit 8757c47

Please sign in to comment.