-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No longer require administrator rights for normal usage.
Request elevation only in necesarry parts of the code. Request elevation on update. After update, launch application without elevation.
- Loading branch information
Alexandru Macocian
committed
Apr 23, 2021
1 parent
be03f1a
commit 6c2d4a1
Showing
13 changed files
with
250 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace Daybreak.Models | ||
{ | ||
public sealed class ElevationRequest | ||
{ | ||
public object DataContext { get; set; } | ||
public Type View { get; set; } | ||
public string MessageToUser { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Windows.Controls; | ||
|
||
namespace Daybreak.Services.Privilege | ||
{ | ||
public interface IPrivilegeManager | ||
{ | ||
bool AdminPrivileges { get; } | ||
|
||
void RequestAdminPrivileges<TCancelView>(string messageToUser, object dataContextOfCancelView = null) | ||
where TCancelView : UserControl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Daybreak.Models; | ||
using Daybreak.Services.Logging; | ||
using Daybreak.Services.ViewManagement; | ||
using Daybreak.Utils; | ||
using Daybreak.Views; | ||
using System.Extensions; | ||
using System.Security.Principal; | ||
using System.Windows.Controls; | ||
|
||
namespace Daybreak.Services.Privilege | ||
{ | ||
public sealed class PrivilegeManager : IPrivilegeManager | ||
{ | ||
public bool AdminPrivileges => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); | ||
|
||
private readonly IViewManager viewManager; | ||
private readonly ILogger logger; | ||
|
||
public PrivilegeManager( | ||
IViewManager viewManager, | ||
ILogger logger) | ||
{ | ||
this.logger = logger.ThrowIfNull(nameof(logger)); | ||
this.viewManager = viewManager.ThrowIfNull(nameof(viewManager)); | ||
} | ||
|
||
public void RequestAdminPrivileges<TCancelView>(string messageToUser, object dataContextOfCancelView = null) | ||
where TCancelView : UserControl | ||
{ | ||
this.logger.LogInformation("Requesting admin privileges"); | ||
this.viewManager.ShowView<RequestElevationView>(new ElevationRequest { View = typeof(TCancelView), DataContext = dataContextOfCancelView, MessageToUser = messageToUser }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<UserControl x:Class="Daybreak.Views.RequestElevationView" | ||
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:local="clr-namespace:Daybreak.Views" | ||
mc:Ignorable="d" | ||
xmlns:controls="clr-namespace:Daybreak.Controls" | ||
d:DesignHeight="450" d:DesignWidth="800"> | ||
<Grid VerticalAlignment="Center" HorizontalAlignment="Center" Background="White" MinHeight="200" MinWidth="400"> | ||
<StackPanel VerticalAlignment="Center" Orientation="Vertical"> | ||
<TextBlock Text="{Binding MessageToUser}" Margin="10, 0, 10, 0" Foreground="Black" | ||
TextWrapping="Wrap" HorizontalAlignment="Center" FontSize="16"></TextBlock> | ||
<TextBlock Text="Do you want to restart the application with administrator rights?" HorizontalAlignment="Center" | ||
FontSize="16" Foreground="Black" Margin="10, 0, 10, 10"></TextBlock> | ||
<Grid> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="*"></ColumnDefinition> | ||
<ColumnDefinition Width="*"></ColumnDefinition> | ||
</Grid.ColumnDefinitions> | ||
<controls:OpaqueButton Text="Yes" BackgroundOpacity="0.2" TransparentBackground="Gray" HorizontalAlignment="Center" Margin="10" Width="50" Height="25" | ||
Clicked="YesButton_Clicked" Foreground="Black" FontSize="16"></controls:OpaqueButton> | ||
<controls:OpaqueButton Text="No" BackgroundOpacity="0.2" TransparentBackground="Gray" HorizontalAlignment="Center" Margin="10" Width="50" Height="25" | ||
Clicked="NoButton_Clicked" Foreground="Black" Grid.Column="1" FontSize="16"></controls:OpaqueButton> | ||
</Grid> | ||
</StackPanel> | ||
</Grid> | ||
</UserControl> |
Oops, something went wrong.