Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable auto-elevation for a profile, redux #11310

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
efe42d1
stunned that this remotely works as well as it does
zadjii-msft Sep 2, 2021
abc0265
marginally reduce the risk of a merge conflict
zadjii-msft Sep 2, 2021
974d18a
Merge remote-tracking branch 'origin/main' into dev/migrie/f/632-on-w…
zadjii-msft Sep 22, 2021
8b8de52
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Sep 22, 2021
8895e06
these are notes, but they're useless who tf am I kidding
zadjii-msft Sep 27, 2021
9b1e30b
I can't believe how dumb this is (or how well it works)
zadjii-msft Sep 27, 2021
904045b
Tons of comments
zadjii-msft Sep 27, 2021
28bde43
Works with splitting panes again
zadjii-msft Sep 28, 2021
c2b759e
stick these projects in a utils folder because it's getting crowded
zadjii-msft Sep 28, 2021
571b8a9
Merge remote-tracking branch 'origin/dev/migrie/f/non-terminal-conten…
zadjii-msft Sep 29, 2021
4bcdfc3
This should have been in the parent
zadjii-msft Sep 29, 2021
6717b4d
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Sep 30, 2021
ab78ad4
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Oct 27, 2021
8d7f726
spel
zadjii-msft Oct 27, 2021
88da035
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Oct 28, 2021
80fe386
tiny nits
zadjii-msft Oct 28, 2021
7597114
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Oct 28, 2021
fdd283d
other pr nits, test comments
zadjii-msft Oct 28, 2021
ebef341
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Nov 1, 2021
370a7aa
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Nov 10, 2021
3d41cba
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Nov 10, 2021
1b4efad
Merge branch 'dev/migrie/f/non-terminal-content-elevation-warning' in…
zadjii-msft Nov 11, 2021
7a3ec1e
Fix this test
zadjii-msft Nov 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions src/cascadia/LocalTests_TerminalApp/SettingsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace TerminalAppLocalTests

TEST_METHOD(TestIterableColorSchemeCommands);

TEST_METHOD(TestElevateArg);

TEST_CLASS_SETUP(ClassSetup)
{
return true;
Expand Down Expand Up @@ -1193,4 +1195,247 @@ namespace TerminalAppLocalTests
}
}

void SettingsTests::TestElevateArg()
{
const std::string settingsJson{ R"(
{
"defaultProfile": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"profiles": [
{
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"commandline": "cmd.exe"
},
{
"name": "profile1",
"guid": "{6239a42c-1111-49a3-80bd-e8fdd045185c}",
"elevate": true,
"commandline": "pwsh.exe"
},
{
"name": "profile2",
"guid": "{6239a42c-2222-49a3-80bd-e8fdd045185c}",
"elevate": false,
"commandline": "wsl.exe"
}
],
"keybindings": [
{ "keys": ["ctrl+a"], "command": { "action": "newTab", "profile": "profile0" } },
{ "keys": ["ctrl+b"], "command": { "action": "newTab", "profile": "profile1" } },
{ "keys": ["ctrl+c"], "command": { "action": "newTab", "profile": "profile2" } },

{ "keys": ["ctrl+d"], "command": { "action": "newTab", "profile": "profile0", "elevate": false } },
{ "keys": ["ctrl+e"], "command": { "action": "newTab", "profile": "profile1", "elevate": false } },
{ "keys": ["ctrl+f"], "command": { "action": "newTab", "profile": "profile2", "elevate": false } },

{ "keys": ["ctrl+g"], "command": { "action": "newTab", "profile": "profile0", "elevate": true } },
{ "keys": ["ctrl+h"], "command": { "action": "newTab", "profile": "profile1", "elevate": true } },
{ "keys": ["ctrl+i"], "command": { "action": "newTab", "profile": "profile2", "elevate": true } },
]
})" };

const winrt::guid guid0{ ::Microsoft::Console::Utils::GuidFromString(L"{6239a42c-0000-49a3-80bd-e8fdd045185c}") };
const winrt::guid guid1{ ::Microsoft::Console::Utils::GuidFromString(L"{6239a42c-1111-49a3-80bd-e8fdd045185c}") };
const winrt::guid guid2{ ::Microsoft::Console::Utils::GuidFromString(L"{6239a42c-2222-49a3-80bd-e8fdd045185c}") };

CascadiaSettings settings{ til::u8u16(settingsJson) };

auto keymap = settings.GlobalSettings().KeyMap();
VERIFY_ARE_EQUAL(3u, settings.ActiveProfiles().Size());

const auto profile2Guid = settings.ActiveProfiles().GetAt(2).Guid();
VERIFY_ARE_NOT_EQUAL(winrt::guid{}, profile2Guid);

VERIFY_ARE_EQUAL(9u, keymap.Size());

{
KeyChord kc{ true, false, false, static_cast<int32_t>('A') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile0", realArgs.TerminalArgs().Profile());
VERIFY_IS_NULL(realArgs.TerminalArgs().Elevate());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid0, guid);
VERIFY_ARE_EQUAL(L"cmd.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(false, termSettings.Elevate());
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>('B') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile1", realArgs.TerminalArgs().Profile());
VERIFY_IS_NULL(realArgs.TerminalArgs().Elevate());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid1, guid);
VERIFY_ARE_EQUAL(L"pwsh.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(true, termSettings.Elevate());
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>('C') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile2", realArgs.TerminalArgs().Profile());
VERIFY_IS_NULL(realArgs.TerminalArgs().Elevate());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid2, guid);
VERIFY_ARE_EQUAL(L"wsl.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(false, termSettings.Elevate());
}

{
KeyChord kc{ true, false, false, static_cast<int32_t>('D') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile0", realArgs.TerminalArgs().Profile());
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs().Elevate());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Elevate().Value());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid0, guid);
VERIFY_ARE_EQUAL(L"cmd.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(false, termSettings.Elevate());
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>('E') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile1", realArgs.TerminalArgs().Profile());
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs().Elevate());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Elevate().Value());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid1, guid);
VERIFY_ARE_EQUAL(L"pwsh.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(false, termSettings.Elevate());
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>('F') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile2", realArgs.TerminalArgs().Profile());
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs().Elevate());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Elevate().Value());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid2, guid);
VERIFY_ARE_EQUAL(L"wsl.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(false, termSettings.Elevate());
}

{
KeyChord kc{ true, false, false, static_cast<int32_t>('G') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile0", realArgs.TerminalArgs().Profile());
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs().Elevate());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Elevate().Value());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid0, guid);
VERIFY_ARE_EQUAL(L"cmd.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(true, termSettings.Elevate());
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>('H') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile1", realArgs.TerminalArgs().Profile());
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs().Elevate());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Elevate().Value());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid1, guid);
VERIFY_ARE_EQUAL(L"pwsh.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(true, termSettings.Elevate());
}
{
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
KeyChord kc{ true, false, false, static_cast<int32_t>('I') };
auto actionAndArgs = TestUtils::GetActionAndArgs(keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::NewTab, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<NewTabArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Commandline().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().StartingDirectory().empty());
VERIFY_IS_TRUE(realArgs.TerminalArgs().TabTitle().empty());
VERIFY_IS_FALSE(realArgs.TerminalArgs().Profile().empty());
VERIFY_ARE_EQUAL(L"profile2", realArgs.TerminalArgs().Profile());
VERIFY_IS_NOT_NULL(realArgs.TerminalArgs().Elevate());
VERIFY_IS_TRUE(realArgs.TerminalArgs().Elevate().Value());

const auto [guid, termSettings] = winrt::TerminalApp::implementation::TerminalSettings::BuildSettings(settings, realArgs.TerminalArgs(), nullptr);
VERIFY_ARE_EQUAL(guid2, guid);
VERIFY_ARE_EQUAL(L"wsl.exe", termSettings.Commandline());
VERIFY_ARE_EQUAL(true, termSettings.Elevate());
}
}

}
20 changes: 20 additions & 0 deletions src/cascadia/TerminalApp/TabManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ namespace winrt::TerminalApp::implementation
const auto profile{ _settings.GetProfileForArgs(newTerminalArgs) };
const auto settings{ TerminalSettings::CreateWithNewTerminalArgs(_settings, newTerminalArgs, *_bindings) };

// Try to handle auto-elevation
const bool requestedElevation = settings.DefaultSettings().Elevate();
const bool currentlyElevated = _isElevated();

// We aren't elevated, but we want to be.
if (requestedElevation && !currentlyElevated)
{
// Manually set the Profile of the NewTerminalArgs to the guid we've
// resolved to. If there was a profile in the NewTerminalArgs, this
// will be that profile's GUID. If there wasn't, then we'll use
// whatever the default profile's GUID is.

newTerminalArgs.Profile(::Microsoft::Console::Utils::GuidToString(profile.Guid()));
_OpenElevatedWT(newTerminalArgs);
return S_OK;
}
// We can't go in the other direction (elevated->unelevated)
// unfortunately. This seems to be due to Centennial quirks. It works
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we filed this in papercuts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(or written BODGY here... cuz that's a representation of cut workarounds...)

// unpackaged, but not packaged.

_CreateNewTabWithProfileAndSettings(profile, settings, existingConnection);

const uint32_t tabCount = _tabs.Size();
Expand Down
Loading