Skip to content

Commit

Permalink
UI: UAC Elevation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rans4ckeR committed Jan 1, 2024
1 parent 69ffca8 commit 3cd56a3
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CipherPunk.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,8 @@
<DataTemplate DataType="{x:Type ui:DefaultEllipticCurvesViewModel}">
<ui:DefaultEllipticCurvesView/>
</DataTemplate>
<DataTemplate DataType="{x:Type ui:ElevationViewModel}">
<ui:ElevationView/>
</DataTemplate>
</Application.Resources>
</Application>
1 change: 1 addition & 0 deletions CipherPunk.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public App()
.AddSingleton<LoggingViewModel>()
.AddSingleton<DefaultCipherSuitesViewModel>()
.AddSingleton<DefaultEllipticCurvesViewModel>()
.AddSingleton<ElevationViewModel>()
.AddCipherPunk()
.AddCipherSuiteInfoApi();
}).Build();
Expand Down
9 changes: 5 additions & 4 deletions CipherPunk.UI/Infrastructure/BaseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

internal abstract class BaseViewModel : ObservableRecipient
{
private readonly IUacService uacService;
private bool defaultCommandActive;
private bool canExecuteDefaultCommand;
private BitmapSource? uacIcon;
Expand All @@ -18,7 +17,7 @@ internal abstract class BaseViewModel : ObservableRecipient
protected BaseViewModel(ILogger logger, IUacService uacService)
: base(StrongReferenceMessenger.Default)
{
this.uacService = uacService;
UacService = uacService;
IsActive = true;
Logger = logger;
DefaultCommand = new AsyncRelayCommand<bool?>(ExecuteDefaultCommandAsync, _ => CanExecuteDefaultCommand);
Expand All @@ -39,12 +38,14 @@ public bool DefaultCommandActive
}
}

public BitmapSource UacIcon => uacIcon ??= uacService.GetShieldIcon();
public BitmapSource UacIcon => uacIcon ??= UacService.GetShieldIcon();

public bool Elevated => elevated ??= uacService.GetIntegrityLevel().Elevated;
public bool Elevated => elevated ??= UacService.GetIntegrityLevel().Elevated;

protected ILogger Logger { get; }

protected IUacService UacService { get; }

protected bool CanExecuteDefaultCommand
{
get => canExecuteDefaultCommand;
Expand Down
3 changes: 3 additions & 0 deletions CipherPunk.UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<RibbonGroup>
<RibbonButton Label="Windows Default Elliptic Curves" LargeImageSource="{Binding EllipticCurvesButtonImage}" Command="{Binding DefaultEllipticCurvesViewModel.DefaultCommand}"/>
</RibbonGroup>
<RibbonGroup>
<RibbonButton Label="UAC Elevation" LargeImageSource="{Binding CipherSuitesButtonImage}" Command="{Binding ElevationViewModel.DefaultCommand}"/>
</RibbonGroup>
</RibbonTab>
</Ribbon>
<ScrollViewer Grid.Row="1" Grid.Column="0" Background="{StaticResource WindowBackgroundSolidColorBrush}" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
Expand Down
6 changes: 5 additions & 1 deletion CipherPunk.UI/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public MainWindowViewModel(
RemoteServerTestViewModel remoteServerTestViewModel,
LoggingViewModel loggingViewModel,
DefaultCipherSuitesViewModel defaultCipherSuitesViewModel,
DefaultEllipticCurvesViewModel defaultEllipticCurvesViewModel)
DefaultEllipticCurvesViewModel defaultEllipticCurvesViewModel,
ElevationViewModel elevationViewModel)
: base(logger, uacService)
{
IsActive = true;
Expand All @@ -46,6 +47,7 @@ public MainWindowViewModel(
LoggingViewModel = loggingViewModel;
DefaultCipherSuitesViewModel = defaultCipherSuitesViewModel;
DefaultEllipticCurvesViewModel = defaultEllipticCurvesViewModel;
ElevationViewModel = elevationViewModel;
CopyMessageCommand = new RelayCommand(ExecuteCopyMessageCommand);
CloseMessageCommand = new RelayCommand(ExecuteCloseMessageCommand);

Expand Down Expand Up @@ -82,6 +84,8 @@ public MainWindowViewModel(

public DefaultEllipticCurvesViewModel DefaultEllipticCurvesViewModel { get; }

public ElevationViewModel ElevationViewModel { get; }

public double MainContentOpacity
{
get => mainContentOpacity; set => _ = SetProperty(ref mainContentOpacity, value);
Expand Down
32 changes: 32 additions & 0 deletions CipherPunk.UI/UserControls/Elevation/ElevationView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<UserControl x:Class="CipherPunk.UI.ElevationView"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="clr-namespace:CipherPunk.UI"
d:DataContext="{d:DesignInstance Type=ui:ElevationViewModel}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Style="{StaticResource BorderStyle}" Grid.Row="0" Grid.Column="0">
<Expander Header="Current Process UAC Elevation Level">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding MandatoryLevels}" SelectedItem="{Binding MandatoryLevel, Mode=OneWay}" IsEnabled="False"/>
</Grid>
</Expander>
</Border>
</Grid>
</UserControl>
6 changes: 6 additions & 0 deletions CipherPunk.UI/UserControls/Elevation/ElevationView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CipherPunk.UI;

internal sealed partial class ElevationView
{
public ElevationView() => InitializeComponent();
}
33 changes: 33 additions & 0 deletions CipherPunk.UI/UserControls/Elevation/ElevationViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace CipherPunk.UI;

using System.Collections.ObjectModel;

internal sealed class ElevationViewModel : BaseViewModel
{
private ObservableCollection<MandatoryLevel>? mandatoryLevels;
private MandatoryLevel? mandatoryLevel;

public ElevationViewModel(ILogger logger, IUacService uacService)
: base(logger, uacService)
=> UpdateCanExecuteDefaultCommand();

public ObservableCollection<MandatoryLevel>? MandatoryLevels
{
get => mandatoryLevels;
private set => _ = SetProperty(ref mandatoryLevels, value);
}

public MandatoryLevel? MandatoryLevel
{
get => mandatoryLevel;
private set => _ = SetProperty(ref mandatoryLevel, value);
}

protected override Task DoExecuteDefaultCommandAsync(CancellationToken cancellationToken)
{
MandatoryLevels ??= new(Enum.GetValues<MandatoryLevel>().OrderByDescending(q => (int)q));
(MandatoryLevel, _) = UacService.GetIntegrityLevel();

return Task.CompletedTask;
}
}
3 changes: 3 additions & 0 deletions CipherPunk/Services/GroupPolicyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ private static void UpdateOrderPolicy(string valueData, string valueName, REG_VA

using (phkResult)
{
if (regOpenKeyExResult is WIN32_ERROR.ERROR_FILE_NOT_FOUND)
return null;

if (regOpenKeyExResult is not WIN32_ERROR.ERROR_SUCCESS)
throw new Win32Exception((int)regOpenKeyExResult);

Expand Down

0 comments on commit 3cd56a3

Please sign in to comment.