Skip to content

Commit

Permalink
SDA-4562 - Check if admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
KiranNiranjan committed Jun 26, 2024
1 parent f3f787a commit d527dfc
Showing 1 changed file with 62 additions and 36 deletions.
98 changes: 62 additions & 36 deletions installer/win/WixSharpInstaller/Symphony.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//css_imp MaintenanceDialog.cs;
//css_imp MaintenanceDialog.designer.cs;

using System;
using System.Security.Principal;
using WixSharp;
using WixSharp.Forms;
using Microsoft.Deployment.WindowsInstaller;
Expand Down Expand Up @@ -532,55 +534,59 @@ public static ActionResult CleanRegistryCurrentUser(Session session)
[CustomAction]
public static ActionResult CleanNSISRegistryForCurrentUser(Session session)
{
try
{
const string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
const string displayNameValue = "Symphony";

// Open the Uninstall key
using (var key = Registry.CurrentUser.OpenSubKey(uninstallKey))
if (IsUserAdministrator())
{
try
{
if (key == null)
{
session.Log("Uninstall key not found.");
return ActionResult.Success;
}
const string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
const string displayNameValue = "Symphony";

// Iterate through all subkeys
foreach (string subkeyName in key.GetSubKeyNames())
// Open the Uninstall key
using (var key = Registry.CurrentUser.OpenSubKey(uninstallKey))
{
using (var subkey = key.OpenSubKey(subkeyName))
if (key == null)
{
if (subkey == null)
{
continue;
}
session.Log("Uninstall key not found.");
return ActionResult.Success;
}

// Get the DisplayName value
string displayName = subkey.GetValue("DisplayName") as string;
if (displayName == displayNameValue)
// Iterate through all subkeys
foreach (string subkeyName in key.GetSubKeyNames())
{
using (var subkey = key.OpenSubKey(subkeyName))
{
// Get the UninstallString value
string uninstallString = subkey.GetValue("QuietUninstallString") as string;
if (!string.IsNullOrEmpty(uninstallString))
if (subkey == null)
{
// Start the uninstallation process
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = string.Format("/c \"{0}\"", uninstallString);
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
continue;
}

// Get the DisplayName value
string displayName = subkey.GetValue("DisplayName") as string;
if (displayName == displayNameValue)
{
// Get the UninstallString value
string uninstallString = subkey.GetValue("QuietUninstallString") as string;
if (!string.IsNullOrEmpty(uninstallString))
{
// Start the uninstallation process
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = string.Format("/c \"{0}\"", uninstallString);
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
}
}
}
}
}
}
catch (System.Exception e)
{
session.Log("Error executing CleanNSISRegistryForCurrentUser: " + e.ToString());
return ActionResult.Success;
catch (System.Exception e)
{
session.Log("Error executing CleanNSISRegistryForCurrentUser: " + e.ToString());
return ActionResult.Success;
}
}
return ActionResult.Success;
}
Expand Down Expand Up @@ -609,4 +615,24 @@ public static ActionResult StartAfterInstall(Session session)
}
return ActionResult.Success;
}

private static bool IsUserAdministrator()
{
bool isAdmin;
try
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException ex)
{
isAdmin = false;
}
catch (Exception ex)
{
isAdmin = false;
}
return isAdmin;
}
}

0 comments on commit d527dfc

Please sign in to comment.